Data Science with VsCode + Poetry + Jupyter

Rodrigo Torres
A3data
Published in
4 min readSep 21, 2020

How to take advantage of VsCode functionalities and Poetry’s ease to manage dependencies, when working with Jupyter notebooks? This article gives brief and straight forward instructions on how to set all of that together.

Software used:

- Visial Studio Code
- Python
- Poetry
Extensions used for VsCode:
- Python
- Python Docstring Generator

Installing software

Visual Studio Code

In the Stack Overflow 2019 Developer Survey, Visual Studio Code was ranked the most popular developer environment tool, with 50.7% of 87,317 respondents reporting that they use it.

For VsCode installation, follow the steps from their website.
The extensions can be installed by clicking the highlighted icon on the left side bar or by pressing Ctrl + Shift + X.

Python

Windows Users:
https://www.python.org/downloads/
When installing it, make sure to add Python to the path:

Ubuntu Users:

sudo apt-get install python3

Poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Windows Users:
To install poetry, use the following command in the PowerShell:

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python

Then, add Poetry’s bin directory to your system path, as prompted:

For that, follow the steps:

Ubuntu users:

To insall poetry on osx / linux / bashonwindows, use the following command:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

Initialize new Poetry project

Now we can initialize a Poetry project with this command:

poetry new "your_project_name"

Poetry will initialize the project with a basic structure

After initialization we can start adding packages to the new project with the poetry add command:

poetry add numpy

And remove packages with poetry remove command:

poetry remove numpy

The packages available are the same as the ones available with pip.

Installing existing Poetry project

To install a existing Poetry project on your computer (create virtual environment and install packages), simply run poetry install command inside the root folder of the project:

poetry install

Adding Poetry environments to VScode

For every project, Poetry creates a virtual environment, to keep them isolated from each other. We need to tell VsCode the location of these virtual environments to be able to run our codes. That just needs to be done once, after the installation of Poetry.

Poetry only creates the virtual environments folder, after the a project created and the first package is installed. So, don’t try to find the folder before that.

Windows Users:

To find out the oath of Poetry’s virtual environments folder on Windows, just type the following on the address bar of windows explorer:

%userprofile%\AppData\Local\pypoetry\Cache\virtualenvs

In possession of the path, we can edit the json file that contains VsCode settings and assign it to a new field, called “python.venvPath”. It is important to notice that the backslashes “\” from the path, needs to be replaced with double backslashes “\\” on json files. The following video show how to edit the json file:

Ubuntu users:

Follow the same steps on the above video, but set “python.venvPath” to “~/.cache/pypoetry/virtualenvs/”:

{
“python.venvPath”:”~/.cache/pypoetry/virtualenvs/”
}

Setting up VsCode to use the project virtual environment

To make VsCode run python code inside a project using the virtual environment set by poetry, we need to select the python interpreter for the project. For that follow the video and select your projects environment from the list:

Sometimes VsCode needs to be reload to update its settings. If you don’t see the interpreter you need on the list, restart VsCode.

Setting up VsCode to run Jupyter notebooks

To be able to use the notebooks on the virtual environment created by Poetry on VsCode, we first need to install jupyter on the system environment (was the only way I managed to do it, at least). For that we will use pip:

pip install jupyter

Make sure the virtual environment is not active and that you are installing jupyter on the system environment rather than your project’s.

Now you just have to install ipykernel on your projects virtual environment, and you are all set to start using notebooks on VsCode:

poetry add ipykernel

--

--