Python ModuleNotFoundError:__main__ 不是包是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41816973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ModuleNotFoundError: What does it mean __main__ is not a package?
提问by lmiguelvargasf
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:
我正在尝试p_03_using_bisection_search.py
从problem_set_02
目录中运行模块,使用:
$ python3 p_03_using_bisection_search.py
The code inside p_03_using_bisection_search.py
is:
里面的代码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()
I am importing a function that is in p_02_paying_debt_off_in_a_year.py
which code is:
我正在导入一个函数,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()
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.
我不知道如何解决这个问题。我已经尝试添加一个__init__.py
文件,但它仍然无法正常工作。
回答by Moses Koledoye
Simply remove the dot for the relative import and do:
只需删除相对导入的点并执行:
from p_02_paying_debt_off_in_a_year import compute_balance_after
回答by hcnhcn012
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.
我和你有同样的问题。我认为问题在于您在in-package import
. __init__.py
您的目录中没有。因此,只需按照摩西上面的回答进行导入。
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
.
where__main__
指的是您当前的模块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:
当解释器进入时p_03.py
,脚本等于:
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
.
并且p_03_using_bisection_search
不包含任何称为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 requestsdo in relative import):
所以我想出了一个更干净的解决方案,而无需更改 python 环境的值(在查看请求在相对导入中的执行方式之后):
The main architecture of the directory is:
该目录的主要架构是:
main.py
main.py
setup.py
setup.py
---problem_set_02/
---problem_set_02/
------__init__.py
------__init__.py
------p01.py
------p01.py
------p02.py
------p02.py
------p03.py
------p03.py
Then write in __init__.py
:
然后写入__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
.
这里__main__
是__init__
,它究竟指的是模块problem_set_02
。
Then go to main.py
:
然后去main.py
:
import problem_set_02
import problem_set_02
You can also write a setup.py
to add specific module to the environment.
您还可以编写一个setup.py
将特定模块添加到环境中。
回答by Dan Keder
Try to run it as:
尝试将其运行为:
python3 -m p_03_using_bisection_search
python3 -m p_03_using_bisection_search
回答by Gaurav Singh
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.
您好,请按照以下步骤操作,您将解决此问题。如果您已创建目录和子目录,请按照以下步骤操作,请记住所有目录都必须 __init__.py
将其识别为目录。
import sys
and runsys.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.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
import sys
并运行sys.path
,您将能够看到python正在搜索的所有路径。您必须能够看到您当前的工作目录。现在使用 import 导入子目录和要使用的相应模块,请遵循以下命令:
import subdir.subdir.modulename as abc
现在您可以使用该模块中的方法。 相同问题的屏幕截图
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
正如你在这个截图中看到的,我有一个父目录和两个子目录,在第二个子目录下我有 module==CommonFunction,你在执行 sys.path 后看到右侧我可以看到我的工作目录
回答by Aminah Nuraini
Remove the dot and import absolute_import in the beginning of your file
删除点并在文件开头导入 absolute_import
from __future__ import absolute_import
from p_02_paying_debt_off_in_a_year import compute_balance_after
回答by FanBek
Just use the name of the main folder which the .py file is in.
只需使用 .py 文件所在的主文件夹的名称。
from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after