Setting up a clean, robust, and efficient Python development environment in WSL2
This is an excellent question. Setting up a clean, robust, and efficient Python development environment in WSL2 is key to a great workflow.
Here is the recommended setup and process, broken down into logical steps from initial installation to project management. This process emphasizes using modern, best-practice tools that will serve you well in any professional development context.
Step 1: Install WSL2 and Your Ubuntu Distribution¶
If you haven't already, this is the foundation.
-
Install WSL2: Open PowerShell as Administrator and run:
This command automatically installs the WSL2 virtualization platform and the latest LTS version of Ubuntu. Reboot when prompted. -
Initial Ubuntu Setup: After rebooting, an Ubuntu terminal will open. Create your Linux username and password. Once that's done, immediately update your package lists and upgrade your packages:
Step 2: Install Core Development Tools¶
These are the essential build tools that many Python packages and other software depend on for compilation.
*build-essential: Installs the GCC compiler and other critical tools.
* git: For version control.
* curl: A versatile tool for downloading files and testing APIs.
Step 3: The Right Way to Install Python (Using pyenv)¶
Do not use the system's default Python (/usr/bin/python3) for your projects. Doing so can lead to conflicts with OS packages and makes managing different Python versions for different projects a nightmare.
The best practice is to use pyenv, a powerful tool for managing multiple Python versions.
-
Install
pyenvDependencies: -
Install
pyenvitself: -
Configure Your Shell for
pyenv: Add the following lines to the end of your~/.bashrcfile (or~/.zshrcif you use Zsh). This makes surepyenvis loaded every time you open a terminal.Now, apply the changes by restarting your terminal or running:echo '' >> ~/.bashrc echo '# pyenv configuration' >> ~/.bashrc echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrcsource ~/.bashrc -
Install a Specific Python Version: Now you can install any version of Python you need. Let's install the latest stable version (e.g., 3.11.7, check the latest on the Python website).
-
Set Your Global Python Version: Set this newly installed version as your default Python for your user.
Verify it's working by checking your Python version:python --version. It should showPython 3.11.7. Thewhich pythoncommand should point to thepyenvshims directory.
Step 4: Manage Project Dependencies with Virtual Environments¶
Never install Python packages globally with pip install. Always use a virtual environment for each project to isolate its dependencies. Poetry is a modern, all-in-one tool that manages dependencies and virtual environments beautifully.
-
Install Poetry:
bash curl -sSL https://install.python-poetry.org | python3 -Follow the instructions to add Poetry to your PATH (it will likely involve adding a line to your.bashrcfile, similar topyenv). -
Configure Poetry to Create Virtual Environments Inside the Project Folder: This is a popular and convenient setting.
Step 5: Integrate with VS Code on Windows¶
This is the final piece that ties the whole workflow together.
- Install VS Code on Windows: If you haven't already.
- Install the "WSL" Extension: In VS Code, go to the Extensions tab and install the official WSL extension from Microsoft.
- Start a New Project: This is your complete, repeatable workflow for every new Python project.
- Open your WSL2 Ubuntu terminal.
- Create a new project directory and navigate into it:
- Initialize a new Poetry project:
(This will ask you a few questions about your project and create a
pyproject.tomlfile.) - Add your first dependency (e.g.,
requests): (Poetry will automatically create a virtual environment, find a compatible version ofrequests, install it, and lock the version inpoetry.lock.) - Open the project in VS Code by typing:
- VS Code will open on Windows, automatically connected to your WSL2 environment. It will detect that Poetry created a virtual environment and ask if you want to select it for the project. Say yes.
Your Recommended Development Process (The Day-to-Day)¶
Your workflow for a new project now looks like this:
-
In your WSL2 Terminal:
mkdir my-new-app && cd my-new-apppoetry initpoetry add <package-name>(e.g.,poetry add fastapi uvicorn)git init(if you're starting a new repo)code .
-
In VS Code (on Windows):
- VS Code is now connected to WSL2. The integrated terminal is your Linux terminal.
- Select the Python interpreter that Poetry created (it should be in a
.venvfolder inside your project). - Write your code (e.g., in
main.py). - Run your code using the Poetry-managed environment:
- Enjoy a seamless, isolated, and reproducible development environment.
This setup provides the best of all worlds: the power and authenticity of a Linux development environment, the safety of version and dependency management, and the polished user experience of VS Code running on Windows.