【问题标题】:ImportError: No module named 'util'ImportError:没有名为“util”的模块
【发布时间】:2015-07-09 13:12:21
【问题描述】:

我知道这个问题有很多变体,但我找不到像我这样的问题。当我尝试导入模块illustris_python 时,我收到错误ImportError: No module named '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]: 

【问题讨论】:

  • 那个目录在你的 PYTHONPATH 上吗?
  • @dbliss:他的site-packages 真的不应该出现在他的PYTHONPATH 上。无论如何,它最终会出现在sys.path。
  • 可能性 1:您是从您克隆/解压缩/illustris_python 进入的目录中运行的吗?如果是这样,它很容易混淆本地副本与已安装的副本。
  • @dbliss 它不在 PYTHONPATH 中。
  • 可能性2:illustris_python是否支持Python 3?

标签: python python-3.x import python-import


【解决方案1】:

查看the BitBucket repo,我很确定问题在于此代码仅适用于 Python 2.x。有人为最终的端口做了一些清理工作,但还有更多工作要做。

这个特定的错误是near the top of snapshot.py:

from util import partTypeNum

在 Python 2.6 中,这是一个 relative 导入(它已被 PEP 328“弃用”,但我很确定默认情况下您实际上并没有收到警告……),所以它首先在与snapshot.py 相同的包中查找util.py,然后再查看sys.path。

在 Python 3.4 中,它是一个 绝对 导入,所以它只在您的 sys.path 中查找(嗯,它调用您的顶级模块查找器,但通常这意味着在您的 sys.path 中查找),那里没有util.py。

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

from .util import partTypeNum

【讨论】:

  • @abarnet 将所有模块更改为显式相对导入,但现在我在 __intit__ 模块中收到错误
  • @Surfcast23:你的意思是__init__,不是_int_,对吧?如果这不是整个项目中唯一的 Python 2-vs.-3 错误,我不会感到惊讶。如果您想自己移植它,您将不得不尝试调试所有可能的问题,并为遇到的每个问题提出新问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2012-12-07
    • 2012-05-23
    • 2019-07-29
    • 2015-07-04
    • 2014-03-15
    • 2017-12-29
    相关资源
    最近更新 更多