How to use Pipenv with Jupyter and VSCode | by Daniel Deutsch | Towards Data Science

This article was written in 2019. During the years of development, I have encountered various issues with the pipenv setup. I changed the environment and dependency management to using a conda environment and a requirements file with pip-compile. My latest setup is described in my new article “How to start a data science project” https://towardsdatascience.com/how-to-start-a-data-science-project-boilerplate-in-2021-33d81393e50
If you still want to use pipenv then the article should still be valuable. Enjoy.
Currently, I study Artificial Intelligence at the JKU university and for some exercises, we need to use jupyter notebooks. Having worked a little bit with Python the package manager pipenv proofed to be valuable. Now, I encountered some problems using it with Jupyter notebooks and within VSCode. Therefore, a short guide on how I solved it.
The Issue
Developing with Jupyter Notebook in the browser
Develop with Jupyter Notebook in VSCode
About
As I described in my last article Working with Jupyter and VSCode I use pyenv and pipenv for managing all packages in my python development. I also referenced some articles why this way is helpful and easy to use. Now, it is necessary to dive a little more into it. There are two ways you would want to develop with jupyter notebook. Either you work with it directly in the browser or inside VSCode. In both use cases, there can emerge problems.
Let’s say you already have the proper python environment on your system and now you want to create a specific one for a project.
First, create a Pipenv environment.Make sure to navigate into the correct directory.Use pipenv install <packages> to install all your packages.Then use pipenv shell to activate your shell.Then use pipenv install jupyter and afterward pipenv run jupyter notebook.
First, create a Pipenv environment.
Make sure to navigate into the correct directory.
Use pipenv install <packages> to install all your packages.
Then use pipenv shell to activate your shell.
Then use pipenv install jupyter and afterward pipenv run jupyter notebook.
Now the jupyter server is started and your notebook will have access to the correct environment.
Now for the workflow within VSCode. Here, it is import to be aware of different shells. I often use a separate terminal (iterm2) and sometimes an activated shell is not recognized by VSCode or you are in a wrong directory or it is not activated. All of this causes problems. Therefore my workflow is as follows:
First, create a Pipenv environment.Make sure to navigate into the correct directory.Use pipenv install <packages> to install all your packages.Then, be sure to have a proper settings file in your vscode folder with content like this:
First, create a Pipenv environment.
Make sure to navigate into the correct directory.
Use pipenv install <packages> to install all your packages.
Then, be sure to have a proper settings file in your vscode folder with content like this:
{    "python.venvPath": "${workspaceFolder}/.venv/bin/python",    "python.pythonPath": ".venv/bin/python",}
Afterward, you can choose the proper python environment within VSCode. ( It should be the one created with Pipenv!) Now the correct environment is recognized for python files.
Normally this should be sufficient for VSCode and you can start the Jupyter server in it.
But sometimes you change the environment, or there is an issue in the settings file. If this is the case you need to open the VSCode terminal and run pipenv shell to activate the shell. (Check if the correct environment is still selected in VSCode):
Now, after opening the .ipynb file, you will be able to run the cells and not get the error "... was not able to start jupyter server in environment xxx"
Let me know if it helps or if you have other issues or solutions working with Pipenv in VSCode and Jupyter Notebook.
I consider myself a problem solver. My strengths are to navigate in complex environments, provide solutions and breaking them down. My knowledge and interests evolve around business law and programming machine learning applications. I provide services in building data analysis and evaluating business-related concepts.