jtmoulia


Using Pipenv & Jupyter

Python's data analysis packages (pandas, sklearn, spacy, etc) and user interfaces (Jupyter, IPython, seaborn) make it a swiss army knife for working with data. This article sketches out how to quickly set up a Python data analysis environment using Pipenv to install Jupyter.

Install Pipenv

The one requirement is Pipenv for setting up the Python venv and installing the dependencies into it. If you're on OS X homebrew is a great package manager:

brew install pipenv

Set up the venv

Pipenv identifies which venv should be used by the working directory, so first create and navigate into your project directory:

mkdir my-project
cd my-project

With Pipenv you can set up the venv with Jupyter:

pipenv install jupyter

Based on what you need to get done you can install additional data analysis packages, e.g. pandas for data manipulation, seaborn for visualization, and spacy for NLP:

pipenv install pandas seaborn spacy

Note that your exact Python environment can be replicated using the Pipfile and Pipfile.lock files.

Starting the Jupyter Notebook

Launch the Jupyter kernel and notebook by calling from your project directory:

pipenv run jupyter notebook

Once loaded, Jupyter should automatically launch its filesystem navigator in your browser. This command will run in the foreground, preventing you from running any further commands in the same shell.

Importing Python Libraries

Assuming you installed the libraries described above they can be imported in the Jupyter notebook by running:

import pandas as pd
import seaborn as sns

Here are some other commonly imported packages with their shorthand alias:

import numpy as np
import matplotlib.pyplot as plt

Connecting the IPython Console

To run ad-hoc commands and inspect the state of variables in the notebook you can connect an IPython console to the running Jupyter notebook. From the project directory where the Jupyter notebook was launched run:

pipenv run jupyter console --existing

IPython is especially useful for quickly looking up documentation.

Written by

Tags:

Published:

Modified: