This post has two parts:
-
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)
-
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)
- change directory to location you want to create virtual environment (cd)
python3 - m venv .VIRTUAL_ENV
(or whatever name you wish to call it)
Activating Virtual Environment:
Check packages in Virtual Environment (Empty as nothing installed)
Print hello to make sure all is working okay.
Demoing Expected Behaviour using numpy
- numpy not yet installed.
pip3 install numpy
And we can see the numpy package(s) are added to the library
same again with pip3 install matplotlib
.
Alot more packages are being added this time but successfully installs without issue.
Checking both installed okay:
Now for PyPDF2:
Before installing:
pip3 install PyPDF2
Package appears to install correctly, yellow squiggle disappears all seems okay.
But when I run the script now:
and again just to confirm that is the case:
Requirement already satisfied: PyPDF2 in ./.VIRTUAL_ENV/lib/python3.13/site-packages
final checks:
pip3 show PyPDF2
pip3 list
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:
2
Answers
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.
Please refer to the same issue on GitHub: No module named ‘PyPDF2’ .
https://github.com/py-pdf/pypdf/issues/456#issuecomment-2071363332