Search code examples
pythondjangopython-3.xpython-import

Import app in django project


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.


Solution

  • Example project (that 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. My guess is that you (or PyCharm) try to run some file or import module when you (or PyCharm) are in app directory. Current directory (whatever it means) should be was (that one created by django-admin.exe. Hope it helps.