Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Python ModuleNotFoundError in VS Code using Code Runner |
---|
How to solve the problem that Visual Studio Code can't find a module that you've imported. |
So you're using Visual Studio Code to run Python programs using virtual environments, and you see this error when you run a script importing a module:
The module name then appears in the quotation marks.
You are certain that:
You know that you can solve the problem by running this command, but you want a more permanent solution:
Try pressing Ctrl + F5 to run your program. Does this work? If so, maybe the problem is with the Code Runner extension that you're using.
If you usually run your programs by pressing Alt + Ctrl + N, you almost certainly are using Code Runner.
To solve this, press Ctrl + , to load up your settings, and if necessary click on this icon to go into JSON view:
Click on this icon at the top right of your Visual Studio Code window to see your settings as JSON text.
Add this text in:
Add this to the bottom of your file, making sure that you put a comma at the end of the previous setting.
Here's the setting if you want to copy it:
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
Note that one delegate (thanks, Michael) reported that the $pythonPath needed to be included in quotation marks, like this:
However, I didn't need to do this.
If this worked, you've solved your problem! It did for me, although not before I'd tried too many other solutions involving .env files, .path files, launch.json settings, the PythonPath environment variable and other esoteric options. Now what was I doing previously?!
I got this information from this blog, which will tell you more about how and why this solution works.
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.