Getting Started with Jupyter Notebooks on OSX
with Homebrew, Python and Virtual Environments
A Jupyter Notebook is an open source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. Jupyter Notebook is maintained by the people from Project Jupyter
Jupyter Notebooks are a spin-off project from the IPython project, which used to have an IPython Notebook project itself. The name, Jupyter, comes from the core supported programming languages that it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, which allows you to write your programs in Python, but there are currently over 100 other kernels that you can also use.
Although Jupyter has been developed for data science applications, which are written in languages like Python, R and Julia, the platform is now used in all kinds of ways for projects. Apart from that, by removing the barriers for data scientists, Jupyter made documentation, data visualisations, and caching a lot easier, especially for hardcore non-technical folks.
Let’s get it set up!
Here are 4 easy steps to getting this all setup!
1. Install Homebrew
If you do not hav Homebrew installed on your Mac, do that first. More details on Homebrew can be found here. But the quick and simple is:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
What does Homebrew do you ask? Well it installs the stuff you need that Apple and other linux distributions did not! For example if you want to quickly install “wget” you just do this:
brew install wget
Homebrew is the missing package manager for MacOS and other Linux distributions.
2. Install PyEnv and VirtualEnv
PyEnv and VirtualEnv are a great tool for managing different versions of Python on your Mac. Even if you already have Python installed on your system, it is worth having Pyenv installed so that you can easily try out new version features or contribute to a project that uses a different version of Python. To install it just use brew like this:
brew install pyenv pyenv-virtualenv
And add pyenv init to your shell. If you don’t use Zsh as your shell yet, read Step 3 of the Basic Github Checkout, I use Zsh as my shell so I just used:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv virtualenv-init -)"\nfi' >> ~/.zshrc
This will enable shims and autocompletion. Pyenv will try its best to download and compile the wanted Python version, but sometimes compilation fails because of unmet system dependencies, or compilation succeeds but the new Python version exhibits weird failures at runtime. The following instructions are recommendations for a sane build environment.
It should have been installed already in step 1 but just in case you missed that abd f you haven’t done so, install Xcode Command Line Tools
xcode-select --install brew install openssl readline sqlite3 xz zlib
For more information for other Linux Distributions check out Suggested build environment on the PyEnv github wiki.
Now you can issue the command:
pyenv install 3.9.0
or whichever Python version tickles your fancy and you should now be able to set this as your default Python:
pyenv global 3.9.0
This will set 3.9.0 as your global primary Python or you can set it to your preferred Python version. To make sure everything is working as expected, start a new terminal window and issue these commands to see if Pyenv is controlling the version of Python:
python (then ctrl-d to exit python) which python python -V pyenv versions pyenv versions
3. Getting Virtual Environments Setup
You can now setup a Virtual Python Environment using the current version of Python in your project directory. Virtual environments are a big part of managing Python installations and applications. If you haven’t used virtual environments before, check out the Python Virtual Environments Primer. For this demo I will setup two different projects with two different versions of Python
mkdir project379 cd project379 pyenv virtualenv 3.7.9 project379 pyenv local project379 python -m venv project379
And activate this Virtual Python Environment:
source project379/bin/activate mkdir project390 cd project390 pyenv virtualenv 3.9.0 project390 pyenv local project390 python -m venv project390
And activate this Virtual Python Environment:
You can activate different versions of Python using virtual environments in different projects directories. As you switch between the directories the version of Python used will change.
4. Install Jupyter
In any of your virtual Python environment you can now install Jupyter and it should use the Python version installed in that directory!
pip install jupyter
and as needed, install ipykernel:
pip install ipykernel
If you install any other packages with pip in your virtual environment, like for example:
pip install pandas
you’ll have install that in your environment using the following command:
python -m ipykernel install --user --name project379
Now you can run your Jupyter notebook in your virtual environment simply be running: