geotheory
geotheory

Reputation: 23650

Fixing Python on Mac

Getting Python right on Mac seems a constant challenge. I'm working with a Homebrew implementation, and now have difficulty running Jupyter except with sudo:

$ which python
/usr/local/bin/python
$ which pip
/Users/username/bin/pip
$ which jupyter
/usr/local/bin/jupyter
$ jupyter notebook
Traceback (most recent call last):
  File "/usr/local/bin/jupyter", line 7, in <module>
    from jupyter_core.command import main
ImportError: No module named jupyter_core.command
$ sudo jupyter notebook
The Jupyter Notebook is running at: http://localhost:8888/

Once running, Jupyter fails to import pandas (installed via pip install pandas):

import pandas
ImportError                               Traceback (most recent call last)
<ipython-input-18-d6ac987968b6> in <module>()
----> 1 import pandas
ImportError: No module named pandas

.. even though pandas is available (python -s 'import pandas' works fine). Guessing its path isn't available to root.

I'm wondering if Jupyter is particularly problematic or if this is just my setup..?

Grateful for assistance as this is becoming very tiresome. Any guidance on wiping python and reinstalling 'properly' from scratch would be handy.


Edit:

$ which virtualenv
/usr/local/bin/virtualenv
21:16 $ virtualenv
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in <module>
    from virtualenv import main
ImportError: No module named virtualenv

Upvotes: 2

Views: 1155

Answers (2)

Ray
Ray

Reputation: 41428

Stop using python via native install on OSX or via homebrew right now!!!

With these two technologies in your toolbox you will never have the issue you are experiencing again.

Please note pyvenv and pyenv are two seperate technologies used to enable similar goals (to control/sandbox environments) with unfortunately VERY similar names. One thing to note is you'll see older references to virtualenv if you google around a bit. This is the same thing as pyvenv.

Someone put together a good overview of using these with Jupyter: http://www.alfredo.motta.name/create-isolated-jupyter-ipython-kernels-with-pyenv-and-virtualenv/.

The other option is to do everything in virtual machines (with say virtualbox) or better still Docker containers, but this a whole other ball of fun best left to after you master virtual environments (you'll want them when you build your docker images anyhow).

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95252

What python is in the shebang (#!) line on pip and jupyter? You may have installed jupyter with a pip that was using /usr/bin/python. Which means it's not there in the libraries installed for Homebrew's /usr/local/bin/python.

You should be able to resolve this with something like this:

/usr/local/bin/python $(type -p pip) install jupyter

But in general I second @Ray's suggestion of using pyenv to manage a personal set of Python installations that you can switch between, independently of what's on the system. (I likewise use rbenv for Ruby and perlbrew for Perl.)

Upvotes: 1

Related Questions