Reputation: 345
whenever I try to import matplotlib or matplotlib.pyplot in VS Code I get the error in the title:
Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource)
or
Import "matplotlib.pyplot" could not be resolved from source Pylance(reportMissingModuleSource)
The hyperlink of the reportMissingModuleSource sends me to https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules, where it says:
"Diagnostics for imports that have no corresponding source file. This happens when a type stub is found, but the module source file was not found, indicating that the code may fail at runtime when using this execution environment. Type checking will be done using the type stub."
However, from the explanation I don't understand exactly what's wrong and what I should do to fix this, can someone help me with this?
Upvotes: 17
Views: 88308
Reputation: 11
VSCode terminal does not activate your venv right after it is created. You might have installed the package to your computer instead of venv you use.
Try close VSCode terminal and repoen. Virtual environment might be active and try to install the package again.
Upvotes: 1
Reputation: 81
I had the same issue. Sometimes it's back to multiple versions of python on your device. You just need to change the path. Make sure the correct Python interpreter is selected in your IDE.
In vscode
Press CTRL + Shift + P
or (⌘ + Shift + P
on macOS) to open the command palette. Then type Python select interpreter
in the search field and choose the right version.
Upvotes: 8
Reputation: 21
I experienced a similar issue even after installing vs_BuildTools.
Matplotlib could not be resolved from source parlance
What to do:
Upvotes: 2
Reputation: 1
I was getting the same problem and realised the package install path for my active virtual env wasn't listed in sys.path after appending this location to sys.path my .ipynb was able to import matplotlib
Upvotes: 0
Reputation: 11
I have the same issue - I did two things and its working now
Check if there is some earlier versions of python installed on your machine - if yes then remove and reinstall the latest one
Second install the Microsoft C++ Build Tools https://visualstudio.microsoft.com/visual-cpp-build-tools/
Restart the app and run again.
Upvotes: 0
Reputation: 21
Just changes the interpreter to 2.7.x in left-bottom corner enter image description here
Upvotes: 2
Reputation: 9481
I can reproduce your question when I select a python interpreter where doesn't exist matplotlib
:
So, the solution is opening an integrated Terminal then run pip install matplotlib
. After it's installed successfully, please reload window, then the warning should go away.
Upvotes: 29