跳转至

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.

  1. Install WSL2: Open PowerShell as Administrator and run:

    wsl --install
    
    This command automatically installs the WSL2 virtualization platform and the latest LTS version of Ubuntu. Reboot when prompted.

  2. 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:

    sudo apt update && sudo apt upgrade
    

Step 2: Install Core Development Tools

These are the essential build tools that many Python packages and other software depend on for compilation.

sudo apt install build-essential git curl
* 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.

  1. Install pyenv Dependencies:

    sudo apt install -y make build-essential libssl-dev zlib1g-dev \
    libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
    libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
    

  2. Install pyenv itself:

    curl https://pyenv.run | bash
    

  3. Configure Your Shell for pyenv: Add the following lines to the end of your ~/.bashrc file (or ~/.zshrc if you use Zsh). This makes sure pyenv is loaded every time you open a terminal.

    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 -)"' >> ~/.bashrc
    
    Now, apply the changes by restarting your terminal or running: source ~/.bashrc

  4. 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).

    pyenv install 3.11.7
    

  5. Set Your Global Python Version: Set this newly installed version as your default Python for your user.

    pyenv global 3.11.7
    
    Verify it's working by checking your Python version: python --version. It should show Python 3.11.7. The which python command should point to the pyenv shims 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.

  1. 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 .bashrc file, similar to pyenv).

  2. Configure Poetry to Create Virtual Environments Inside the Project Folder: This is a popular and convenient setting.

    poetry config virtualenvs.in-project true
    

Step 5: Integrate with VS Code on Windows

This is the final piece that ties the whole workflow together.

  1. Install VS Code on Windows: If you haven't already.
  2. Install the "WSL" Extension: In VS Code, go to the Extensions tab and install the official WSL extension from Microsoft.
  3. 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:
      mkdir my-python-project
      cd my-python-project
      
    • Initialize a new Poetry project:
      poetry init
      
      (This will ask you a few questions about your project and create a pyproject.toml file.)
    • Add your first dependency (e.g., requests):
      poetry add requests
      
      (Poetry will automatically create a virtual environment, find a compatible version of requests, install it, and lock the version in poetry.lock.)
    • Open the project in VS Code by typing:
      code .
      
    • 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 workflow for a new project now looks like this:

  1. In your WSL2 Terminal:

    • mkdir my-new-app && cd my-new-app
    • poetry init
    • poetry add <package-name> (e.g., poetry add fastapi uvicorn)
    • git init (if you're starting a new repo)
    • code .
  2. 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 .venv folder inside your project).
    • Write your code (e.g., in main.py).
    • Run your code using the Poetry-managed environment:
      poetry run python main.py
      
    • 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.