How do I get the commands for PyPDF2 in Python - 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

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.

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.

Why IPython shell can find urllib.parse while plain python shell can't? [duplicate]

I have observed the following behavior in python 3.4.2, and I am unableto explain it. Hopefully someone could shed some light on the matter:
In IPython:
In [129]: import urllib
In [130]: print(urllib.parse)
<module 'urllib.parse' from '/Users/ashwin/.pyenv/versions/3.4.2/lib/python3.4/urllib/parse.py'>
I've imported a module, and printed one of its attributes. Everything works as expected. So far, life is good.
Now, I do the same thing from the command line:
$ python -c 'import urllib; print(urllib.parse)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parse'
Say what?! that's not how that's supposed to work.
Ok, maybe this is a python-wide behavior; maybe modules are not immediately imported when using the -c flag. Let's try another module:
$ python -c 'import datetime; print(datetime.datetime)'
<class 'datetime.datetime'>
What?! How does it work for datetime and not for urllib? I'm using the same version of python in both places (3.4.2)
Does anyone have any thoughts on this?
EDIT:
Per one of the comments:
$ which -a ipython
/Users/ashwin/.pyenv/shims/ipython
/Library/Frameworks/Python.framework/Versions/2.7/bin/ipython
/usr/local/bin/ipython
/usr/local/bin/ipython
And
$ which -a python
/Users/ashwin/.pyenv/shims/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/bin/python
/usr/bin/python
When you run import urllib, it creates the module object of the urllib module (which is actually a package) without importing its submodules (parse, request etc.).
You need the parent module object (urllib) to be in your namespace if you want to access its submodule using attribute access. In addition to that, that submodule must already be loaded (imported). From the documentation:
if package spam has a submodule foo, after importing
spam.foo, spam will have an attribute foo which is bound to the
submodule. [...] The invariant holding is that if you have
sys.modules['spam'] and sys.modules['spam.foo'] (as you would
after the above import), the latter must appear as the foo attribute
of the former.
There is only one instance of each module, thus any changes made to the urllib module object (stored in sys.modules['urllib']) get reflected everywhere.
You don't import urllib.parse, but IPython does. To prove this I'm going to create a startup file:
import urllib
print('Running the startup file: ', end='')
try:
# After importing 'urllib.parse' ANYWHERE,
# 'urllib' will have the 'parse' attribute.
# You could also do "import sys; sys.modules['urllib'].parse"
urllib.parse
except AttributeError:
print("urllib.parse hasn't been imported yet")
else:
print('urllib.parse has already been imported')
print('Exiting the startup file.')
and launch ipython
vaultah#base:~$ ipython
Running urllib/parse.py
Running the startup file: urllib.parse has already been imported
Exiting the startup file.
Python 3.6.0a0 (default:089146b8ccc6, Sep 25 2015, 14:16:56)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
It is the side effect of importing pydoc during the startup of IPython (which ipython is /usr/local/bin/ipython):
/usr/local/bin/ipython, line 7:
from IPython import start_ipython
/usr/local/lib/python3.6/site-packages/IPython/__init__.py, line 47:
from .core.application import Application
/usr/local/lib/python3.6/site-packages/IPython/core/application.py, line 24:
from IPython.core import release, crashhandler
/usr/local/lib/python3.6/site-packages/IPython/core/crashhandler.py, line 28:
from IPython.core import ultratb
/usr/local/lib/python3.6/site-packages/IPython/core/ultratb.py, line 90:
import pydoc
/usr/local/lib/python3.6/pydoc.py, line 68:
import urllib.parse
This explains why the below code fails - you only import urllib and nothing seems to import urllib.parse:
$ python -c 'import urllib; print(urllib.parse)'
On the other hand, the following command works because datetime.datetime is not a module. It's a class that gets imported during import datetime.
$ python -c 'import datetime; print(datetime.datetime)'
urllib.parse is available from Python 3 onwards. I think you might need to import urllib.parse, not import urllib. Not sure if (when) submodule import is implicit.
I would guess IPython imports urllib.parse on startup and that is why it is available.
parse is a module not an attribute:
Python 3.4.2 (default, Oct 15 2014, 22:01:37)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.parse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'parse'
>>> import urllib.parse
>>> urllib.parse
<module 'urllib.parse' from '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/parse.py'>

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.

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