1

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

  • redefine xrange --> xrange = range
  • add try-except
  • install pypdf or pypdf2

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'?
>>>

eugenio b
  • 19
  • 3
  • I had the exact same issue on a mac m1. As you can see in your error log, there's a message about "Crypto/Cipher/ARC4.py" which is an issue with the package `pycrypto`. So try to do: `pip uninstall pycrypto` – antogerva May 24 '23 at 17:19

2 Answers2

2

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+

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Hi, I have the pypdf updated (I ran the command you suggest). I use archlinux version 6.1.12-arch1-1, python version 3.10.9 and pypdf version 3.6.0 – eugenio b Mar 21 '23 at 23:13
  • Great so then the issue must be gone. If you still see an error message with xrange, you still have the old version. You might have installed the new version in another environment – Martin Thoma Mar 22 '23 at 08:14
  • Sorry, i meant i had already the version updated and i still get the same error – eugenio b Mar 22 '23 at 09:54
  • You are definitely not executing `pypdf==3.6.0`. You might have installed `pypdf==3.6.0`, but the issue very clearly shows that you're not using it. Very likely you are executing a different environment than you're using. – Martin Thoma Mar 22 '23 at 15:31
  • You can try `python -m pip install pypdf --upgrade`. Using `python -m pip` (or python3; you didn't share how you invoke python) ensures that pip is using the python environment you think it should use. – Martin Thoma Mar 22 '23 at 15:33
  • I checked again use the python version and the python package listed. when i run the command you suggest, it returns " requirement already satisfied". I do the following instruction. run python from shell, digit `import pypdf` it returns the error "name 'xrange' is not defined" as mentioned in the question – eugenio b Mar 22 '23 at 22:37
  • You're doing something wrong. Please share the complete traceback, otherwise nobody will be able to help you. – Martin Thoma Mar 22 '23 at 22:40
  • i have previously unistalled pypdf. - python -m pip install pypdf --upgrade Defaulting to user installation because normal site-packages is not writeable Collecting pypdf Installing collected packages: pypdf Successfully installed pypdf-3.6.0 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 NameError: name 'xrange' is not defined. Did you mean: 'range'? – eugenio b Mar 23 '23 at 23:48
1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • i tried using both pypdf and pypdf2 and they both gave me the same error. In addition, I have to use python3 – eugenio b Mar 19 '23 at 10:51
  • I'm the maintainer of pypdf and PyPDF2. Your answer is wrong. The question asker just needs to upgrade. Pypdf does only support Python 3 by now – Martin Thoma Mar 19 '23 at 23:27