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

新手上路,请多包涵

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

在此处输入图像描述

我正在尝试从 problem_set_02 目录运行模块 p_03_using_bisection_search.py ,使用:

 $ 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 文件,但它仍然无法正常工作。

原文由 lmiguelvargasf 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 740
2 个回答

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

 from p_02_paying_debt_off_in_a_year import compute_balance_after

原文由 Moses Koledoye 发布,翻译遵循 CC BY-SA 3.0 许可协议

我和你有同样的问题。我认为问题是您在 in-package import 中使用了相对导入。您的目录中没有 __init__.py 。因此,只需按照 Moses 上面的回答进行导入即可。

我认为核心问题是当你用点导入时:

 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

其中 __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 将特定模块添加到环境中。

原文由 hcnhcn012 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题