Problems with installing matplotlib in python 3.6

15,618

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
Share:
15,618
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • AwesomeJackify
    AwesomeJackify over 7 years
    You just type any of the two commands(depending which one doesn't work) without anything else in the terminal. I have installed matplotlib before on my mac when I began reading 'doing math with python by Amit Saha' and the instructions said to install matplotlib for mac by typing-------------pip install matplotlib-venn--------. Actually, if you recently installed python, pip doesn't need to be upgraded as it comes with python so sorry if I confused you. To try it out here is a tip--->go to terminal, type python, then type import matplotlib. Bang done! (unless there is a error of course)
  • Arthur Tacca
    Arthur Tacca over 7 years
    @user46944 Yes, creating a virtual environment creates a new folder (called ./myenv in this example). But the nice thing about it is that pip install and pip uninstall only affect packages in that directory (while the virtual environment is activated). So it's really safe, because if you screw things up somehow you can just delete the whole directory (i.e. rm -r myenv) and it's like it never existed. To start Idle with that virtual environment activated, the easiest way is to activate the virtual environment from the command line and then run python -m idlelib.
  • Arthur Tacca
    Arthur Tacca over 7 years
    @user46944 1. I don't know anything about Mac's finder (or Macs in general) but the resulting folder is just a normal directory so I think it would show up. 2. No you shouldn't save your scripts in there; the virtual environment directory is just for the packages you install in it with pip. 3. There's no need (but it's fine if you do); the virtual environment should be unaffected by packages you install elsewhere.
  • AwesomeJackify
    AwesomeJackify over 7 years
    If you try to install matplotlib again even if you think you have downloaded it there are two outcomes- 1. you successfully downloaded it meaning you did not download it the first time 2.It says you already have it downloaded. 3.It says you already have it download and recommends you to upgrade because a new version came out(follow the onscreen prompt if you want to upgrade). Sorry, I haven't mentioned but matplotlib does not support python3. there is an unstable version on GitHub but I wouldn't recommend it. Don't worry, python2 isn't so bad. Just remember input() is raw_input!