ModuleNotFoundError:__main__不是软件包是什么意思?

问题:ModuleNotFoundError:__main__不是软件包是什么意思?

我正在尝试从控制台运行模块。我的目录结构是这样的:

我正在尝试使用以下命令p_03_using_bisection_search.pyproblem_set_02目录中运行模块:

$ python3 p_03_using_bisection_search.py

里面的代码p_03_using_bisection_search.py是:

__author__ = 'm'


from .p_02_paying_debt_off_in_a_year import compute_balance_after


def compute_bounds(balance: float,
                   annual_interest_rate: float) -> (float, float):

    # there is code here, but I have omitted it to save space
    pass


def compute_lowest_payment(balance: float,
                           annual_interest_rate: float) -> float:

    # 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()

我正在导入p_02_paying_debt_off_in_a_year.py其中代码是的函数:

__author__ = 'm'


def compute_balance(balance: float,
                    fixed_payment: float,
                    annual_interest_rate: float) -> float:

    # 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:

    # Omitted code
    pass


def compute_fixed_monthly_payment(balance: float,
                                  annual_interest_rate: float) -> float:

    # 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()

我收到以下错误:

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

我不知道如何解决这个问题。我尝试添加__init__.py文件,但是仍然无法正常工作。

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'


from .p_02_paying_debt_off_in_a_year import compute_balance_after


def compute_bounds(balance: float,
                   annual_interest_rate: float) -> (float, float):

    # there is code here, but I have omitted it to save space
    pass


def compute_lowest_payment(balance: float,
                           annual_interest_rate: float) -> float:

    # 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'


def compute_balance(balance: float,
                    fixed_payment: float,
                    annual_interest_rate: float) -> float:

    # 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:

    # Omitted code
    pass


def compute_fixed_monthly_payment(balance: float,
                                  annual_interest_rate: float) -> float:

    # 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.


回答 0

只需删除相对导入的点,然后执行以下操作:

from p_02_paying_debt_off_in_a_year import compute_balance_after

Simply remove the dot for the relative import and do:

from p_02_paying_debt_off_in_a_year import compute_balance_after

回答 1

我和你有同样的问题。我认为问题是您在中使用了相对导入in-package import__init__.py您的目录中没有。因此,请按照以上摩西的回答进行导入。

我认为核心问题是在导入点时:

from .p_02_paying_debt_off_in_a_year import compute_balance_after

它等效于:

from __main__.p_02_paying_debt_off_in_a_year import compute_balance_after

where __main__指您当前的模块p_03_using_bisection_search.py


简而言之,解释器不知道您的目录体系结构。

当解释器进入时p_03.py,脚本等于:

from p_03_using_bisection_search.p_02_paying_debt_off_in_a_year import compute_balance_after

并且p_03_using_bisection_search不包含任何名为的模块或实例p_02_paying_debt_off_in_a_year


因此,我想出了一个更干净的解决方案,而无需更改python环境的贵重物品(在查找请求在相对导入中的作用之后):

该目录的主要体系结构是:

main.py

setup.py

problem_set_02/

——__init__.py

——p01.py

——p02.py

——p03.py

然后写__init__.py

from .p_02_paying_debt_off_in_a_year import compute_balance_after

这里__main____init__,它究竟指的是模块problem_set_02

然后转到main.py

import problem_set_02

您也可以编写一个setup.py将特定模块添加到环境中。

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.


回答 2

尝试将其运行为:

python3 -m p_03_using_bisection_search

Try to run it as:

python3 -m p_03_using_bisection_search


回答 3

您好,请按照以下步骤操作,您将解决此问题。如果已创建目录和子目录,请按照以下步骤操作,请记住,所有目录必须必须 __init__.py将其识别为目录。

  1. import sys并运行sys.path,您将能够看到python搜索的所有路径。您必须能够看到当前的工作目录。

  2. 现在,按照以下命令导入要使用import使用的子目录和相应模块:import subdir.subdir.modulename as abc现在,您可以使用该模块中的方法。 屏幕截图相同的问题

如您在此屏幕快照中看到的,我有一个父目录和两个子目录,在第二个子目录下,我有module == CommonFunction,执行sys.path后您会看到右侧,我可以看到我的工作目录

If you have created directory and sub-directory, follow the steps below and please keep in mind all directory must have __init__.py to get it recognized as a directory.

  1. In your script, include import sys and sys.path, you will be able to see all the paths available to 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 subdir.subdir.modulename as abc and now you can use the methods in that module.

As an example, you can see in this screenshot I have one parent directory and two sub-directories and under second sub-directories I have the module CommonFunction. On the right my console shows that after execution of sys.path, I can see my working directory.


回答 4

删除点并在文件开头导入absolute_import

from __future__ import absolute_import

from p_02_paying_debt_off_in_a_year import compute_balance_after

Remove the dot and import absolute_import in the beginning of your file

from __future__ import absolute_import

from p_02_paying_debt_off_in_a_year import compute_balance_after

回答 5

只需使用.py文件所在的主文件夹的名称即可。

from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after

Just use the name of the main folder which the .py file is in.

from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after