I had a problem with an import of an app in another app in my django project. I know there are severals question/asnwsers on this subject, and believe me I read a lot of them, even some about python import.
Here is my project tree (i'll put real folders name):
was/ # full path from my computer /home/user/project/was
....was/ #django project created by django-admin startproject 'was'
....manage.py
....artists/ #first app
....migrations/
....templates/
....__init__.py
....other_python_files.py
....crew/ #second app
....migrations/
....templates/
....__init__.py
....some_files.py
....was/ # folder containing settings.py, main urls.py
....__init__.py
My first was project (/home/user/project/was) contains the virtualenv generates folders (python3.4).
I checked on my python path sys.path
and my project structure in Pycharm and there is /home/user/project/was.
When I do this in PyCharm, I have the autocompletion working fine :
from ..crew.models import MyClass #here i'm in a artists app file
But I get an ValueError :attempted relative import beyond top-level package when import app
And now, same scenario, importing a crew class in artists app but :
from was.crew.models import MyClass
Autocompletion works fine in pycharm but this time I got the classic ImportError : no name was.crew
.
I find a solution doing it by adding this line in my settings.py :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR)) # add this line
Note that BASE_DIR
already exist in settings.
And now I can do this :
from crew.models import MyClass
And here I got no error (change some settings in Pycharm to have the autocompletion works).
This works, but I really wonder why I have to add this line and why my two first attempt don't work.
I'm a bit lost with package, pythonpath etc (that's why I specified in my schema all __init__.py
files).
Shouldn't from ..anotherapp.models import Class
works fine without pimping anything in settings.py ?
Whatever, should I keep my solution or is it not a good thing?
Thanks in advance for any response.
PS : note that I already try to had __init__.py
files in my first and second was folder, without success.