Search code examples
pythondjangopython-importrelative-path

Relative import in Django


I am currently developing a client/server application. Client and server are both Django projects.

I would like to import client models in the server application. Here is the arborescence of my files :

client/
    fileapp/
        __init__.py
        models.py
        ...
    client/
        __init__.py
        ...
    manage.py

server/
    fileapp/
        __init__.py
        views.py
        ...
    server/
        __init__.py
        ...
    manage.py

I would like in my server project, import client/fileapp/models. I try to do this with Django shell and make relative import with that instruction :

>>> from ..client.fileapp.models import *
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named client.fileapp.models

I also try to append the client path to sys.path but it doesn't work either. If you know how to do kind of that mechanism, please tell me.

Thank you.


Solution

  • client/
        fileapp/
            __init__.py
            models.py
            ...
        client/
            __init__.py
            ...
        __init__.py
        manage.py
    
    server/
        fileapp/
            __init__.py
            views.py
            ...
        server/
            __init__.py
            ...
        __init__.py
        manage.py
    __init__.py
    ...