简体   繁体   English

接收导入错误:没有名为***的模块,但有__init__.py

[英]Receiving Import Error: No Module named ***, but has __init__.py

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__.py All __init__.py modules are completely empty. 从上面可以看出,模块根目录有__init__.py所有__init__.py模块都是空的。

And assistance would be gratefully received. 我们将非常感激地提供援助。

Try adding a sys.path.append to the list of your imports. 尝试将sys.path.append添加到导入列表中。

import sys
sys.path.append("/Project/src/")
import root
import root.nested.tests

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. 当我执行位于'嵌套'目录中的'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()方法的“..”参数的数量,这是由程序中执行主方法的位置决定的。

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 pwd when you are on linux shell, or use os.path.expanduser(..) ) 在我的例子中,我在path.sys.append(...)使用~/my/path/ ,修复程序用显式路径名替换~ (如果你在linux上输入pwd ,可以查询它) shell,或者使用os.path.expanduser(..)

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. 另一种在没有路径的情况下解决这个问题的方法如下:考虑以下代码,在文件夹名称'app'中你有3个文件x.py,y.py和一个空的init .py。 So to run x.py you have an import from y such that: 因此,要运行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 BEFORE app do the following: 解决方案:在应用程序BEFORE文件夹中执行以下操作:

$ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM