560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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
|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:
"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.
25 Aytoun Street