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

一曲冷凌霜 提交于 2019-11-30 16:49:56

问题


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.

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:

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.

And assistance would be gratefully received.


回答1:


Try adding a sys.path.append to the list of your imports.

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



回答2:


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.

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.

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




回答3:


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(..) )




回答4:


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:

x.py

from app.y import say_hi
print ("ok x is here")
say_hi()

And

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:

$ python -m app.x

Note: I did not use x.py (simply app.x)

Result:

Nespresso@adhg MINGW64 ~/Desktop/testpy
$ python -m app.x
Im Y
ok x is here
Y says hi


来源:https://stackoverflow.com/questions/16480898/receiving-import-error-no-module-named-but-has-init-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!