ImportError: No module named 'util'
Asked Answered
N

3

9

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.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]: 

Screenshot showing location of util:

screenshot

Nashom answered 30/4, 2015 at 1:24 Comment(9)
is that directory on your PYTHONPATH?Uremia
@dbliss: His site-packages really shouldn't be on his PYTHONPATH. It'll end up on sys.path anyway.Hyams
Possibility 1: Are you running from inside the directory you cloned/unzipped/whatever illustris_python into? If so, it can easily get confused about the local copies vs. the installed copies.Hyams
@dbliss it is not in the PYTHONPATH.Nashom
Possibility 2: Does illustris_python support Python 3?Hyams
Meanwhile… how did you install this? The only thing I can find named illustris_python is a repo of sample files on BitBucket that doesn't include a setup.py or any manual installation instructions. If you had to guess how to install it because they didn't tell you, I'd be surprising if you guessed everything 100% right; I'm sure I wouldn't get it right without some debugging. :)Hyams
@Hyams I am pretty sure that it is not being ran from inside the directory. I am using the Winpythin version of IPython which I open through the start menu.Nashom
@Hyams hmm, that might be it. I had to rewrite a few lines that were written in 2.xNashom
OK, that's exactly the problem; it's a Python 2-vs.-3 bug. Let me write an answer.Hyams
H
4

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
Hyams answered 30/4, 2015 at 1:55 Comment(2)
@abarnet changed all of the modules to an explicit relative import, but now I get the error in the __intit__ moduleNashom
@Surfcast23: You mean __init__, not _int_, right? I wouldn't be surprised if this isn't the only Python 2-vs.-3 bug in the whole project. If you want to port it yourself, you're going to have to try to debug all the problems you can, and open new questions for each one where you get stuck.Hyams
B
0

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.

Belittle answered 22/6, 2017 at 3:18 Comment(0)
B
0

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.

Brevity answered 23/1, 2023 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.