"no module named PyPDF2" error

Question:

I use Spyder, with Python 2.7, on a windows 10. I was able to install the PyPDF2 package with a conda command from my prompt. I said installation complete. Yet, If I try to run a simple import command:

import PyPDF2

I get the error:

ImportError: No module named PyPDF2

How can I fix this?

Asked By: ALisboa

||

Answers:

I had this problem too when I tried to import PyPDF2 like this:

sudo apt-get install python-pypdf2

When running some simple script with import PyPDF2, I would get an error like this:

ImportError: No module named PyPDF2

The solution was to also install pdfmerge, like this:

pip install pdfmerge

Answered By: Jason

If you use python3 maybe

apt-get install python3-pypdf2
Answered By: Junior Usca

In my case, I was trying to import ‘pyPdf2’ instead of ‘PyPDF2’. Observe the case.

import PyPDF2

is correct.

Answered By: Ashutosh Chamoli

This is the case which I followed for python3. For python2 try with pip:

pip install PyPDF2
Answered By: heytherebrowncow

I faced the same problem. But, In my case,

  • I previously installed Python3 separately from official website and was using without any issues

  • Then later I installed Anaconda package distribution software which itself has another Python3 installed in corresponding directory.

So, when I installed PyPDF2, it installed normally and while importing throws an error, because the base path of python3 was changed to be used with Anaconda.

Then I opened Anaconda prompt and installed PyPDF2 there and tried to import. It worked!!

Then I can use it from any command prompt in my Windows PC. Or else you can delete Anaconda and everything works normally. Its just a conflict of two pythons in my pc.

Conclusion: Try any overlapping softwares in your PC(in my case Anaconda prompt) and try their CMD to install packages and import. If I wanted to install any package I have to go to Anaconda prompt and install it and importing that modules works anywhere without any error. So from now on wards I’m only using Anaconda prompt as my default installation prompt.

Answered By: darla_sud

How to install Python packages on Windows, Mac, and Linux for various versions of Python which are simultaneously installed:

I have multiple versions of Python installed on my Windows 8.1 machine (Python 2.7, 3.5, and 3.7). This created problems (confusion, I should say). You must therefore be very explicit when installing packages. Ex:

py -3.7 -m pip install PyPDF2   # on Windows
python3.7 -m pip install PyPDF2 # on Mac and Linux

INSTEAD OF the more generic:

pip install PyPDF2 or
pip3 install PyPDF2

And to upgrade pip, be very specific in your python version, like this:

py -3.7 -m pip install --upgrade pip   # on Windows
python3.7 -m pip install --upgrade pip # on Mac and Linux

INSTEAD OF the more generic:

py -3 -m pip install --upgrade pip   # on Windows
python3 -m pip install --upgrade pip # on Mac and Linux

Now, I can run python 3.7 with py -3.7 on Windows, or with python3.7 on Linux, and since I did py -3.7 -m pip install PyPDF2 on Windows, or python3.7 -m pip install PyPDF2 on Linux or Mac, the import PyPDF2 command works! Previously, since I had only done pip3 install PyPDF2, the import PyPDF2 command only worked if I ran py -3.5 on Windows or python3.5 on Linux, oddly enough, since apparently that was my "default Python3 version" which the more generic pip3 install PyPDF2 command must have installed the PyPDF2 module into. I think it has something to do with the fact that I installed Python 3.5 for all users, but Python 3.7 for only my user account, so the different pip install commands were placing the installed packages into different locations, with the 3.5 version being the "default" Python3 install location.


See more here: https://docs.python.org/3/installing/index.html:

… work with multiple versions of Python installed in parallel?

On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

Appropriately versioned pip commands may also be available.

On Windows, use the py Python launcher in combination with the -m switch:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4
Answered By: Gabriel Staples

I had the same issue and fixed it when switching Python compiler (bottom left corner on Visual Studio Code) . Try on different versions and eventually it should work.

Answered By: Gus00

When using pip, it usually gets installed in Python 2+ so try

pip3 install PyPDF2
Answered By: IsMaTh IM

Im following a UDEMY course here. Im using Anaconda prompt and jupyter notebook.

I encountered the same issue as OP. What I did to have the library working:

  1. restart the environment
    • go to your anaconda prompt
    • control c to stop the running instance
    • conda activate ***your_env_here***
  2. pip install PyPDF2
  3. (in my case open the jupyer notebook) jupyter notebook
  4. You can now import the library without the error. import PyPDF2

Hope this works for you.

Answered By: Gel

I encountered the same issue today while doing Udemy course.
try the following:

  1. type this
    import sys
    !{sys.executable} -m pip install PyPDF2
  2. then
    import PyPDF2

Hope it works for you too.

Answered By: user18348997