250

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

enter image description here

我正在尝试使用以下目录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文件,但它仍然无法正常工作。

4

6 回答 6

289

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

from p_02_paying_debt_off_in_a_year import compute_balance_after
于 2017-01-23T22:44:44.997 回答
107

我和你有同样的问题。我认为问题在于您在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将特定模块添加到环境中。

于 2017-09-29T17:47:05.257 回答
12

尝试将其运行为:

python3 -m p_03_using_bisection_search

于 2018-07-18T07:39:47.287 回答
4

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

from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after
于 2020-05-03T08:26:21.627 回答
4

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

from __future__ import absolute_import

from p_02_paying_debt_off_in_a_year import compute_balance_after
于 2019-09-05T16:35:52.607 回答
2

如果您已创建目录和子目录,请按照以下步骤操作,请记住所有目录都必须 __init__.py使其被识别为目录。

  1. 在您的脚本中,包含import syssys.path,您将能够看到 Python 可用的所有路径。您必须能够看到您当前的工作目录。

  2. 现在导入要使用的子目录和相应模块:import subdir.subdir.modulename as abc现在您可以使用该模块中的方法。

在此处输入图像描述

例如,您可以在此屏幕截图中看到我有一个父目录和两个子目录,在第二个子目录下我有模块CommonFunction。在右侧我的控制台显示执行后sys.path,我可以看到我的工作目录。

于 2017-10-21T08:53:32.840 回答