I had a problem importing an application into another application in my django project. I know that there is a question / questionnaire for this question, and believe me, I read a lot of them, even some about importing python.
Here is my project tree (I will put the name of the real folders):
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 project (/ home / user / project / was) contains virtualenv creates folders (python3.4).
I checked my python sys.path and my project structure in Pycharm, and there is / home / user / project / was.
When I do this in PyCharm, I have autocomplete working fine:
from ..crew.models import MyClass
But I get ValueError :attempted relative import beyond top-level package when import app
And now, the same scenario, importing a crew class in an application for artists, but:
from was.crew.models import MyClass
Autocomplete works fine in pycharm, but this time I got the classic ImportError: no name was.crew .
I find a solution to do this by adding this line to my .py settings:
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 exists in the settings. And now I can do it:
from crew.models import MyClass
And here I did not get an error (change some settings in Pycharm so that autocomplete works).
This works, but I'm really curious why I should add this line and why my first two attempts do not work.
I got a little lost with the package, pythonpath, etc. (therefore, I indicated all __init__.py files in my scheme).
Shouldn't from ..anotherapp.models import Class work fine without putting anything into settings.py?
Anyway, should I keep my decision or is it not very good?
Thanks in advance for any response.
PS: note that I already tried to have __init__.py files in my first and second folder without success.