我知道这个问题有很多变体,但我找不到一个像我这样的。当我尝试导入模块util时,我得到了错误ImportError: No module named 'util'
模块util在需要它的模块snapshot.py
下面的目录中,所以我很困惑为什么python可以看到一个模块,但看不到另一个模块。我已经在下面包含了导入调用和回溯。
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
In [1]: import illustris_python as il
Traceback (most recent call last):
File "<ipython-input-1-ff06d24b4811>", line 1, in <module>
import illustris_python as il
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site- packages\illustris_python\__init__.py", line 3, in <module>
from . import *
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site- packages\illustris_python\snapshot.py", line 6, in <module>
from util import partTypeNum
ImportError: No module named 'util'
In [2]:
发布于 2015-04-30 01:55:55
看看the BitBucket repo,我很确定问题在于这段代码只支持Python2.x。有人已经做了一些工作来清理它,为最终的端口,但仍然有更多的工作要做。
这个特定的错误是near the top of snapshot.py
from util import partTypeNum
在Python2.6中,这是一个相对导入(它被PEP 328“弃用”,但我非常确定在默认的…下你实际上不会收到警告),所以它首先在与snapshot.py
相同的包中查找,在那里它找到一个util.py
,然后再查找您的sys.path
。
在Python3.4中,它是一个绝对的导入,所以它只是在你的sys.path
中查找(嗯,它调用你的顶级模块查找器,但通常这意味着要在你的sys.path
中查找),并且那里没有util.py
。
如果您试图自己完成将此示例代码移植到3.x,只需将其更改为显式的相对导入:
from .util import partTypeNum
发布于 2017-06-22 03:18:26
现在我已经解决了你的问题。我所做的就是在"illustris_python“的方向上打开终端。希望能对你有所帮助。
https://stackoverflow.com/questions/29957477
复制