Problems with installing matplotlib in python 3.6

18.9k Views Asked by At

I'm trying to teach myself python, and I feel out of my depth. To start, I am working on a mac which already comes with python 2.7 installed.

I installed python 3.6 recently and have been using it to teach myself the basics. I'd like to eventually learn how to produce mathematical plots in python, and I know I will need the matplotlib package to do that.

Following some advice online, I was told that python3 already comes with pip installed, which is what I thought I should use to install matplotlib. The advice said I should type the following into the mac terminal:

python3.6 -m pip install matplotlib

I typed this, and it seemed like the package was installing, but I ended up getting some sort of error code that said:

Command "python setup.py egg_info" failed with error code 1 in [folder].

I tried opening IDLE and typing "import matplotlib", but I got the error: "no module named matplotlib". I also tried typing "import matplotlib.pyplot as plt", but I got the same error.

Based on further research and this youtube video, I've decided to just install miniconda in order to have access to the matplotlib package.

The problem is, I'm not sure if I should somehow be uninstalling whatever was installed when I ran the code above to install matplotlib. I've actually run that line of code 3 or 4 times. Should I remove anything before installing miniconda? Also, I am running python 3.6, while miniconda is listed on the website as being for python 3.5. Does this mean it won't work for my version of python?

7

There are 7 best solutions below

0
On BEST ANSWER

I ended up downloading anaconda and using the python interpreter that comes with it, as anaconda comes with matplotlib and many other python packages of interest.

2
On

Running pip like that would install packages system-wide. I'm guessing it's failing because you're not running as root (i.e. the administrator user). But wait! Don't try again as root! Instead of installing packages, do it in a virtual environment. First create it:

virtualenv myenv

This creates a directory called myenv with a bunch of stuff in it (so make note of where you run this command). Whenever you want to use the virtual environment (like straight away!) you first need to activate it:

. myenv/bin/activate

Don't miss out that dot (followed by a space) at the beginning! As the other answer says, the first thing you should do in it is upgrade pip:

pip install --upgrade pip

Now you're ready install whatever else you like:

pip install matplotlib

One last note: The virtual environment is tied to a particular Python version. By default it uses the system's Python 2.7 installation, so to use a different one you need to specify it when you create the virtual environment, like this (if that Python version is installed system-wide):

virtualenv -p python3.5 myenv

Or like this (if that Python version is not installed system-wide):

virtualenv -p /path/to/my/installation/of/python3.5 myenv

While the virtual environment is activated, you don't need to specify the particular path/version of Python. Just run it like this:

python
2
On

Try upgrade setup tools

--upgrade setuptools

or

easy_install -U setuptools

or upgrade pip

pip install --upgrade pip
0
On

I also encountered many problems during my installation.
It seems that version 2 of matplotlib is not compatible with Python version 3.

Finally, I succeeded by specifying version 3 of matplotlib as follows with the following command:

sudo apt-get install python3-matplotlib

Reference from the Matplotlib website:
https://matplotlib.org/users/installing.html#building-on-linux

0
On

the pip command typically is for the Python 2. use pip3 instead to install the libraries in the python 3.X path

This should work pip3 install matplotlib

0
On

Matplotlib files are downloaded in ~/.local/lib/python3.6/site-packages/ and not in /usr/lib/python3.6/ . Try the command:

sudo cp -r ~/.local/lib/python3.6/site-packages/* /usr/lib/python3.6/
0
On

The solution that work for me in python 3.6 is the following

py -m pip install matplotlib