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

匿名 (未验证) 提交于 2019-12-03 08:28:06

问题:

I am trying to run a module from the console. The structure of my directory is this:

I am trying to run the module p_03_using_bisection_search.py, from the problem_set_02 directory using:

$ python3 p_03_using_bisection_search.py

The code inside p_03_using_bisection_search.pyis:

__author__ = 'm'  """ Docstring """  from .p_02_paying_debt_off_in_a_year import compute_balance_after   def compute_bounds(balance: float,                    annual_interest_rate: float) -> (float, float):     """"     Docstring     """     # there is code here, but I have omitted it to save space     pass   def compute_lowest_payment(balance: float,                            annual_interest_rate: float) -> float:     """     Docstring     """     # there is code here, but I have omitted it to save space     pass      def main():     balance = eval(input('Enter the initial balance: '))     annual_interest_rate = eval(input('Enter the annual interest rate: '))      lowest_payment = compute_lowest_payment(balance, annual_interest_rate)     print('Lowest Payment: ' + str(lowest_payment))   if __name__ == '__main__':     main()

I am importing a function that is in p_02_paying_debt_off_in_a_year.py which code is:

__author__ = 'm'  """ Docstring """   def compute_balance(balance: float,                     fixed_payment: float,                     annual_interest_rate: float) -> float:     """     Docstring     """     # this is code that has been omitted     pass   def compute_balance_after(balance: float,                           fixed_payment: float,                           annual_interest_rate: float,                           months: int=12) -> float:     """     Docstring     """     # Omitted code     pass   def compute_fixed_monthly_payment(balance: float,                                   annual_interest_rate: float) -> float:     """     Docstring     """     # omitted code     pass   def main():     balance = eval(input('Enter the initial balance: '))     annual_interest_rate = eval(         input('Enter the annual interest rate as a decimal: '))     lowest_payment = compute_fixed_monthly_payment(balance,                                                    annual_interest_rate)     print('Lowest Payment: ' + str(lowest_payment))   if __name__ == '__main__':     main()

I am getting the following error:

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

I have no idea how to solve this issue. I have tried adding a __init__.py file, but it is still not working.

回答1:

Simply remove the dot for the relative import and do:

from p_02_paying_debt_off_in_a_year import compute_balance_after


回答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 ,like :

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 .

We all know that __main__ refers to your current module p_03_using_bisection_search.py.

Here comes the issue:

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

Apparently, p_03_using_bisection_search does not contain any modules or instances called p_02_paying_debt_off_in_a_year.

Briefly, the interpreter does not know your directory architecture.


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.



回答3:

Hi Please follow below step, you will resolve this problem. If you have created directory and sub-directory then follow below steps and please keep in mind all directory must have "init.py" to get it recognized as a directory.

  1. "import sys" and run "sys.path" , you will be able to see all path that is being search by python.You must be able to see your current working directory.

  2. Now import sub-directory and respective module that you want to use using import follow this command: "import subdir.subdir.modulename as abc" and now you can use the methods in that module. ScreenShotforSameIssue

as you can see in this screenshot I have one parent directory and two sub-directories and under second sub-directories i have module==CommonFunction and you see right side after execution of sys.path I can see my working directory



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!