skip to Main Content

This post has two parts:

  1. To help beginners starting up with creating virtual environments and installing packages for Python in Visual studio Code (I am on MACOS however should be able to follow on windows)

  2. A question of trying to Resolve why PyPDF2 package doesn’t seem to install.

VIRTUAL ENVIRONMENTS AND PACKAGE INSTILLATION VIA PIP3

Creating New Virtual Environment (.VIRTUAL_ENV)

  1. change directory to location you want to create virtual environment (cd)
  2. python3 - m venv .VIRTUAL_ENV (or whatever name you wish to call it)

creating Virtual Environemnt

Activating Virtual Environment:

  1. Copy Path of virtual environment
    Copy Path of virtual environment

  2. Activate it (source /bin/activate.)
    Activate it

Check packages in Virtual Environment (Empty as nothing installed)
Check packages in Virtual Environment (Empty as nothing installed)

Selecting Interpreter
Selecting Interpreter

Print hello to make sure all is working okay.

Print hello to make sure all is working okay

Demoing Expected Behaviour using numpy

  1. numpy not yet installed.

demoing numpy not yet installed

  1. pip3 install numpy

pip3 install numpy

And we can see the numpy package(s) are added to the library

same again with pip3 install matplotlib.

pip3 install matplotlib

Alot more packages are being added this time but successfully installs without issue.

Checking both installed okay:

enter image description here

Now for PyPDF2:

Before installing:

Before installing PyPDF2

pip3 install PyPDF2

pip3 install PyPDF2

Package appears to install correctly, yellow squiggle disappears all seems okay.

But when I run the script now:

Module Not found error

and again just to confirm that is the case:

Requirement already satisfied

Requirement already satisfied: PyPDF2 in ./.VIRTUAL_ENV/lib/python3.13/site-packages

final checks:

pip3 show PyPDF2

enter image description here

pip3 list

enter image description here

If anyone can assist on why this is the case it would be appreciated.

I have looked at the following post however the suggestions here either dont work or are for windows:

"no module named PyPDF2" error

2

Answers


  1. This seems like a problem with VSCode not being set to the correct Python interpreter.
    Also, have you tried reactivating the virtual environment?
    Check your current pip list to make sure the package is installed in the correct environment.

    Update: I have created a test script with pretty much the same way as you did. And the code works. Ensure where your Python executive is and run via the command line. See the screenshots below.

    Create a venv and install required packages

    Ensure package availability

    Run the code via correct python executive

    import PyPDF2

    Login or Signup to reply.
  2. Please refer to the same issue on GitHub: No module named ‘PyPDF2’ .

    pypdf2 is deprecated, replaced by pypdf.

    https://github.com/py-pdf/pypdf/issues/456#issuecomment-2071363332

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search