1

I just did a

 sudo pip install matplotlib

which worked fine. Then

 sudo pip install matplotlib

Details:

$sudo pip install mpld3
Downloading/unpacking mpld3
  Downloading mpld3-0.2.tar.gz (1.1MB): 1.1MB downloaded
  Running setup.py (path:/private/tmp/pip_build_root/mpld3/setup.py) egg_info for package mpld3

Installing collected packages: mpld3
  Running setup.py install for mpld3

Successfully installed mpld3
Cleaning up...

So mpld3 also installed fine.

For some reason matplotlib is showing up in python, but not mpld3:

In [3]: import matplotlib.pyplot as plt

In [4]: plt.plot([3,1,4,1,5], 'ks-', mec='w', mew=5, ms=20)
Out[4]: [<matplotlib.lines.Line2D at 0x103ab4d10>]

In [5]: plt.show()

works fine ..

But:

In [6]: import mpld3
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-25a6968c2f16> in <module>()
----> 1 import mpld3

ImportError: No module named mpld3

Is it necessary to update e.g. PYTHONPATH or some other env var?

UPDATE Answering (good) question from @infinity about the pip vs ipython versions:

$which pip
/usr/local/Cellar/python/2.7.8_2/bin/pip
18:20:42/lightbox2 $which ipython
/usr/local/bin/ipython
18:20:45/lightbox2 $which python
/usr/local/Cellar/python/2.7.8_2/bin/python
18:23:58/lightbox2 $sudo pip install ipython
Password:
Requirement already satisfied (use --upgrade to upgrade): ipython in /Library/Python/2.7/site-packages
Cleaning up...
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

4 Answers4

2

Looking at your where results, I think your ipython seems to look at the wrong path when trying to import modules.

The complete diagnose and fix (for you and future Googlers):

  1. Use which (Linux) or where (windows) on your command prompt to check the pip and python/ipython paths.

    1.1. If the ipython seems to run from a different path, start it and check where the matching Python installation using import sys; sys.executable.

  2. Anyway, fix this problem by carefully modifying the PATH environment variable in a way that it will show the path to right Python executable.

Edit: After a bit of extra discussion with the OP (look @ comments), if the ipython installation isn't linked with the python executable, you should change the first line of your ipython file to shabang, then the path of your Python executable. It should look like: #!/usr/bin/python when /usr/bin/python is the path to the right Python executable.

Edit 2: Better solution for unlinked ipython would be to install ipython for the right Python version, using python -m pip install ipython (thanks @abarnert and @koffein).

Yam Mesicka
  • 6,243
  • 7
  • 45
  • 64
  • The "path to right python executable" is not sufficient here. The PATH does point to the correct one. The issue appears to be there is no way to configure ipython to point to a different version of python - short of modifying the ipython script itself. – WestCoastProjects May 24 '15 at 01:49
  • Maybe editing the first line /usr/local/bin/ipython to `#!/usr/local/Cellar/python/2.7.8_2/bin/python` will do? – Yam Mesicka May 24 '15 at 01:51
  • That is what I have been saying is required. But that is ugly. It is surprising there were no means to configure ipython to point to a different python short of mangling the ipython script itself. In my own version I have changed to !/usr/bin/env python – WestCoastProjects May 24 '15 at 01:53
  • Have you trying to install ipython using pip? (`pip install ipython`). You pip runs from the right path, I think it should work – Yam Mesicka May 24 '15 at 01:55
  • The OP shows that I had re-done *pip install ipython*. That is a reasonable suggestion. I am just going to live with the update to the ipython script that I mentioned. – WestCoastProjects May 24 '15 at 01:56
  • 1
    I am going to make an answer that says to change the ipython script to #!/usr/bin/env python. But you have been helpful so will upvote you. – WestCoastProjects May 24 '15 at 01:57
  • Also editing my own answer to include this line, if you don't mind. – Yam Mesicka May 24 '15 at 01:59
  • The `ipython` executable is just a very thin script that wraps up the actual `ipython` module, which is installed in the site-packages of one Python or another. If you just point it at a different Python, with a different version of IPython, or no IPython at all, it will break. Don't do that. Instead, either only install IPython for the one Python version you care about, or install a separate IPython script for each Python (which may require renaming the script after it's installed, or having separate `bin` directories and symlinking from there). – abarnert May 24 '15 at 05:19
2

Lets say you start the python console from the terminal with ipython. If you want to ensure that you are installing the packages to the right python installation, use

sudo ipython -m pip install mpld3

So automatically the right pip (and the right paths) should be used.

koffein
  • 1,792
  • 13
  • 21
0

The ipython executable is hardcoded to /usr/bin/python. The best workaround I can see is to change the first line from

#!/usr/bin/python

to

#!/usr/bin/env python

That fixed the issue for my case of a non-standard python location.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
0

The ipython script is just a very thin wrapper around the actual IPython module, installed into Python's site-packages.

Just changing its #! line to point at a different Python is almost always a bad idea. That other Python may have a different IPython version in its site-packages/dist-packages, or may not have one at all.


Of course if you only want one ipython, it's just a matter of installing it with the right version of Python, and not installing it for any other Python.

The problem most people run into is that they accidentally install it for two different Pythons, and whichever one they installed second (or, occasionally, whichever one ends up in a winning location on the PATH) ends up being the one they use, even though it's not the one they intended.

If you don't want IPython for that second Python version, just don't install it. Or, if you already did, uninstall it for both Python versions, then reinstall it for just the one you want.


What if you want IPython for multiple Python versions?

Unfortunately, they're all going to try to install with the same name, ipython. Maybe ipython2 and ipython3, but that only helps if your own two Python installations are one 2.x and one 3.x. If you have CPython 3.4 and PyPy 3.2, or a system-installed CPython 2.7 and a third-party CPython 2.7, that doesn't do you any good.

So, what can you do?

What I do is to make sure each Python (besides the system-installed one) is configured to use a scripts/bin directory that isn't on my PATH, then symlink things manually.

So, on both my Mac and Linux systems, ipython and ipython2 both point at the system Python 2.7, and were installed just by sudo pip install ipython. But ipython3 is a symlink that I manually created to where my main Python 3 installation installs binaries. And ipypy and ipypy3 are symlinks to where my main PyPy 2.x and 3.x installations install binaries. (And these of course all parallel symlinks I created named python, python2, python3, pypy, and pypy3.)

I did have to create these symlinks manually, but that's better than modifying the scripts.

abarnert
  • 354,177
  • 51
  • 601
  • 671