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

This error occurs when you try to import a module using a relative import, but Python cannot find the module’s parent package.

To fix this error attempted relative import with no known parent package, you need to make sure that the module being imported is part of a package, and that the package is on the Python path. You can do this by adding an empty init.py file to the package’s directory, and by making sure that the directory containing the package is on the Python path.

Alternatively, you can run the module using the -m flag with the Python interpreter, like this:

python -m mypackage.mymodule

This tells Python to run mymodule.py as a module, which will allow it to correctly resolve relative imports.

The main advantage of using relative imports is that they make it easier to move packages and modules around within a project, without having to update all the import statements. They also make it easier to reuse code across multiple projects, because the import paths can be modified to match the package hierarchy of each project.