Pipenv with VScode or Jupyter

Pipenv with VScode and Jupyter

Please follow and like us:

Python can work on all types of computers. So can the python packages that you install. However, not every Python package works well with every other python package. So if you need to make a project work, one thing you could do is to erase everything other than your operating system. And you could do this every time you start a new project. Obviously, starting from an empty hard drive is a very time-consuming way to get everything working on your system. The best solution in my opinion is to create a virtual environment with pipenv. You can find out more about pipenv on Pypi.org’s pipenv page.

Pipenv is a program that allows users to set up virtual environments with the packages and package versions you need. It works with VScode and can also create kernels for Jupyter Lab or Jupyter Notebook. Kernels let you run Jupyter Lab or Jupyter Notebook with particular packages installed. I will cover using pipenv with Jupyter later.

For now, let’s focus on using pipenv with VScode. Previously, developers had to use Pip and virtualenvs separately. Pipenv allows developers to use them together.

Pipenv with VScode

First, start VScode. Go to the view, drop-down menu at the top-left and select Terminal. You will need to see the command line from VScode to make a virtual environment.

If you begin writing python code with the lines:

import pandas as pd
import numpy as np
import sklearn
import os

You may or may not have pandas installed on your system already. If you do have pandas installed, you likely also have numpy installed, but it is best to make sure.

You happen to know that you do not have sklearn installed. Os is already installed because it is one of the standard python packages. Before doing anything else, save the file as pipenv_test.py in a folder on your desktop.

You should be able to see the command line on the bottom half of the screen. Make sure the active directory in the terminal is the same as the directory that you saved the pipenv_test.py. To change directories type in cd desktop and hit return. This command switches to the desktop.

Then type in,

cd foldername

where foldername is the name of whatever folder in which you saved the pipenv_test.py example.

Now, let’s run pipenv from the command line and install the packages you need. In this case, type in,

pipenv install pandas numpy scikit-learn

After running the command, you will see confirmation that the virtual environment was created. Then, pipenv will install the latest versions of the packages that you listed. You will see a status bar showing how much is left to install.

Once completed, the terminal returns to the command line prompt that was there previously. After the virtual environment is created, you still need to activate it. Type in,

pipenv shell

to activate the virtual environment.

Then, to ensure that everything is working correctly, type in,

python pipenv_test.py

This command will attempt to run the code that you have so far. If there are no problems with package imports, you should not get any errors, and the terminal should return to pipenv shell.

You will notice that running pipenv install creates two files, a Pipfile, and Pipfile.lock. The Pipfile is a list of the packages that you want installed when you create the virtual environment. It naturally lists the packages you installed previously, pandas, numpy, scikit-learn. You will also notice that sklearn has a different name when importing it within a python program than when you install it as scikit-learn with pipenv.

Pipenv with Jupyter Lab or Jupyter Notebook

Now I will go through the steps for creating a Jupyter Lab or Jupyter Notebook kernel with these packages installed. Making a kernel does not require moving to a specific directory. It can be done from any directory in the terminal. VScode does not need to be running, but since it already is, let’s continue to use the terminal in VScode.

Now type in,

pipenv install ipykernel pandas numpy scikit-learn

Ipykernel makes the kernel for use in Jupyter.

Making the Pipenv Kernel that Runs in Jupyter

Finally, type,

python -m ipykernel install –user –display-name machine_learning_tutorial –name machine_learning_tutorial

I need to point out that the text above has three double hyphens, one before user, another before display, and another before name. The font of this theme block does not show it well. Here is an image of how the text should look.

For some reason you may also have a problem copying and pasting this particular command onto the terminal. So I recommend copying the actual text (not the image of the text) first to a text editor or VScode, fixing the double hyphens if you need to, and then pasting into the command line.

Now, see if the kernel is working.

Type in,

Jupyter Lab

Jupyter Lab should open in your default browser. On the top left of the browser window, below the address bar, select File. Then, click on new and Select Notebook.

Now, select the kernel you want to run in the new notebook from the dropdown menu. You should be able to choose the kernel you just created, pipenv_test_kernel. If you don’t see the kernel listed you may not have created the environment properly.

Once you are running the new kernel in Jupyter, try importing pandas, numpy and sklearn like you did in pipenv_test.py.

Save the file with an ipnb extension in the folder you want. Then, try running it with the right arrow above the code you wrote and below the folder tabs. If you did everything right, you should not get any errors.

Sometimes Pipenv may hang while trying to lock dependencies. In that case, press control-C to stop pipenv and return to the command line prompt, then,

pipenv lock –clear

Noteably, the above command has two dashes (minus signs) before clear, it may or may not copy that way. After trying the above command. Try,

pipenv install

In many cases it may now work.


Pypi.org. (2020, May 29) Pipenv: Python Development Workflow for Humans. Retrieved from https://pypi.org/project/pipenv/

Pieters, M. (2019, Feb 28) in answer to question: ‘Pipenv stuck ā€œā ‹ Lockingā€¦ā€‘. stackoverflow.com. Retrieved from
on July 15, 2020.

Please follow and like us: