ModuleNotFoundError:__main__不是一个包,这是什么意思?[重复]
- 2025-04-15 09:19:00
- admin 原创
- 26
问题描述:
我正在尝试从控制台运行一个模块。我的目录结构如下:
我正在尝试使用以下命令p_03_using_bisection_search.py
从problem_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
文件,但仍然不起作用。
解决方案 1:
只需删除相对导入的点并执行以下操作:
from p_02_paying_debt_off_in_a_year import compute_balance_after
解决方案 2:
我和你遇到了同样的问题。我认为问题在于你使用了相对路径导入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
将特定模块添加到环境中。
解决方案 3:
尝试以如下方式运行它:
python3 -m p_03_using_bisection_search
解决方案 4:
只需使用.py 文件所在的主文件夹的名称。
from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after
解决方案 5:
删除点并在文件开头导入 absolute_import
from __future__ import absolute_import
from p_02_paying_debt_off_in_a_year import compute_balance_after
解决方案 6:
如果您已经创建了目录和子目录,请按照以下步骤操作,并请记住所有目录都必须 __init__.py
被识别为目录。
在脚本中,包含
import sys
和sys.path
,您将能够看到 Python 可用的所有路径。您必须能够看到当前的工作目录。现在导入您想要使用的子目录和相应的模块:
import subdir.subdir.modulename as abc
现在您可以使用该模块中的方法。
举个例子,你可以在这个截图中看到我有一个父目录和两个子目录,在第二个子目录下我有模块CommonFunction
。在右侧我的控制台显示执行后sys.path
,我可以看到我的工作目录。
扫码咨询,免费领取项目管理大礼包!