ImportError: No module named 'util' - python

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:

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

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.

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.

Related

Can't import torch in Python script

So I wanted to try out torch today, but I have the somewhat confusing problem that I can only use it in a Python console but not when I run a script that contains the same code.
In the command prompt it works just fine:
C:\Users\USER>python
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
1.10.1+cu113
>>>
But when I run the exact same code from a .py file, it looks like this:
PS [USER]#D:\Benutzer\USER\Desktop\torch>python .\torch.py
Traceback (most recent call last):
File ".\torch.py", line 1, in <module>
import torch
File "D:\Benutzer\Julius\Desktop\torch\torch.py", line 5, in <module>
print(torch.__version__)
AttributeError: partially initialized module 'torch' has no attribute '__version__' (most likely due to a circular import)
I actually tried to install torch via Anaconda before, which produced the exact same error, which is why I now tried to use it with plain Python, where I installed torch via PIP. (I also had to downgrade Python from 3.10.1 to 3.8.10 because PIP would'nt find torch)
When I run it in the VSCode debugger, it also looks like the import doesn't work correctly, because it shows this:
I suspect there is some kind of circular import problem happening, but I don't understand why. The code is the exact same as in the console. There is only a single import statement in the file: import torch
So, while writing this question and sanity checking myself it suddenly hit me:
I named the file torch.py.
So it tried to import itself. (I'm not terribly familiar with Python imports, I didn't think it would look for local files)
TL;DR: DON'T call your script file name exactly as a lib import.

How do I get the commands for PyPDF2 in Python

If I enter from the command line...
python -m pip
I get a list of the pip commands.
If however I enter
python -m pypdf2
I get "...no modules named pypdf2...".
From the shell, if I enter
>>>help("pypdf2")
I get "No documentation found for pypdf2".
If I get a list of the modules with help("modules"), pypdf2 is the first module listed, not in alphabetical order like the rest of the modules.
I can download the documentation on line, but I am curious does this just mean that there is no documentation for pypdf2 in python?
How did you install it? Assuming you've done python -m pip install pypdf2, you should note that the actual package name is PyPDF2. So even though the install works lower/mixed case, you have to use the actual name to import it or access its help docs: PyPDF2 vs pypdf2:
>>> import pypdf2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pypdf2'
>>> import PyPDF2
>>> help(PyPDF2)
Help on package PyPDF2:
...
>>> help("pypdf2")
No Python documentation found for 'pypdf2'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
>>> help("PyPDF2")
Help on package PyPDF2:
...
PS. I didn't know that help() also accepts the object's name as strings rather than the object itself. Was going to point that out as an error, but tried it myself and saw that it works. Thanks!
PS2. So that allows providing the object name (as a string) to read the help docs without needing to import it first. Nice.
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # no import here
>>> help(PyPDF2) # won't work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'PyPDF2' is not defined
>>> help("PyPDF2") # works, still not imported
Help on package PyPDF2:
```
The name you need to import is PyPDF2. Not pypdf2, not pyPdf2 or anything else.
Capitalization does matter for the import. It doesn't for the install.
However, PyPDF2 does not have any command line utility, so this is expected:
$ python -m PyPDF2
/home/martin/.pyenv/versions/3.8.9/bin/python: No module named PyPDF2.__main__; 'PyPDF2' is a package and cannot be directly executed

Importing python module using bytecode with out source

From the python book:
Learning Python. 5th edition, page #727
I read the following:
if Python finds only a byte code file on the search path and no
source, it simply loads the byte code directly; this means you can
ship a program as just byte code files and avoid sending source
But when attempting the same on Python 3.5, it doesn't work:
~/Python/Module_Test$ cat a.py
a = "abc"
l = [1,2,3]
importing module 'a' created the byte-code file as:
~/Python/Module_Test/__pycache__$ ls
a.cpython-35.pyc
Now I removed the 'a.py' file and from the byte-code directory, I'm importing the module 'a':
~/Python/Module_Test/__pycache__$ python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'a'
I even tried to add the byte-code directory to the search path, still it fails to load the module:
>>> import sys
>>> sys.path.append('/home/pradeep/Python/Module_Test/__pycache__')
>>> import a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'a'
What I am doing wrong? Can we import module from the byte code without the source? Is my understanding of the statement of the book wrong?
Your understanding is not wrong; you can but, it isn't the best idea to do this. Afaik the default behavior of the import statement doesn't do this on its own, you'll either need to use a deprecated function from imp, write your own, or customize the import process to do it.
With imp, you'd use load_compiled as so:
from imp import load_compiled
mod = load_compiled('a', '__pycache__/a.cpython-35.pyc')
To get your module imported. The notable thing that I'm aware that Python does, is that it doesn't re-compile a module a.py if it's corresponding *.pyc is around and is still valid.
After you remove 'a.py', you need to put 'a.pyc' in its place. The import mechanism will see it (when it interprets 'import a'), and will import it successfully. It does this without referring to the cache.
To get 'a.pyc', look in the pycache and copy-to-folder/rename 'a.xxxx.pyc' to 'a.pyc' There is a python function to compile to bytecodes too, so you don't have to work with the pycache
So long as your installation of python does not change, this should work. I believe the format of '.pyc' files can differ between installations. I believe this is a way to ship bytecodes instead of source. However, you must preserve the directory structure and the placement of the 'pyc' files in the same directories as the corresponding 'py' files. Of course the target machine must have an appropriate installation of python.

No module name _tkinter windows python 2.7

I have been searching for hours reading over various suggestions for this topic so please do not downvote me too fast. The closest thing i could find to a solution is here: Tkinter: "Python may not be configured for Tk".
But the trouble is that they do not really explain on there how to get it for windows. Or exactly what part of the setup.py file to modify. Or which setup to modify, I have two setups in different locations. I am looking for some solid advice without harsh comments if I doing something ignorant. Please help... gently lol.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\csvn\Python25\lib\lib-tk\Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
Edit for Johnathan:
>>> import sys
>>> print(sys.executable)
C:\csvn\Python25\python.exe
>>> print(sys.version)
2.7.1 (r271:86832, Jan 5 2016, 01:37:08) [MSC v.1600 64 bit (AMD64)]
>>> print(sys.path)
['', 'C:\\csvn\\Python25\\lib\\site-packages\\setuptools-18.0-py2.7.egg', 'C:\\c
svn\\Python25\\python27.zip', 'C:\\csvn\\Python25\\DLLs', 'C:\\csvn\\Python25\\l
ib', 'C:\\csvn\\Python25\\lib\\plat-win', 'C:\\csvn\\Python25\\lib\\lib-tk', 'C:
\\csvn\\Python25', 'C:\\csvn\\Python25\\lib\\site-packages', 'C:\\csvn\\Python25
\\lib\\site-packages\\win32', 'C:\\csvn\\Python25\\lib\\site-packages\\win32\\li
b', 'C:\\csvn\\Python25\\lib\\site-packages\\Pythonwin']
>>>
Your title says python2.7 but your error message says python2.5 ... I'm guessing you are accidentally running the wrong python installation.

"from _json import..." - python

I am inspecting the JSON module of python 3.1, and am currently in /Lib/json/scanner.py. At the top of the file is the following line:
from _json import make_scanner as c_make_scanner
There are five .py files in the module's directory: __init__ (two leading and trailing underscores, it's formatting as bold), decoder, encoder, scanner and tool. There is no file called "json".
My question is: when doing the import, where exactly is "make_scanner" coming from?
Yes, I am very new to Python!
It's coming from a C-compiled _json.pyd (or _json.so, etc, etc, depending on the platform) that lives elsewhere on the sys.path. You can always find out where that is in your specific Python installation by importing the module yourself and looking at its __file__, e.g.:
>>> import _json
>>> _json.__file__
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/_json.so'
As you see, in my installation of Python 2.6, _json comes from the lib-dynload subdirectory of lib/python2.6, and the extension used on this platform is .so.
It may be coming from a file, or it may be built-in. On Windows, it appears to be built-in.
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import _json
>>> _json.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
and there is no _json.pyd or _json.dll in the offing.
If you want to see the source, having a binary file on your machine or not is irrelevant -- you'll need the SVN browser.

Categories