30

I have installed the pyPdf module successfully using the command pip install pydf but when I use the module using the import command I get the following error:

enC:\Anaconda3\lib\site-packages\pyPdf\__init__.py in <module>()
1 from pdf import PdfFileReader, PdfFileWriter
  2 __all__ = ["pdf"]
ImportError: No module named 'pdf'

What should I do? I have installed the pdf module as well but still the error does not go away.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Nitin Vijay
  • 435
  • 1
  • 8
  • 14

8 Answers8

29

This is a problem of an old version of pypdf. The history of pypdf is a bit compliated, but the gist of it:

Use pypdf>=3.1.0. All lowercase, no number. Since December 2022, it's the best supported version.

Install pypdf

$ sudo -H pip install pypdf

You might need to replace pip by pip2 or pip3 if you use Python 2 or Python 3.

Use pypdf

import pypdf

WARNING: PyPDF3 and PyPDF4 are not maintained and PyPDF2 is deprecated - pypdf is the way to go!

Three potential alternatives which are maintained:

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    For the benefit of careless readers: note case: `pyPdf` has only one uppercase letter, while `PyPDF2` has 4. – ShreevatsaR Oct 06 '20 at 04:34
15

I've had the same error popping up after installing pypdf via pip and trying to import it in IPython (I'm using python 3.5.2):

In [5]: import pyPdf
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-a5780a4295f9> in <module>()
----> 1 import pyPdf

/home/mf/virtual_envs/pdfdataextract/lib/python3.5/site-packages/pyPdf/__init__.py in <module>()
----> 1 from pdf import PdfFileReader, PdfFileWriter
      2 __all__ = ["pdf"]

ImportError: No module named 'pdf'

This was even after installing the pdf library using pip.

Luckily, there's a PyPDF2 library which works like a charm for me.

Matthias Fischer
  • 553
  • 4
  • 18
7

Use PyPDF2.
I've been using it in Python 3 (v3.5.2 to be precise), and it works quite well.
Here's a simple command that you can use to install PyPDF2.

sudo -H pip3 install PyPDF2

For using it:

from PyPDF2 import PdfFileReader

Let me know if you need any clarification.

Lakshmikant Deshpande
  • 826
  • 1
  • 12
  • 30
6

Firstly, in your code you wrote:

from pdf import PdfFileReader, PdfFileWriter

Instead of:

from PyPDF2 import PdfFileReader, PdfFileWriter

Secondly use

 pip3.x install pyPdf

instead of pip install pyPdf if it will not work

Dancer PhD
  • 197
  • 1
  • 4
  • 11
0

I use pypdf2 , it work for me. pip install pypdf2. I use Ubuntu 16.04

kherox
  • 1
0

I imported PDF library using python 3.8.5 as;

import PyPDF4

or

from PyPDF4 import PdfFileReader


it runs great.....

0

I had the same problem.
I undeleted the PyPDF2 package from /miniconda3 and re-installed in /home.

moken
  • 3,227
  • 8
  • 13
  • 23
Rin777
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 06 '23 at 13:01
-6

Your import code should read:

from pyPdf import PdfFileReader, PdfFileWriter
James C. Taylor
  • 430
  • 3
  • 8