Search code examples
pythonpypdf

ImportError No Module Named 'PyPDF2'


New to Python..., well actually new to programming in general, so pls bear with me. On Ubuntu 20.04 (yes, new to Linux as well) with Python 3.8.2

I'm trying to run a script that uses PyPDF2. I was able to install it just fine with:

sudo apt-get install python3-pypdf2 and I can import it from the command line without any error:

import PyPDF2

When i try to import it from Pycharm, however, it generates a ModuleNotFoundError error:

Traceback (most recent call last):
  File "/home/surista/.config/JetBrains/PyCharm2020.1/scratches/scratch_2.py", line 1, in <module>
    from PyPDF2 import PdfFileReader
ModuleNotFoundError: No module named 'PyPDF2'

Here's the script I'm using.

from PyPDF2 import PdfFileReader

def get_info(path):
    with open(path, 'rb') as f:
        pdf = PDFFileReader(f)
        info = pdf.getDocumentInfo()
        number_of_pages = pdf.getNumPages()

    print(info)

    author = info.author
    creator = info.creator
    producer = info.producer
    subject = info.subject
    title = info.title

if __name__ == '__main__':
    path = '/home/surista/Documents/pdfs/test_eng-1.pdf'
    get_info(path)

Probably missing something obvious here, but any help would be appreciated.


Solution

  • First of all you should install python packages via pip. Run pip install PyPDF2, that might fix it already.

    Also check which interpreter is selected for your project in pycharm. If Pycharm isn't using your system python, it won't see packages installed from a normal shell.

    You'll find it in the Settings -> Project: your_project -> Project Interpreter.