Python 接收导入错误:没有名为 *** 的模块,但有 __init__.py
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16480898/
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
Receiving Import Error: No Module named ***, but has __init__.py
提问by Mark Rowlands
I understand that this question has been asked several times but after reading them, and making the suggested fixes, I'm still stumped.
我知道这个问题已经被问过好几次了,但是在阅读了它们并进行了建议的修复之后,我仍然被难住了。
My project structure is as follows:
我的项目结构如下:
Project
|
src
|
root - has __init__.py
|
nested - has __init__.py
|
tests - has __init__.py
|
utilities - has __init__.py
|
services - has __init__.py
I've successfully run a unittest regression class from Eclipse without any issues.
我已经成功地从 Eclipse 运行了一个单元测试回归类,没有任何问题。
As soon as I attempted to run the same class from the command-line (as other users who will be running the suite do not have access to Eclipse) I receive the error:
一旦我尝试从命令行运行相同的类(因为将运行该套件的其他用户无权访问 Eclipse),我收到错误消息:
ImportError: No module named 'root'
As you can see from above, the module root has an __init__.pyAll __init__.pymodules are completely empty.
从上面可以看出,模块根目录有一个__init__.py所有__init__.py模块都是完全空的。
And assistance would be gratefully received.
将不胜感激地收到帮助。
采纳答案by Gus E
Try adding a sys.path.appendto the list of your imports.
尝试将 a 添加sys.path.append到您的导入列表中。
import sys
sys.path.append("/Project/src/")
import root
import root.nested.tests
回答by Mark Rowlands
Just a note for anyone who arrives at this issue, using what Gus E showed in the accept answer and some further experience I've found the following to be very useful to ensure that I can run my programs from the command-line on my machine or on another colleague's should the need arise.
对于遇到此问题的任何人,请使用 Gus E 在接受答案中显示的内容和一些进一步的经验,我发现以下内容非常有用,可确保我可以在我的机器上从命令行运行我的程序或者在需要时在另一位同事那里。
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
When I execute the 'main' method, which is located in the 'nested' directory, it ensures that the 'src' directory is added to the PYTHONPATH at the time of execution meaning all following imports will not throw an error.
当我执行位于 'nested' 目录中的 'main' 方法时,它确保在执行时将 'src' 目录添加到 PYTHONPATH,这意味着所有后续导入都不会抛出错误。
Obviously, you need to adjust the number of ".." arguments to the os.path.join()method as determined by the location in your program of where your main method is executed from
显然,您需要调整os.path.join()方法的“..”参数的数量,这取决于您的程序中执行 main 方法的位置
回答by Yuval Atzmon
If anybody lands here:
如果有人在这里登陆:
I encountered this error as well. In my case, I used ~/my/path/at the path.sys.append(...), and the fix was replacing ~with the explicit path name (you can inquire it if you type pwdwhen you are on linux shell, or use os.path.expanduser(..))
我也遇到了这个错误。在我的例子中,我使用~/my/path/了path.sys.append(...),并且修复是用~显式路径名替换(如果你pwd在 linux shell 上输入,或者使用 ,你可以查询它os.path.expanduser(..))
回答by adhg
Yet another way to solve this without the path goes like this: consider the following code where inside your folder name 'app' you have 3 files x.py, y.py and an empty init.py. So to run x.py you have an import from y such that:
另一种没有路径的解决方法是这样的:考虑下面的代码,在你的文件夹名称“app”中,你有 3 个文件 x.py、y.py 和一个空的init.py。因此,要运行 x.py,您需要从 y 导入:
x.py
x.py
from app.y import say_hi
print ("ok x is here")
say_hi()
And
和
y.py
y.py
print ("Im Y")
def say_hi():
print ("Y says hi")
so the folder structure would look like this:
所以文件夹结构看起来像这样:
testpy
app
__init__.py
x.py
y.py
Solution: in the folder BEFOREapp do the following:
解决方案:在应用程序之前的文件夹中执行以下操作:
$ python -m app.x
Note: I did not use x.py (simply app.x)
注意:我没有使用 x.py(只是 app.x)
Result:
结果:
Nespresso@adhg MINGW64 ~/Desktop/testpy
$ python -m app.x
Im Y
ok x is here
Y says hi

