Can't import torch in Python script - python

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.

Related

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.

ImportError: No module named 'util'

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.

Problems in pexpect (python3.3)

Running 3.3 python on CentOS 7.
Tryin' to write simple script but can't get pexpect module to work as I want
if I use interpreter python 3.3, I can write this commands correctly
[root#localhost expect]# python3.3
Python 3.3.3 (default, Apr 7 2015, 02:31:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> child = pexpect.spawn('telnet 10.1.1.1')
but If I run file pexpect.py with exactly same commands, I get
[root#localhost expect]# python3.3 /usr/etc/pexpect.py
Traceback (most recent call last):
File "/usr/etc/pexpect.py", line 1, in <module>
import pexpect
File "/usr/etc/pexpect.py", line 3, in <module>
child = pexpect.spawn('telnet 10.1.1.1');
AttributeError: 'module' object has no attribute 'spawn'
I found some similar info in the google, advice was to move .py file to another folder.
It didn't work for me.
Another advice was to delete " pycache" folder (I've got same in my pexpect.py location), but it didn't work aswell. Errors are still the same, this folder are still created after running the script (trying, I mean).
Any ideas?
You have called your file pexpect.py. You need to rename it to something else as you are importing from your file not the pexpect module. You also need to delete any .pyc in the same folder. It does not matter where you move your script, the current folder is still going to be in the path before where the actual pexpect module is.

Unable to import RRDtool in Python

I am trying to import RRDtool into Python as I want to access an RRD database using Python, but when I am trying to import rrdtool I am getting the following error.
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/rrdtool-1.4.5/bin')
>>> import rrdtool
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rrdtool
My RRDtool is located in /opt/rrdtool-1.4.5/bin.
Well, the problem is solved just by executing the following command.
sudo apt-get install python-rrd
You probably need py-rrdtool, which you could get directly from the site or your package manager.
It is unlikely that Python modules are located inside a 'bin' folder. And importing files from some configured path requires that the referred path is a proper Python package. It means it must contain an __init__.py file.
You could use the RRDtool documentation for inspiration (man rrdpyton). Something
along the lines of
import sys
sys.path.append('/path/to/rrdtool/lib/python2.6/site-packages/')
import rrdtool
should do the trick.

Categories