- 1 year ago
Python Venv with Windows Linux Subsystem
Using Python venv with Windows Subsystem for Linux (WSL) can be a bit tricky due to differences in file systems and Python installations. Here's a step-by-step guide to set up a Python virtual environment in WSL:
Install WSL:
If you haven't already, install Windows Subsystem for Linux by following the instructions in the Microsoft documentation: https://docs.microsoft.com/en-us/windows/wsl/installInstall Python in WSL:
Open a WSL terminal (e.g., Ubuntu, Debian) and install Python. In most WSL distributions, Python should be pre-installed. You can check the installed Python version using the commandpython3 --version
.Create a Python Virtual Environment in WSL:
In the WSL terminal, navigate to the desired project folder and create a Python virtual environment using venv:bashpython3 -m venv venv
Activate the Virtual Environment:
To activate the virtual environment, run the following command:bashsource venv/bin/activate
You should see
(venv)
in your terminal prompt, indicating that the virtual environment is active.Install Python Packages:
With the virtual environment active, you can install Python packages as usual usingpip
. For example:bashpip install requests
Run Python Scripts:
You can now run Python scripts within the virtual environment as you normally would in WSL. For example:bashpython my_script.py
Accessing Windows Files:
The WSL file system is separate from the Windows file system. To access Windows files from WSL, they are usually mounted under the/mnt
directory. For example, to access theC:
drive, you can use/mnt/c/
. So, if you need to work with Windows files in your Python script, you can use this path.
Remember to deactivate the virtual environment when you are done working on your project:
bashdeactivate
That's it! You now have a Python virtual environment set up and running in Windows Subsystem for Linux.