Friday, May 9, 2025

[Python] What is the best practice for managing Python environments inside WSL?

I am using VS Code with WSL and use Miniconda on Windows, what is the best practice for managing Python environments inside WSL?

(answers generated by ChatGPT)

Using a separate Miniconda installation inside WSL gives you the most consistent and manageable Python environment experience (isolated from system Python).

🔧 Steps:

  1. Install Miniconda in WSL:

    bash

    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh

    Follow the prompts to install, and allow it to add conda to your .bashrc or .zshrc.


  1. Restart your WSL terminal or run:

    bash

    source ~/.bashrc
  2. Answer yes to make conda available easily. Then immediately run:

    bash
    conda config --set auto_activate_base false
    This keeps conda available in your shell. Prevents auto-activation of the base environment (you'll be in your system Python unless you explicitly activate a conda env).
  3. Create a new environment:

    bash

    conda create -n myenv python=3.11 conda activate myenv
  4. You can install a conda package also without activating the environment. Just use conda install -n <env_name> <package> or conda install -p <path/to/env> <package>.
  5. Use it in VS Code:

    • Open your project folder in VS Code using code. from inside WSL.

    • When the conda environment is active, VS Code should detect it. If not:

      • Press Ctrl+Shift+PPython: Select Interpreter → Choose the myenv from conda.

No comments:

Post a Comment