Import application into django project

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 #here i'm in a artists app file 

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.

+5
source share
2 answers

Sample project (works).

Project structure

 test_project/ test_project/ settings.py urls.py wsgi.py app1/ models.py app2/ models.py 

app1 / models.py

 from django.db import models class Restaurant(models.Model): name = models.CharField(max_length=255) 

app2 / models.py

 from django.db import models from app1.models import Restaurant class Waiter(models.Model): restaurant = models.ForeignKey(Restaurant) name = models.CharField(max_length=255) 

In your case, both from ..crew.models import MyClass and from crew.models import MyClass should work. I assume that you (or PyCharm) are trying to run some file or import a module when you (or PyCharm) are in the application directory. The current directory (regardless of its value) should be was (the one created by django-admin.exe ). Hope this helps.

+2
source

In django projects you do not need to specify the path from the root to the project directory. Also BASE_DIR is the path to your django project, when you import any application into django, you just import it using your name.

So, your import will be.

 from crew.models import MyClass 
+2
source

All Articles