Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import matplotlib even if it's installad

I am running a sample python program on my Mac (El Capitan, 10.11.5)
I have a the default version of python installed (2.6) and on top of that I installed python 2.7 and 3.5. I need matplotlib on my 2.7 version.

I installed it with pip (not pip3) and I don't know why it got installed on python 3.5.

If I type pip list this is the output:

cycler (0.10.0)
Django (1.8.4)
matplotlib (1.5.1)
numpy (1.11.1)
pip (8.1.2)
pyparsing (2.1.5)
python-dateutil (2.5.3)
pytz (2016.6.1)
selenium (2.53.6)
setuptools (19.4)
six (1.10.0)
wheel (0.26.0)

which python outputs: /usr/local/bin/python

My path is:

/usr/local/share/python3:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

In /usr/local/share/python3: I have python 3.5, in /usr/local/bin: python 2.7 and then in /usr/bin the default python.

When I run in my program from matplotlib import pyplot as plt and try to use it I get:

ImportError: No module named matplotlib

I think the problem is that matplotlib is installed on python3.5 site-package. How could I fix this?

Thank you!

like image 330
roccoSenteta Avatar asked Feb 12 '26 11:02

roccoSenteta


1 Answers

I have a the default version of python installed (2.6) and on top of that I installed python 2.7 and 3.5. I need matplotlib on my 2.7 version.

that's genereally not a problem, however you need to make sure the python environments are not mixed up.

I think the problem is that matplotlib is installed on python3.5 site-package. How could I fix this?

1. Use python's virtualenv feature

My recommendation is to use virtualenv *):

# for a python 3.5 environment
$ cd /path/to/<project with python 3>
$ PATH="/path/to/python3.x;$PATH" python -m venv myenv

# for a python 2.7 environment
$ cd /path/to/<project with python 2>
$ PATH="/path/to/python2.7;$PATH" virtualenv myenv

This will create clean per-project python environments with their seperate site-packages. With that you can work on your project (or even multiple projects) that have different packages or package versions installed, without them interfering.

That said, before you move any further, open a new Terminal to make sure all path are reset to a clean state.

2. Re-Install packages into your fresh virtualenv

Activate the environment and re-install the required packages into the project's environment using

# assuming the list of packages is in /path/to/project/requirements.txt
$ cd /path/to/project
$ source myenv/bin/activate
$ pip install -r requirements.txt

Once you have done this, you should be able to import matplotlib lib just fine:

python -c 'import matplotlib; print matplotlib'
<module 'matplotlib' from '/path/to/python/site-packages/matplotlib/__init__.pyc'>

3. Give yourself a break

To simplify using virtualenvs try virtualenvwrapper. This adds a couple of commands to your system to simplify the handling of virtualenvs, e.g.:

# create new environments
$ mkvirtualenv foo
# activate a particular environment
$ workon foo
# list packages in your environment 
$ lssitepackages 
(...)

*) Note that Python 3 provides the venv package as part of the standard library, whereas Python 2.7 requires that you install the virtualenv package first.

like image 80
miraculixx Avatar answered Feb 14 '26 02:02

miraculixx