Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with installing matplotlib in python 3.6

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?


2 Answers

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
like image 113
Arthur Tacca Avatar answered Jun 06 '26 05:06

Arthur Tacca


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

like image 21
Joshua Kan Avatar answered Jun 06 '26 07:06

Joshua Kan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!