Solve ModuleNotFoundError What does it mean __main__ is not a package

ModuleNotFoundError What does it mean __main__ is not a package is an error which happens in python when a relative import is not needed.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Explaining the Error : ModuleNotFoundError What does it mean __main__ is not a package

First, we need to reproduce the error, let us start by checking out the message of the error. Bellow is the message displayed with the error.

                                                                       #
ModuleNotFoundError: No module named '__main__.doodly'; '__main__' is not a package
                                                                       #

The error shows up after we use a relative import to import doodly.py from inside jojo.py

jojo.py contains the code bellow.

                                                                       #
from .p_doodly import svintaball


def svinta(balance: float,
                   rate: float) -> (float, float):
...
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : remove the relative import

The first solution is to remove the relative import

In the code above, we should replace the line bellow.

                                                                       #
from .p_doodly import svintaball
                                                                       #

With the following line of code.

                                                                       #
from p_doodly import svintaball
                                                                       #

Notice that we have removed the dot which is how you do a relative import in Python. As a result we are using a normal import and not a relative import.

I hope this solution has worked for you. If not, then follow the solution bellow.

Solution 2 : __init__.py

When the error message at hand happens when we are trying to import from a directory which is not recognized as a directory.

You should start your script by adding sys.path and import sys at the beginning.

                                                                       #
import arma.belt.synch.py as sy # import subdir.subdir.modulename as somemodule
                                                                       #

In the example above we have imported a module named synch.py from a subdirectory called belt which lives inside a subdirectory called arma.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

The error in this article is one of the most confusing errors in Python, I hope this article has helped you solve the issue or at least pointed you in the right direction. Keep learning guys, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/