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
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:
Pip install Keras
from Terminal (for some reason Keras wasn't shown in Anaconda Navigator, as recommended in this post)Pip install Tensorflow
from TerminalJupyter Notebook
from TerminalI 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.
Now in jupyternotebook from keras import Sequential worked fine
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:
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.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!
$ rm -rf ~/anaconda3
remove everything from /Library/Frameworks/Python.Framework/
:
$ sudo rm -rf /Library/Frameworks/Python.Framework/
remove everything from /Applications/Python*/
, which you may installed manually:
$ sudo rm -rf /Applications/Python*
remove all the symlinks from /usr/local/bin
:
$ sudo rm /usr/local/bin/python*
remove all packages installed by pip
in ~/Library/Python/
: '
$ rm -rf ~/Library/Python/
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*
That's it! Now you have only a system python2.7
. You can download Anaconda and install it:
$ sh Anaconda3-*-MacOSX-x86_64.sh
Now you have a new python3
. To check this open a new terminal and try:
$ python --version
Python 3.6.5 :: Anaconda, Inc.
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
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.