3

Have installed Python 3.7.6 and am trying to write a code in Visual Studio Code

Using: import pikepdf

Gets me the error of ModuleNotFoundError: No module named 'pikepdf'

However, I run "pip install pikepdf" and I get:

Requirement already satisfied: pikepdf in c:\users\ME\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (1.8.2) Requirement already satisfied: lxml>=4.0 in c:\users\ME\appdata\local\packages\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (from pikepdf) (4.4.2)

My installation path for Python is: C:\Users\ME\AppData\Local\Programs\Python\Python38

And trying to change something in the "Python: Python Path" gets me more errors.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Mario Garcia
  • 189
  • 1
  • 2
  • 10

3 Answers3

8

The issue was that Visual Studio Code comes with its own version of Python, while I had installed my own.

The issue was solved by changing the version VSC was running so it matched the one where the modules were being installed.

enter image description here

Community
  • 1
  • 1
Mario Garcia
  • 189
  • 1
  • 2
  • 10
4

You can check which environment your VSCode is using. You can do that by inspecting your left-bottom corner, here:

Visual Studio Code image.

When you are running your code, you are using python or python3.7?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Dawid Gacek
  • 544
  • 3
  • 19
  • 1
    It says Python 3.8.0 64-bit (and nothing else), though it's weird because when I use CMD and ask python version, it returns 3.7.6 – Mario Garcia Jan 05 '20 at 09:57
0

This problem can also occur when debugging a Python script from Visual Studio Code. In case you want to import a local python file the debugger can't find it. A solution is to load the module by path:

path = os.path.abspath(os.path.join(pathlib.Path(__file__).parent.absolute(), '..', 'subdir', 'myFile.py'))
loader = importlib.machinery.SourceFileLoader('myFile', path)
spec = importlib.util.spec_from_loader('myFile', loader)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

# now use the module:
module.myMethod()
myClassInstance = module.myClass()    

Now debugging works fine, with correctly loaded my module.

FrankyHollywood
  • 1,497
  • 19
  • 18
  • While I found this works, this answer is better: https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code – FrankyHollywood Sep 07 '22 at 15:23