Skip to content

ModuleNotFoundError using a custom apps directory & path #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
richkirsch opened this issue Nov 8, 2019 · 3 comments · Fixed by #498
Closed

ModuleNotFoundError using a custom apps directory & path #224

richkirsch opened this issue Nov 8, 2019 · 3 comments · Fixed by #498

Comments

@richkirsch
Copy link

To make my apps more reusable, I prefer to use an apps directory in my project root to house them, with the following structure:

--Project/
    -- manage.py
    -- apps/
        -- app1/
            -- app.py
            -- models.py
            -- etc...
    -- etc ...

To make life easier, in manage.py, I have the following:

current_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_path, "apps"))

This enables me to use e.g. from app1 import SomeClass anywhere in the project, instead of from apps.app1 import SomeClass.

However, this is causing mypy with django-stubs (though not without it) to crash a with ModuleNotFoundError:

  File "/Users/me/Projects/AwesomeProject/apps/app2/models.py", line 4, in <module>
    from app1.models import SomeModel
ModuleNotFoundError: No module named 'app1'

I had a similar issue with PyLint, but was able to solve it with the help of this StackOverflow page, taking advatange of an initialisation hook to set the system path in a similar way to above but in .pylintrc, like so:

[MASTER]
init-hook='
  import os, sys;
  from pylint.config import find_pylintrc;
  sys.path.append(os.path.join(os.path.dirname(find_pylintrc()), 'apps'))'
load-plugins=pylint_django, pylint_celery
...

My questions are:
1) Is there a similar approach I can take?
2) Otherwise, what are alternative suggestions?

@Atala
Copy link

Atala commented Mar 9, 2020

I think a workaround is to add the apps folder to your PYTHONPATH :

PYTHONPATH=:<path_to_apps_folder> mypy

@LucidDan
Copy link

I do this too, using a dedicated apps directory, and as @Atala says, I adjust my PYTHONPATH, which works fine.

@flyte
Copy link

flyte commented May 4, 2020

This isn't possible when you don't control the calling environment creation such as when mypy is called from the Python extension in VSCode.

Could this module respect the mypy_path mypy.ini setting and add those paths before trying to import the Django settings file?

https://mypy.readthedocs.io/en/stable/config_file.html#import-discovery

danifus added a commit to danifus/django-stubs that referenced this issue Oct 22, 2020
danifus added a commit to danifus/django-stubs that referenced this issue Oct 22, 2020
danifus added a commit to danifus/django-stubs that referenced this issue Oct 22, 2020
sobolevn pushed a commit that referenced this issue Oct 22, 2020
voidus pushed a commit to voidus/django-stubs that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants