Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

Python 3: module in same directory as script: "ImportError: No module named"

I'm trying to import a module (venues) from an IPython shell. The venues module is correctly imported but it then tries itself to import a module named makesoup and fails to do so.

I'm located in the ~ directory and am trying to import the venues.py file located in the subdirectory processors. The makesoup.pyfile is also located in the processors subdirectory, which means any Python script near it should be able to find it, right?

In [1]: import processors.venues
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-765135ed9288> in <module>()
----> 1 import processors.venues

~/processors/venues.py in <module>()
      7 """
      8 
----> 9 import makesoup
     10 import re
     11 

ImportError: No module named 'makesoup'

I have added an empty __init__.py in both the ~ and processors directories, unsuccessfully.

Note: the makesoup module is correctly imported when I'm located in processors but I know this is not the only way it should work.

question from:https://stackoverflow.com/questions/27365273/python-3-module-in-same-directory-as-script-importerror-no-module-named

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The makesoup.py file is also located in the processors subdirectory, which means any Python script near it should be able to find it, right?

No. This feature was changed in Python 3 and that syntax no longer works.

Change the import makesoup to this:

from . import makesoup

Or to this:

from processors import makesoup

Both of these will make it impossible to run python processors/venues.py directly, though you can still do python -m processors.venues from your home directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...