Python 导入错误:没有名为“util”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29957477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:12:56  来源:igfitidea点击:

ImportError: No module named 'util'

pythonpython-3.ximportpython-import

提问by Stripers247

I know that there are many variations of this question, but I could not find one like mine. When I try to import the module illustris_pythonI get the error ImportError: No module named 'util'The module util is in the directory below the module snapshot.pythat needs it so I am confused as to why python sees one module , but not the other. I have included the import call as well as traceback below.

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

Screen shot showing location of util

显示 util 位置的屏幕截图

采纳答案by abarnert

Looking at the BitBucket repo, I'm pretty sure the problem is that this code is Python 2.x-only. Someone's done some work to clean it up for an eventual port, but there's still more to be done.

查看BitBucket 存储库,我很确定问题在于此代码仅适用于 Python 2.x。有人已经做了一些工作来清理它以供最终端口使用,但还有更多工作要做。

This particular error is near the top of snapshot.py:

此特定错误位于以下顶部附近snapshot.py

from util import partTypeNum

In Python 2.6, this is a relativeimport (it's "deprecated" by PEP 328, but I'm pretty sure you don't actually get the warning by default…), so it first looks in the same package as snapshot.py, where it finds a util.py, before looking through your sys.path.

在 Python 2.6 中,这是一个相对导入(它被PEP 328“弃用” ,但我很确定你实际上并没有在默认情况下收到警告......),所以它首先在与 相同的包snapshot.py中查找,在那里找到a util.py、在浏览您的sys.path.

In Python 3.4, it's an absoluteimport, so it just looks in your sys.path(well, it calls your top-level module finders, but usually that means looking in your sys.path), and there is no util.pythere.

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

If you're trying to finish porting this sample code to 3.x yourself, just change it to an explicit relative import:

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

from .util import partTypeNum

回答by user8197454

Right now I have solve the problem you have. What I have done is open the terminal in the direction of "illustris_python". Hope it could be helpful.

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