Jupyter Notebooks I: Getting Started with Jupyter Notebooks
In the Virtual Environments II, I showed you how to create a directory and activate a virtual environment for your Python data science project. I also showed you how to install some libraries: Ipykernel, Jupyter Notebook, Pandas and Matplotlib. In this tutorial, we’re going to discuss Ipykernel and how to get started with Jupyter Notebooks.
This blog is part of a series of tutorials called Data in Day. Follow these tutorials to create your first end-to-end data science project in just one day. This is a fun easy project that will teach you the basics of setting up your computer for a data science project and introduce you to some of the most popular tools available. It is a great way to get acquainted with the data science workflow.
I. Creating a Kernelspec to Start Using Jupyter Notebooks
Jupyter notebooks are an interactive environment where you can write and execute Python code, as well as add markdown cells to explain your methods and code. We are going to use Ipykernel to link our virtual environment to jupyter so that we can easily use that environment in a notebook.
- In the previous tutorial, we activated a virtual environment in our project folder and installed Ipykernel, Jupyter Notebook and some other packages. If you need to install them now, you can enter:
$ pip3 install ipykernel jupyter notebook
2. Now we will use ipykernel to create a kernelspec, which is a file within the kernel folder of the ~/Library/Jupyter directory that was installed when you installed Jupyter Notebook. The kernelspec file will link Jupyter Notebook to the virtual environment that you created.
3. Even though the kernelspec will live in ~/Library/Jupyter, that is not where we are going to create it. Instead, enter the following into the command line in your project folder where your virtual environment is activated:
python -m ipykernel install — user — name my_project_env — display-name “my_project_env”
In the command above, the name of our virtual environment my_project_env is located next to the — name argument, which we repeated for the -display-name argument. The— name argument should always match the exact name of your virtual environment. The display name is what you will see in Jupyter Notebooks, so you can call it whatever you like.
If this plan worked, the terminal should display something like this:
Installed kernelspec my_project_env in /Users/myusername/Library/Jupyter/kernels/my_project_env
II. Launching Jupyter Notebook
4. Now that you have Jupyter notebook installed and you’ve used ipykernel to create a kernel spec, you should be able to launch a Jupyter notebook by entering the following:
$ jupyter notebook
5. This will launch Jupyter Notebook, and your project directory will be displayed in your browser. In the top right corner, you’ll see a drop down menu that says ‘New’. Click the button, and in the drop down you should see your virtual environment listed. Click on it and a new untitled notebook will appear.
6. Now, you have a new notebook to begin coding your project. Let’s test it out and make sure our preliminary package installations worked by trying to import them into our notebook.
By default, the first cell in the notebook should be a code cell. If not, use the drop down menu on the tool bar to define that cell as a code cell.
Enter the following to the first cell:
import pandas as pd
If the notebook did not output an error message, the installation has been a success! Now you can start coding your project. If you did get an error message, I suggest carefully reading the first, second, and third tutorial in this series, just to make sure that you did not miss a step.
III. What Did We Do?
- Checked to see if we installed Ipykernel and Jupyter Notebooks.
- Used Ipykernel to create a kernelspec to link our virtual environment to Jupyter.
- Launched a new notebook in the browser.
- Checked our Pandas package installation.
VI. What is Next?
Keep reading to learn how to link your project to GitHub in GitHub I. In later tutorials, you’ll learn some tips about how to use Jupyter Notebooks.
Check out more tutorials like this at Data in Day.