Help with import

Hello Django Masters. Could some one help me with importing modules? I’m new in Django, so please don’t judge me too hard.

This is my project structure:

Main/
├── Commons
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── modules
│   │   └── network.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── Restapi
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── Main
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── __init__.py
└── manage.py

So my issues is:
In Commons directory in views.py I need to import modules/network.py

Way where view.py was able to execute:
from modules import network

But, than I get an error when I tried to do python3 manage.py migrate

    from modules     import network
ModuleNotFoundError: No module named 'modules'

I could say, that this error maybe resolved a long long time ago, by someone, but I did not undesnartd at all, please help me with this

When you’re running a Django project, directory references are relative to the base directory, not the directory containing the code you’re currently running.

Try:
from Commons.modules import network

1 Like

Hello, @KenWhitesell thank you, for your replay, I’ve got an error again

> Traceback (most recent call last):
>   File "Main/Commons/views.py", line 1, in <module>
>     from Commons.modules import network
> ModuleNotFoundError: No module named 'Commons'
> 
> Process finished with exit code 1

I’ve tried from ..Commons.modules import network as

Again an error:

Traceback (most recent call last):
  File "Main/Commons/views.py", line 1, in <module>
    from ..Commons.modules.network import endpoints
ImportError: attempted relative import with no known parent package

Process finished with exit code 1

In this case, try from .modules import network

What version of Django and which version of Python are you using? You might need a __init__.py file in that modules directory.

1 Like

I thought I don’t need the __init__.py since my:
Django version is: 3.2.4
Python is: 3.9.4