In this post I will show you how to setup jupyter notebooks with pip under Windows. Also we will go through the process of creating virtual environments with pipenv and adding them to the kernel list in jupyter.
Python 3
We first have to install python. Since python 2 is essentially deprecated we will go with python 3 instead. Whenever we call a command with python
in the following we mean python3
. Visit https://www.python.org/ and download the latest, stable version of python 3. Be sure to check that you do not choose to add python to your PATH
variable as this could interfere with other installations like Cygwin. Once the installation is finished create two directories in your documents folder:
%USERPROFILE%\Documents\PythonInstall
%USERPROFILE%\Documents\PythonScripts
Inside PythonInstall
create the following two .bat
files
pythoncmd.bat
@echo off
SET PATH=%PATH%;C:\Python3;C:\Python3\Scripts
cmd.exe /K "cd %USERPROFILE%\Documents\PythonScripts"
pause
jupyternotebooks.bat
@echo off
SET PATH=%PATH%;C:\Python3;C:\Python3\Scripts
jupyter notebook "%USERPROFILE%\Documents\PythonScripts"
pause
where in both files you should replace C:\Python3
with the base path of your python installation. The first file will start a command prompt inside your PythonScripts
folder and add the python interpreter to your PATH
variable. The second file can start jupyter once it is installed. Additionally, you can add links to these files to your python start menu folder.
Jupyter Notebooks
We first make sure that we have a clean package environment. Start pythoncmd.bat
and run
pip freeze
If the output is empty you are good to go. If not run the following commands:
pip freeze > req.txt
pip uninstall -y -r req.txt
del req.txt
This will uninstall any previously installed packages. To install jupyter simply run
pip install jupyter --upgrade
That’s it! You can now use jupyternotebooks.bat
and start coding.
Virtual Environments with pipenv
Virtual environments give you a clean python setup. You create a virtual environment with pipenv by typing
pipenv install
You can use any ordinary pip command with pipenv. In order to add the current virtual environment to your jupyter kernels run
pipenv install ipykernel
pipenv shell
This will bring up a prompt similar to
(my-virtualenv-name) bash$
To install the kernel for jupyter notebooks run
python -m ipykernel install --user --name=my-virtualenv-name