close

[Solved] ModuleNotFoundError: What does it mean __main__ is not a package?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ModuleNotFoundError: What does it mean main is not a package? in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ModuleNotFoundError: What does it mean main is not a package Error Occurs?

Today I get the following error ModuleNotFoundError: What does it mean main is not a package? in python.

How To Solve ModuleNotFoundError: What does it mean main is not a package Error ?

  1. How To Solve ModuleNotFoundError: What does it mean main is not a package Error ?

    To Solve ModuleNotFoundError: What does it mean main is not a package Error So I came up with a cleaner solution without changing python environment valuables (after looking up how requests do in relative import):

  2. ModuleNotFoundError: What does it mean main is not a package?

    To Solve ModuleNotFoundError: What does it mean main is not a package Error So I came up with a cleaner solution without changing python environment valuables (after looking up how requests do in relative import):

Solution 1

Simply remove the dot for the relative import and do:

from p_02_paying_debt_off_in_a_year import compute_balance_after

Solution 2

I have the same issue as you did. I think the problem is that you used relative import in in-package import. There is no __init__.py in your directory. So just import as Moses answered above.

The core issue I think is when you import with a dot:

from .p_02_paying_debt_off_in_a_year import compute_balance_after

It is equivalent to:

from __main__.p_02_paying_debt_off_in_a_year import compute_balance_after

where __main__ refers to your current module p_03_using_bisection_search.py.


Briefly, the interpreter does not know your directory architecture.

When the interpreter get in p_03.py, the script equals:

from p_03_using_bisection_search.p_02_paying_debt_off_in_a_year import compute_balance_after

and p_03_using_bisection_search does not contain any modules or instances called p_02_paying_debt_off_in_a_year.


So I came up with a cleaner solution without changing python environment valuables (after looking up how requests do in relative import):

The main architecture of the directory is:

main.py
setup.py
problem_set_02/
   __init__.py
   p01.py
   p02.py
   p03.py

Then write in __init__.py:

from .p_02_paying_debt_off_in_a_year import compute_balance_after

Here __main__ is __init__ , it exactly refers to the module problem_set_02.

Then go to main.py:

import problem_set_02

You can also write a setup.py to add specific module to the environment.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read