ImportError: No module named 'util'

Question:

When I try to import the module illustris_python I get the error

ImportError: No module named ‘util’

The module util is in the directory below the module snapshot.py that 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.

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.2python-3.4.3.amd64libsite-packagesillustris_python__init__.py", line 3, in <module>
    from . import *

  File "C:WinPython-64bit-3.4.3.2python-3.4.3.amd64libsite-packagesillustris_pythonsnapshot.py", line 6, in <module>
    from util import partTypeNum

ImportError: No module named 'util'


In [2]: 

Screenshot showing location of util:

screenshot

Asked By: Stripers247

||

Answers:

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.

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

from util import partTypeNum

In Python 2.6, this is a relative import (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.

In Python 3.4, it’s an absolute import, 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.py there.

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

from .util import partTypeNum
Answered By: abarnert

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.

Answered By: user8197454

I fixed this issue. I noticed the library need to have access to a file titled.util.py. I just copied the file from the related github, and pasted in the folder where the library was installed.

Answered By: farshad madani