Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sarah-j-smith/1a054d5efa3ee7f32c1ee52f17a8a0f1 to your computer and use it in GitHub Desktop.
Save sarah-j-smith/1a054d5efa3ee7f32c1ee52f17a8a0f1 to your computer and use it in GitHub Desktop.
TensorFlow/Python3/Jupyter Virtualenv on Mac

Pre-requisites

What we'll install

Installation

Download & install a code editor if you don't have one. Visual Studio Code is recommended.

Install python3, virtualenv and virtualenvwrapper. Note the $ below indicates a command typed at the Terminal prompt. Type the commands after the $, then press enter to execute it.

$ brew install python3
$ export PATH=/usr/local/bin:$PATH
$ pip3 install --upgrade pip
$ pip3 install virtualenv
$ pip3 install virtualenvwrapper

Now create a new virtual Python environment that references python3, and use that to install TensorFlow & Jupyter notebooks. Note that the prompt in your terminal will change to (tf):

$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv --python=/usr/local/bin/python3 tf
$ 
(tf) $ pip install --upgrade tensorflow
(tf) $ pip install jupyter

You may need to add

$ export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3

Before the above if you get errors about not being able to load hooks.

Check Installation

Type a short TensorFlow program at the Python interactive shell.

(tf) $ python

The output should be a flashing cursor in the Python 3 shell with something like this:

(tf) $ python
Python 3.6.2 (default, Sep 29 2017, 15:14:48) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

If the number after Python eg Python 3.6.2 in the above example, is not 3.x.y then something went wrong. Go back and re-check your commands above for errors.

TensorFlow Check

Now type the program, line by line. Here the >>> is the python shell prompt - type just the part after that:

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

Press control-D to exit the Python shell.

Jupyter Notebook

Change to a new directory:

(tf) $ mkdir ~/py3tf && cd ~/py3tf

Then run

(tf) ~/py3tf $ jupyter notebook

Your browser should open and a Jupyter Notebook is displayed. Enter the TensorFlow check code into the first cell and press shift-enter to run it.

Usage

From now, if your command line does not show (tf) in front, do

$ source /usr/local/bin/virtualenvwrapper.sh
$ workon tf

Using your code editor edit your .bashrc and .bash_profile files in your home directory, to save having to type the above, and also to have coloured output that is easier to read.

The file ~/.bash_profile may already exist, if not create it. Ensure it contains these lines.

source ~/.bashrc

# Have an ls that is in color by default
alias ls='ls -G '

# Brew etc
export PATH=/usr/local/bin:$PATH

# Virtualenv
source /usr/local/bin/virtualenvwrapper.sh

The file ~/.bashrc may already exist, if not create it. Ensure it contains these lines.

export PS1="\[\033[1;34m\]\w $\[\033[0m\] "
export PS2="> "

You can restart your Jupyter Notebook with Python3 and TensorFlow by opening a new Terminal window and:

$ cd ~/py3tf 
$ workon tf
(tf) ~/py3tf $ jupyter notebook

##Troubleshooting

Do not use sudo

The above virtual environment means that all python packages are installed local to your user id. They are available when you switch into the virtualenv environment by typing workon tf. Don't ever use sudo inside of the environment.

Some online guides might suggest using sudo - disregard that as it will mess up your environment.

If you have messed up your virtualenv and are getting Operation not permitted or similar errors, try:

sudo find /Users/sez/.virtualenvs/ -type d -exec chown sez:staff \{} \;

In the above replace sez with your username. It should be the name of your home directory, and you can also find it out by typing whoami at the Terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment