简体   繁体   中英

Unable to import 'matplotlib.pyplot'

I know this question is asked a lot, I searched for the last three hours for an answer, but couldn't solve my problem.

As soon as I try to:

import matplotlib.pyplot as plt

my Visual Studio Code IDE tells me that: Unable to import 'matplotlib.pyplot'

My current version of Python is:

Python 3.7.4 (default, Aug 13 2019, 15:17:50) [Clang 4.0.1 (tags/RELEASE_401/final)]:: Anaconda, Inc. on darwin

as you can see, I used the Anaconda package, hence matplotlib should be included. I am Using Mac OS 10.15.2

If I enter

import matplotlib.pyplot as plt

it does import that, however, as soon as I try a basic example, like

x = [1,2,3] 
y = [2,4,1] 
plt.plot(x, y) 

I obtain:

[<matplotlib.lines.Line2D object at 0x10c23d950>]

And a blank token in my Dashboard pops up, which I can only force to quit.

In the course of trying to solve this problem, I tried anything I saw that was related to this topic, although I did not understand everything of it. I hope I did not make a real damage. My last step was completely deinstalling anaconda and reinstalling.

Thanks for taking your time!

The module can be imported, but your IDE says module not found , means your linter (vscode uses pylinter) is not configured correctly.

Start PyLint from correct anaconda environment in Visual Studio Code

First you need to install package matplotlib using conda console in your project

conda install -c conda-forge matplotlib

You also can install package using PIP

python -m pip install -U matplotlib

And Finally import your package in your source code

import matplotlib.pyplot as plt

Or another Way you can import

from matplotlib import pyplot as plt

Having come here with this error myself, especially if you're using VS Code you need to make sure that the version of Python you're running in VS Code is the same one that's running on your OS. Otherwise, you can conda install or pip3 install all you want into the OS, but VS Code and Jupyter Notebooks won't know anything about those modules. There are a few ways to figure this out, but mostly it's making sure that the Python version you see when typing python --version in the terminal matches the interpreter shown in the top-right corner of your VS Code window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM