7

I did not have any problem to use "plt", but it suddenly shows an error message and does not work, when I import it. Please see the below.

>>> import matplotlib
>>> import matplotlib.pyplot as plt

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 6, in <module>
    from matplotlib.figure import Figure, figaspect
 File "/usr/lib64/python2.6/site-packages/matplotlib/figure.py", line 18, in <module>
   from axes import Axes, SubplotBase, subplot_class_factory
 File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 2, in <module>
   import math, sys, warnings, datetime, new
 File "new.py", line 12, in <module>
   import matplotlib.pyplot as plt
 AttributeError: 'module' object has no attribute 'pyplot'

This package is suddenly corrupted. So, I tried to install as below. I use Ubuntu.

In [1]: sudo apt-get install python-matplotlib
  File "<ipython-input-1-2400ac877ebd>", line 1
    sudo apt-get install python-matplotlib
   ^
SyntaxError: invalid syntax

If I need to reinstall, what are the detailed instructions on how to do it?

I am very new to Python. So, my problem might be too simple to be solved. But I cannot.

8 Answers 8

8

Try to type sudo apt-get install python-matplotlib in a terminal. In your message you typed it in the IPython console. This is a Bash command, not a Python one.

You need to quit IPython before. For that use Ctrl + D.

Sign up to request clarification or add additional context in comments.

Comments

5

I have this problem before.

What the conditions of mine is that I have install multiple versions of Python and NumPy in my OS. That's why the Python interpreter can't find the right NumPy to support the Matplotlib library.

The method I chose to solve this problem was modifying the $PYTHONPATH variable by editing the file .bashrc in my home path.

Change the order of the Python install path as shown, like putting the path "/usr/lib/python2.7/dist-packages" in the front of the $PYTHONPATH.

Comments

3

The correct answer to resolve this issue was found at Pylance reports that the import "matplotlib.pyplot" could not be resolved from source via a comment by jakebailey.

He says, "Did you install matplotlib into the environment while VS Code was running? If you reload, does this message go away?"

Visual Studio Code doesn't refresh anything. So, after you install anything via pip or pip3, it is a good idea to:

  1. Save your work.
  2. Close Visual Studio Code.
  3. Restart Visual Studio Code.

This fixed it for me.

Comments

1

Something is wrong with Tkinter I guess. Try adding

import matplotlib as mpl

mpl.use('TkAgg')

in the front of import matplotlib.pyplot as plt.

2 Comments

Why has Tkinter anything to do with it?
OK, the OP has left the building: "Last seen more than 3 years ago" Perhaps somebody else can chime it?
1

Try to do this:

python3 -m pip install matplotlib

2 Comments

Re "Try to do this": Why? What is it supposed to accomplish?
Please consider adding some explanation to the command explaining how it solves the problem.
1

I had the same problem using python through Spyder. Installing packages while the IDE is open do not make them updated in the environments even if you restart the kernel from the IDE. So the safest and sure option is to close the IDE (in my case spyder) and then restart. That has solved my issues.

Comments

1

Run your project in python virtual environment

  • Manual Approach

    • Open Terminal/CMD

      cd your_project_dir
      python -m venv .venv

    • Now each time you have to activate the environment

      .venv\Scripts\activate

    • Now install all the desired lib/packages (eg.)

      pip install matplotlib numpy scikit-image

  • In VS Code

    • Requirements
    • Open Terminal, and run this

      python -m venv .venv

    • wait for a minute
    • Kill the terminal and Open a new one
    • Now install all the desired lib/packages (eg.)

      pip install matplotlib numpy scikit-image

  • Screenshots








Comments

0

For terminal:

If you are using Python 2, then write this:

pip install matplotlib

If you are using Python 3, then write this:

pip3 install matplotlib

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.