Sjotroll
Sjotroll

Reputation: 345

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

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

Answers (8)

user3461608
user3461608

Reputation: 1

Check this part and change it to correct interpreter.

enter image description here

Upvotes: 0

csuvasregal
csuvasregal

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

karimdvl
karimdvl

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

chidi EOT
chidi EOT

Reputation: 21

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

Upvotes: 2

TGiblin95
TGiblin95

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

SauZ Khan
SauZ Khan

Reputation: 11

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

Restart the app and run again.

Upvotes: 0

Ruben Tanahara
Ruben Tanahara

Reputation: 21

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

Upvotes: 2

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9481

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.

Upvotes: 29

Related Questions