eugenio b
eugenio b

Reputation: 33

Python3 and pypdf

I have a problem with pypdf and python3: when I do import pypdf or import PyPDF2 python shell returns an error "xrange is not define".

I have seen this is a common problem and I tried the suggested workaround

they does not work.

am I missing something or can anyone suggest me the correct modification?

EDIT - this is the full list of commands I use

[16:45 eugenio@arch-eu ~]
└───> $ pip uninstall pypdf
Found existing installation: pypdf 3.6.0
Uninstalling pypdf-3.6.0:
Would remove:
/home/eugenio/.local/lib/python3.10/site-packages/pypdf-3.6.0.dist-info/* /home/eugenio/.local/lib/python3.10/site-packages/pypdf/*
Proceed (Y/n)?
Successfully uninstalled pypdf-3.6.0

[16:46 eugenio@arch-eu ~]
└───> $ python -m pip install pypdf --upgrade
Defaulting to user installation because normal site-packages is not writeable
Collecting pypdf
Using cached pypdf-3.6.0-py3-none-any.whl (245 kB)
Installing collected packages: pypdf
Successfully installed pypdf-3.6.0

[16:47 eugenio@arch-eu ~]
└───> $ whereis python
python: /usr/bin/python /usr/share/man/man1/python.1.gz

[16:47 eugenio@arch-eu ~]
└───> $ ll /bin/python
python python3.10 python3-config pythontex

python3 python3.10-config python-config

[16:48 eugenio@arch-eu ~]
└───> $ python

Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import pypdf
Traceback (most recent call last):
File "", line 1, in
File "/home/eugenio/.local/lib/python3.10/site-packages/pypdf/init.py", line 10, in
from ._encryption import PasswordType
File "/home/eugenio/.local/lib/python3.10/site-packages/pypdf/_encryption.py", line 60, in
from Crypto.Cipher import AES, ARC4 # type: ignore[import]
File "/home/eugenio/.local/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py", line 119, in
key_size = xrange(1,256+1)
NameError: name 'xrange' is not defined. Did you mean: 'range'?
>>>

Upvotes: 1

Views: 769

Answers (2)

Muhammad Farooq
Muhammad Farooq

Reputation: 150

The reason is you are trying to import pypdf in Python 3 but it was designed for Python 2, which uses xrange to generate a sequence of integers.Python 3 uses range instead of xrange, which works differently and returns a sequence object instead of a list object.

you can use pypdf2 instead of pypdf which is made for python 3

you can install pypdf2 using the following command:

pip install pypdf2

or alternatively you can use python 2 to run your code.

Upvotes: 1

Martin Thoma
Martin Thoma

Reputation: 136197

I'm the maintainer of pypdf and PyPDF2.

Use pypdf. PyPDF2 is deprecated.

You apparently use some very old versions of both libraries. pypdf > 3.0.0 does not use xrange. Try upgrading:

pip install pypdf --upgrade

pypdf > 3.0.0 only supports Python 3.6+

Upvotes: 2

Related Questions