Search code examples
pythonimportpip

I can't seem to import pypdf module


I'm new to python, so sorry for newb question. Im trying to play around with pypdf 3.17.1 but when I try to import the library, or any of its modules, VScode gives me an error squigle under pypdf and informs me:Import "pypdf" could not be resolved.

win 10: python 3.1.2.0 vs-code + Pylance

installed pypdf in VScode terminal via "pip install pypdf" sucessful.

ive also uninstalled/re-installed it using "pip3 install pypdf"

I've never changed any of the install paths, nor used custom paths for any pip installations. I've verified that pypdf exists in the path location denoted by pip. I'm a bit stumped as to why it's not finding pypdf when I attempt to import it in myfile.py

error in VScode

pip show pypdf

Name: pypdf
Version: 3.17.1
Summary: A pure-python PDF library capable of splitting, merging, cropping, and transforming PDF files
Home-page:
Author:
Author-email: Mathieu Fenniak <[email protected]>
License:
Location: C:\Users\project68\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires:
Required-by:

print(sys.path)
['c:\\Users\\project68\\Documents\\maya\\scripts\python\\CS50P','C:\\Users\\project68\\Documents\\maya\\scripts\\python','C:\\Users\\project68\\Documents\\maya\\scripts\\python\\CS50P','C:\\ProgramFiles\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\\python311.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\\Lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\\Lib\\site-packages']

Solution

  • Guess #1: Mixed-up Python environments

    The problem is most likely that you have different Python versions. You install pypdf in a different version than you use.

    In such a case, you can run pip through Python:

    python -m pip install pypdf
    

    Your command could also be python3 or python3.11 or similar. The rest stays the same.

    Guess #2: VS Code not loading freshly installed packages

    VScode gives me an error squigle under pypdf and informs me:Import "pypdf" could not be resolved [after I installed it]

    Try restarting VS code. Give it a few seconds after it started up.