WSL & VS Code & ipynb Kernel & pyenv virtualenv | Medium

Source: WSL & VS Code & ipynb Kernel & pyenv virtualenv | Medium

Streamline your Python development by integrating Jupyter, VS Code, pyenv, and WSL

Rangga Kusuma Dinata

4 min read

May 29, 2023

1

Windows Terminal of WSL2 opening Visual Studio Code by the author

A step-by-step guide to simplifying your Python development workflow.

✨ Prerequisites

Before we get started, make sure you have these tools installed:

With all these installed, you’re ready to start. Let’s dive into the steps!

1️⃣ 🐍 Installing the Python Version

The first step is to install the Python version you’ll be using on your virtual environment with pyenv. If you haven’t installed it yet, use this command:

pyenv install <python_version>

To check the installed Python versions, use:

pyenv versions

Here’s what it looks like on my Windows Terminal, the * indicates the current active Python version:

Python version by the author

2️⃣ 📁 Heading to the Target Directory

Next, navigate to the directory where you’ll be working with your virtual environment. Here, pyenv will create a .python-version file for your virtual environment.

3️⃣ 🌐 Creating a Virtual Environment

Creating a virtual environment is a breeze with pyenv. Just use the command:

pyenv virtualenv <python_version> <environment_name>

For example, to use Python 3.11 for a virtual environment named german_credit, run:

pyenv virtualenv 3.11 german_credit

4️⃣ 🔛 Activating Your Virtual Environment

Now, it’s time to activate your newly created virtual environment. This can be achieved with the command:

pyenv local <environment_name>

If you’ve configured eval “$(pyenv virtualenv-init -)” to run in your shell, it will automatically activate or deactivate your Python versions when it enters or exits a directory with a .python-version file.

Here’s what it looks like on my Windows Terminal, notice there is a prefixgerman_creditat my current WSL:

Active virtual environment by the author

5️⃣ 💡 Installing ipykernel into Your Virtual Environment

VS Code needs the ipykernel package to run your Python environment as a kernel. So, install ipykernel in your active virtual environment using:

pip install ipykernel

This is also a good time to install other requirements for your project, such as pandasnumpy, or scipy.

6️⃣ 🚀 Launching VS Code Remote via WSL

With ipykernel installed, you can launch VS Code from the terminal session where you activated your virtual environment using:

code .

This command assumes you’ve already installed the VS Code Remote — WSL Extension.

7️⃣ 📔 Selecting the Kernel in Your Jupyter Notebook

Open your Jupyter notebook in VS Code and click on “Select Kernel” in the upper right-hand corner. If your active virtual environment isn’t in the dropdown, click on “Select Another Kernel”. You should now see a list of available Python Environments. Locate your active virtual environment in this list and click on it.

Here’s what it looks like on my Visual Studio Code:

Selecting kernel by the author

8️⃣ ✅ Verifying Your Setup

Finally, let’s verify the setup. Try running a cell in your Jupyter notebook that imports a package you installed earlier. If there are no errors, congratulations, you’ve successfully set up your Jupyter Notebook Kernel in VS Code with pyenv virtualenv in WSL! 🎊🥳

This step-by-step guide should provide a straightforward path to optimizing your Python development workflow. Happy coding! 🚀

Leave a Reply

The maximum upload file size: 500 MB. You can upload: image, audio, video, document, spreadsheet, interactive, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here