This guide explains how to set up a Python development environment using Visual Studio Code with Windows Subsystem for Linux (WSL). This configuration allows you to run Python scripts and Jupyter notebooks in an Ubuntu environment while using VS Code’s interface.
Why Use WSL for Python Development
Using WSL for Python development offers several advantages:
- Native Linux environment for Python libraries and tools
- No need for dual booting
- Access to both Windows and Linux tools
- Seamless integration with VS Code
Prerequisites
Before starting, ensure you have:
- Windows 10/11 with WSL2 installed
- Ubuntu installed on WSL
- Visual Studio Code installed on Windows
Step 1: Install VS Code Extensions
Install these extensions in VS Code:
- Open VS Code on Windows
- Go to Extensions (Ctrl+Shift+X)
- Install:
- Remote - WSL
- Python
- Jupyter
- Pylance
Step 2: Connect VS Code to Ubuntu WSL
There are two methods to connect VS Code to your Ubuntu environment:
From VS Code
- Click the green button in the bottom-left corner
- Select “Connect to WSL”
- VS Code will reopen connected to Ubuntu
From Ubuntu Terminal
- Open Ubuntu terminal
- Navigate to your project directory
- Run
code .
Step 3: Set Up Your Python Project
The setup files are available at: https://github.com/aamersadiq/python-setup
Clone the repository:
git clone https://github.com/aamersadiq/python-setup.git
cd python-setup
The repository contains:
setup_environment.py: Python virtual environment setup scriptsetup_jupyter.py: Optional Jupyter setup scriptnumber_game.py: Sample Python scriptpython_basics.ipynb: Sample Jupyter notebook- VS Code configuration files
Step 4: Set Up Python Environment
- Open a terminal in VS Code connected to WSL
- Run:
python3 setup_environment.py
This script:
- Creates a Python virtual environment
- Prompts you to activate the environment
- Installs required packages
- Registers a Jupyter kernel
When prompted, activate the virtual environment:
source .venv/bin/activate
The script will continue automatically after activation.
Step 5: Test Your Setup
Running a Python Script
- Open
number_game.pyin VS Code - Click the play button or press F5
- The game runs in the terminal
Running a Jupyter Notebook
- Open
python_basics.ipynbin VS Code - Select the Python kernel when prompted
- Run cells using the play button or Shift+Enter
Daily Workflow
After initial setup, your daily workflow will be:
- Start VS Code and connect to WSL
- Activate the virtual environment:
source .venv/bin/activate - Work on Python files or Jupyter notebooks
Benefits
This setup provides:
- Isolated development environments
- Consistent environment for scripts and notebooks
- VS Code’s debugging and IntelliSense features
- Good performance with WSL2
- Flexibility between Windows and Linux
Conclusion
This configuration combines VS Code’s interface with Ubuntu’s Python environment. The setup scripts make the process straightforward and reproducible.
For more details and all files mentioned in this guide, visit: https://github.com/aamersadiq/python-setup