5

Over the past couple years I've pip installed lots of things without knowing what I was doing. All of a sudden, I am getting this error when I run a snippet in a Python 2.7 Jupyter Notebook:

ImportError: No module named matplotlib

When I run this in Terminal: which -a python python 3... I get:

Sams-MacBook-Pro-2:~ sambrand$ which -a python python3
//anaconda/bin/python
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python

I think this is showing that I have Python in a few places, which might explain why my standard libraries (eg. matplotlib) aren't being found in the Python environment that I want to use.

My ideal result:

Ultimately, I just want to clean up my environment and start from scratch so that I can use Keras, Tensorflow, and all Anaconda packages with Python 3 as my default when I open a Jupyter Notebook.

Additionally, I'd like any subsequent pip install from the terminal to update what I'm using in Jupyter.

From what I gather, this kind of bewilderment about Python environment is a fairly common issue, so I hope this post proves helpful.

samthebrand
  • 3,020
  • 7
  • 41
  • 56
  • Why do you care what happens in a Python 2.7 notebook if your goal is to get Python 3 notebooks working right? Can you import matplotlib from within a Python 3 notebook? – BrenBarn Nov 26 '17 at 07:09
  • 2
    You really should look at using a virtual environment. – cs95 Nov 26 '17 at 07:21
  • @BrenBarn Just now got a Python 3 notebook working. But cannot import matplotlib from within it. (Thanks!) – samthebrand Nov 26 '17 at 07:22
  • @coldspeed that's what the tensorflow docs suggest. Meanwhile [this guy](http://www.informit.com/store/deep-learning-with-tensorflow-livelessons-applications-9780134770857) says use Docker. So I'm going with option 3 for the moment and trying to get most basic setup installed first. I'm probably not doing this very intelligently. – samthebrand Nov 26 '17 at 07:24
  • @samthebrand to use matplotlib you need to install it with pip see the docs https://matplotlib.org/faq/installing_faq.html – Amit Tripathi Nov 26 '17 at 07:29

4 Answers4

13

Having a lot of python versions on the same machine is quite a common situation. To clean up your python you need to do the following steps:

  1. Keep in mind that as for OSX 10.15, "native" python version in still 2.7. It lives in /usr/bin/. (Actual path is /System/Library/Frameworks/Python.framework/Versions/2.7/bin/ but it's the same thing.) If you want to roll back to the clean system python environment, you need to delete everything else.

  2. First of all you need to find all other pythons. On my machine it was like this:

    $ which -a python python3
    /Users/yura/anaconda3/bin/python # <- Anaconda
    /opt/local/bin/python            # <- ports
    /opt/local/bin/python            # <- ports
    /opt/local/bin/python            # <- ports
    /opt/local/bin/python            # <- ports
    /usr/bin/python                  # <- "native"
    ~/anaconda3/bin/python3          # <- Anaconda
    /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 # <- python3 for OSX
    /usr/local/bin/python3           # <- python3 for OSX```
    
    

As you may see, I had installed more stuff from mac ports, I had python3 for Mac downloaded and installed by hands, and also I had badly installed Anaconda (it is not seen from here, but that installation had wrong access rights). Also, you may have something from homebrew, which I don't use. It will also appear in /usr/local/bin. Well, let's get started!

  1. remove Anaconda: $ rm -rf ~/anaconda3

  2. remove everything from /Library/Frameworks/Python.Framework/:

    $ sudo rm -rf /Library/Frameworks/Python.Framework/

  3. remove everything from /Applications/Python*/, which you may installed manually:

    $ sudo rm -rf /Applications/Python*

  4. remove all the symlinks from /usr/local/bin:

    $ sudo rm /usr/local/bin/python*

  5. remove all packages installed by pip in ~/Library/Python/: '

    $ rm -rf ~/Library/Python/

  6. And finally, you may also remove all port-related files, they are in /opt/local/bin/python*. WARNING: it may break some other port packages! So the most accurate way to do it is to use port itself (but you may skip this step in order to leave untouched the other software from ports):

    $ sudo port uninstall python*

  7. That's it! Now you have only a system python2.7. You can download Anaconda and install it:

    $ sh Anaconda3-*-MacOSX-x86_64.sh

  8. Now you have a new python3. To check this open a new terminal and try:

    $ python --version
    Python 3.6.5 :: Anaconda, Inc.
    
  9. And matplotlib and all the other scientific stuff like pandas etc is already there:

    $ python -c "import matplotlib as mpl; print(mpl.__version__)"
    2.2.2
    
Yury Kirienko
  • 1,810
  • 1
  • 22
  • 32
  • 1
    I ended up with a messy Python installation on Mac by having version installed by Anaconda, Homebrew, Python.org, along with the system version. It didn't help that I originally had different versions and then tried to upgrade to 3.9 and then downgrade to 3.7. I followed Many guides on how to cleanup different versions on Mac, but this one was what worked at the end, being the most comprehensive I found. I now use Pyenv from this guide (https://www.ianmaddaus.com/post/manage-multiple-versions-python-mac/#homebrew---pyenv) and it works! – Ali Nov 13 '20 at 10:50
2

The best practice to keep it 'clean' is to use virtual environments. You can use conda to do it.

From terminal run:

conda create -n envs_name python=3.6

for example.

After that you need to activate it, this is like saying "do the following only in my virtual environment and not on the global one":

source activate envs_name
pip install keras
pip install tensorflow
pip install ipykernel

The ipykernel let's you manage your environments inside Jupyter.

It's really easy and convenient.

Diego Aguado
  • 1,604
  • 18
  • 36
1

It took me hours, and I haven't "cleaned" my environment, but I was able to get Python 3, Keras, Tensorflow, and Anaconda running in a Jupyter notebook by following these steps:

  1. Delete Anaconda from computer
  2. Install Anaconda from the web
  3. Install Python 3 from the web
  4. Pip install Keras from Terminal (for some reason Keras wasn't shown in Anaconda Navigator, as recommended in this post)
  5. Pip install Tensorflow from Terminal
  6. Open Jupyter Notebook from Terminal
samthebrand
  • 3,020
  • 7
  • 41
  • 56
0

I got this error "no module named Keras'. I was doing !pip install keras and !pip install tensorflow (in that order) from Jupyter Notebook. After I did the following the error went away.

  1. Opened CMDexe prompt from Anaconda navigator
  2. pip install Tensorflow
  3. pip install Keras

Now in jupyternotebook from keras import Sequential worked fine