What is WSL?
Windows Subsystem for Linux (WSL) is a compatibility layer that allows you to run Linux binaries in a Windows environment. With WSL, developers can leverage the power of Linux command-line tools and applications directly within Windows. This guide will walk you through setting up WSL for Python development in Windows 11.
Step 1: Install Windows Subsystem for Linux (WSL)
1. Open Settings
- Click on the Start menu and select Settings (the gear icon).
2. Access Features
- Go to the Apps section, then click on Optional features.
3. Add WSL
- Scroll down and select More Windows features. Check the Windows Subsystem for Linux option. Click OK.
4. Enable the Virtual Machine Platform
- In the same Optional Features window, find and enable Virtual Machine Platform.
5. Restart
- Restart your computer to allow changes to take effect.
Step 2: Install a Linux Distribution
1. Open Microsoft Store
- After your PC reboots, open the Microsoft Store.
2. Search for Your Distribution
- In the search bar, type the name of a Linux distribution (e.g., Ubuntu, Debian, or Kali Linux).
3. Install the Distribution
- Click on your desired distribution, then select the Install button. Wait for the download and installation process to complete.
Step 3: Set Up Your Linux Distribution
1. Launch the Distribution
- After installation, click Launch from the Microsoft Store or find it via the Start menu.
2. Create a User Account
- You will be prompted to create a new user account. Enter your preferred username and password.
3. Update Package Index
- Once the terminal loads, it’s good practice to update the package index. Type:
sudo apt update sudo apt upgrade
Step 4: Install Python
Python comes pre-installed in many Linux distributions, but for the latest version or specific packages, follow these steps:
1. Install Python
- In your Linux terminal, run:
sudo apt install python3 python3-pip
2. Verify Installation
-
To check that Python is installed correctly, run:
python3 --version -
Check the pip version:
pip3 --version
Step 5: Set Up a Virtual Environment
Using a virtual environment allows you to manage project dependencies efficiently.
1. Install Virtual Environment Package
- Install the
venvmodule with:sudo apt install python3-venv
2. Create a Project Directory
- Create a project directory:
mkdir my_python_project cd my_python_project
3. Create a Virtual Environment
- Create a virtual environment named
venv:python3 -m venv venv
4. Activate the Virtual Environment
-
To activate the virtual environment, run:
source venv/bin/activate -
You will see the environment name prefixed in your terminal prompt, indicating it’s activated.
Step 6: Install Required Packages
With your virtual environment activated, you can now install any necessary Python packages.
1. Using pip
- For example, to install Flask, run:
pip install Flask
2. Requirements File
- If you have a
requirements.txtfile, install all dependencies with:pip install -r requirements.txt
Step 7: Write Your First Python Script
1. Create a Python File
- In the project directory, create a new file called
app.pyusing a text editor like nano:nano app.py
2. Write Sample Code
-
Write the following sample code to create a basic web application with Flask:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, WSL with Python on Windows 11!" if __name__ == "__main__": app.run(host='0.0.0.0', port=5000) -
Save and exit the editor (usually CTRL + X, followed by Y, then Enter).
Step 8: Run Your Python Application
1. Execute the Script
- Ensure your virtual environment is active and run your application:
python app.py
2. Access the Application
- Open a web browser in Windows and navigate to
http://localhost:5000. You should see “Hello, WSL with Python on Windows 11!”
Step 9: Integration with Visual Studio Code
VS Code is a powerful code editor that works seamlessly with both Windows and WSL.
1. Install VS Code
- If you haven’t installed VS Code, download and install it from the official site.
2. Install Remote – WSL Extension
- Open VS Code, go to Extensions, search for and install the Remote – WSL extension.
3. Connect to WSL
- Open the Command Palette (CTRL + SHIFT + P) and type
Remote-WSL: New Window. This will open a new VS Code window connected to your WSL instance.
4. Open Project Folder
- Use the terminal in VS Code to navigate to your project folder, or use the Open Folder option to access your project directly.
Step 10: Debugging and Running Programs in VS Code
1. Set Debugging Configuration
- To debug, click on the Run and Debug icon on the left sidebar. Select create a launch.json file and choose Python.
2. Start Debugging
- Set breakpoints and run your application directly from Visual Studio Code.
By following these detailed steps, you can effectively use WSL for Python development on Windows 11, benefitting from the combined capabilities of both operating systems.