Python ImportError:没有名为';util';

Python ImportError:没有名为';util';,python,python-3.x,import,python-import,Python,Python 3.x,Import,Python Import,我知道这个问题有很多变体,但我找不到一个像我这样的。当我尝试导入模块illustris_python时,我得到一个错误ImportError:没有名为“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)]

我知道这个问题有很多变体,但我找不到一个像我这样的。当我尝试导入模块
illustris_python
时,我得到一个错误
ImportError:没有名为“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]: 
Python 3.4.3(v3.4.3:9b73f1c3e601,2015年2月24日,22:44:40)[MSC v.1600 64位(AMD64)]
有关详细信息,请键入“版权”、“信用”或“许可证”。
iPython3.0.0——一种增强的交互式Python。
?         -> 介绍和概述IPython的功能。
%快速参考->快速参考。
帮助->Python自己的帮助系统。
对象?->有关“对象”的详细信息,请使用“对象??”获取更多详细信息。
%guiref->关于图形用户界面的简要参考。
在[1]中:将illustris_python作为il导入
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
将illustris_python作为il导入
文件“C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\illustris\u python\\uuuuuuuu init\uuuuu.py”,第3行,在
从…起进口*
文件“C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\illustris\u python\snapshot.py”,第6行,在
从util导入partTypeNum
ImportError:没有名为“util”的模块
在[2]中:

看一看,我很确定问题在于,这段代码仅适用于Python 2.x。有人已经做了一些工作来为最终的港口清理它,但还有更多的工作要做

这一特定错误是:

在Python2.6中,这是一个相对导入(它被“弃用”了,但我很确定默认情况下不会得到警告…),因此它首先在与
snapshot.py
相同的包中查找,在查找
util.py
之前,它会在其中找到一个
sys.path

在Python3.4中,它是一个绝对导入,因此它只在
sys.path
中查找(它调用顶级模块查找程序,但通常这意味着在
sys.path
中查找),并且没有
util.py

如果您试图自己完成此示例代码到3.x的移植,只需将其更改为显式相对导入:

from .util import partTypeNum

现在我已经解决了你的问题。我所做的是朝着“illustris_python”的方向打开终端。希望能有所帮助。

你的PYTHONPATH上有这个目录吗?@dbliss:His
网站包
真的不应该在他的
PYTHONPATH上。不管怎样,它都会在
sys.path
上结束。可能性1:您是从克隆/解压/任何
illustris\u python
到的目录中运行的吗?如果是这样,它很容易混淆本地副本和已安装副本。@dbliss它不在PYTHONPATH中。可能性2:illustris_python是否支持python 3?@abarnet将所有模块更改为显式相对导入,但是现在我在
\uuuuu initt\uuuuu
module@Surfcast23:你的意思是
\uuuuu init\uuuuu
,而不是
\uu int\uuuuu
,对吗?如果这不是整个项目中唯一的Python2-vs-3错误,我也不会感到惊讶。如果你想自己移植它,你必须试着调试所有你能解决的问题,并为每一个你遇到的问题提出新的问题。
from .util import partTypeNum