Relative imports ModuleNotFoundError No module named x

Relative imports ModuleNotFoundError No module named x is an error which occurs in Python when you are using relative imports the wrong way.

In this article/Blog post I am going to show you why this error is happening and how you can solve the issue at hand.

I am also going to share with you other possible solutions which have worked for most developers with this problem.

Explaining the Error : Relative imports ModuleNotFoundError No module named x

First of all we need to understand why the error happens at all, but before we do that we need to reproduce the error. In order to do that we will use an example where the developer has two python files.

config.py and test.py, the error happens when the developer is trying to import the config file from/from inside test.py. test.py contains the code bellow

                                                                       #
import config
print (config.debug)
                                                                       #

This did not work, so the developer tries absolute imports and changed the code of test.py to the code bellow.

                                                                       #
from . import config
                                                                       #

In both cases the developer will get the error bellow, which is the error we want to solve.

                                                                       #

ModuleNotFoundError: No module named 'config'
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully solve the problem

Solution 1 : use absolute imports

The solution is simple, use absolute imports and append your project’s path to PYTHONPATH

let us say that our project structure looks like the image bellow.

When importing you should start from the root directory and reference each import.

For example, if you want to import in module b.py, you should do it similar to the code bellow.

                                                                       #
# in module b
import anotherpackage.c
import mypackage.a
                                                                       #

For example, if you want to import in module a.py, you should do it similar to the code bellow.

                                                                       #
# in module a.py
import anotherpackage.mysubpackage.d
                                                                       #

This is how you append your project’s path to PYTHONPATH in windows for example

                                                                       #
set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\project\
                                                                       #

Solution 2 : import from sys.path

The second solution is for people who want to use Relative imports.

The trick is to import something available inside the path sys.path

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

This is the end of our article, I hope this helped you solve the error or at least showed you the way.

You can donate to our Kofi account but you do not Have to.

Keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/