Import "matplotlib" could not be resolved from source Pylance(reportMissingModuleSource)

Question:

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?

Asked By: Sjotroll

||

Answers:

I can reproduce your question when I select a python interpreter where doesn’t exist matplotlib:

enter image description here

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.

Answered By: Molly Wang-MSFT

Just changes the interpreter to 2.7.x in left-bottom corner enter image description here

Answered By: Ruben Tanahara

I have the same issue – I did two things and its working now

Restart the app and run again.

Answered By: SauZ Khan

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

Answered By: TGiblin95

I experienced a similar issue even after installing vs_BuildTools.

Matplotlib could not be resolved from source parlance

What to do:

  1. open the Command Palette (Ctrl+Shift+P)
  2. Type: Python: Select interpreter Here is the interpreter that worked for me!
  3. Navigate to your project.
  4. Select the latest interpreter or check what interpreter
Answered By: chidi EOT

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.

Answered By: karimdvl

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.

Answered By: csuvasregal

Check this part and change it to correct interpreter.

enter image description here

Answered By: user3461608