Setting Up Windows Subsystem for Linux (WSL)
Before starting Python development with WSL, ensure your Windows operating system is updated. Check for updates in Windows Settings > Update & Security. To set up WSL, follow these steps:
-
Enable WSL Feature:
Open PowerShell as Administrator and run the command:wsl --installThis command installs the latest version of WSL and downloads a supported Linux distribution.
-
Choose a Distribution:
The installation will typically default to Ubuntu. If you want a different distribution, you’ll need to install it from the Microsoft Store. Popular distributions include Debian, Kali Linux, and Fedora. -
Complete Installation:
After installation, restart your computer to ensure that all changes take effect. You’ll need to set up your Linux username and password upon first launch.
Basic Linux Commands for Navigation
Familiarize yourself with basic Linux commands to navigate and manage files efficiently:
ls: Lists files and directories in the current directory.cd <directory>: Changes the current directory to the specified directory.pwd: Prints the working directory path.
Example:
cd ~/projects
Installing Python
Once you’ve set up your WSL environment:
-
Update Package Lists:
Open your WSL distribution and update your package lists:sudo apt update -
Install Python:
To install Python, run:sudo apt install python3 -
Verify Installation:
Check the installation by running:python3 --versionThis command displays the currently installed Python version.
Setting Up Python Development Environment
To enhance your Python development experience, set up a virtual environment:
-
Install
venv:
Ifvenvis not already installed, you can add it with:sudo apt install python3-venv -
Create a Virtual Environment:
Navigate to your project directory and create a virtual environment:mkdir myproject cd myproject python3 -m venv venv -
Activate the Virtual Environment:
Activate your virtual environment with:source venv/bin/activateYou should see
(venv)before your command prompt, indicating the virtual environment is active.
Installing Python Packages
When inside a virtual environment, you can install Python packages using pip. To install commonly used packages:
-
Install
pipif it is not included:sudo apt install python3-pip -
Install Packages:
pip install numpy pandas matplotlib
Creating Your First Python Script
-
Create a Python File:
You can use various text editors available on Linux. For example, usingnano:nano hello.py -
Write a Simple Program:
Write the following code:print("Hello, WSL Python!")Save the file and exit (in
nano, pressCTRL + X, thenYto save). -
Run Your Script:
To execute your script, use:python hello.py
Integrating WSL with Visual Studio Code
Visual Studio Code provides a friendly interface for Python development:
-
Install Visual Studio Code:
Download and install Visual Studio Code from the official site. -
Install WSL Extension:
In VS Code, go to Extensions (or pressCtrl + Shift + X) and search for the “Remote – WSL” extension. Install it. -
Open WSL in VS Code:
Open the command palette (Ctrl + Shift + P) and type “Remote-WSL: New Window”. This opens a new VS Code window connected to your WSL instance. -
Open Your Project Folder:
Use the “File” menu to open your project directory in WSL. You can now edit your Python files directly within VS Code.
Using Python Linters and Formatters
To maintain high-quality code, using linters and formatters is essential:
-
Install Flake8:
Within your activated virtual environment, install Flake8:pip install flake8 -
Configure Linting:
In VS Code, go to Settings and search for “python.linting.flake8Enabled”. Enable it to activate Flake8. -
Auto-formatting with Black:
Install the auto-formatter Black:pip install black -
Set Up Formatter:
Inside VS Code, go to Settings and search for “Python Formatting Provider” to select “black” as your formatter.
Managing Dependencies
For managing project dependencies efficiently:
-
Generate
requirements.txt:
You can create arequirements.txtfile that lists all installed packages in your environment:pip freeze > requirements.txt -
Install from
requirements.txt:
To install packages listed in arequirements.txtfile, use:pip install -r requirements.txt
Running Python Applications
Running a Python application in WSL is seamless and integrates well with debugging tools in VS Code:
-
Launch Configuration:
In VS Code, set up a launch configuration for Python by clicking the Run icon on the sidebar and following prompts. -
Run and Debug:
Start running your application by hittingF5, enabling you to debug as needed.
Accessible Documentation
Access and utilize Python documentation effectively:
-
Online Resources:
The official Python documentation is invaluable for learning and troubleshooting. -
Local Documentation:
Installpydocto access documentation directly from the command line:pip install pydocUse it like this:
pydoc <module_name>
Best Practices for Python Development in WSL
-
Code Version Control: Use Git for version control. Initialize a Git repository in your project:
git init -
Regular Backups: Regularly commit your code changes. Create a .gitignore file to exclude unnecessary files.
-
Documentation: Keep your code well documented using docstrings and comments.
-
Collaborate with Others: Utilize GitHub or other platforms for collaborative projects, enabling easy sharing and version management.
Following these structured instructions will set you up for successful Python development using Windows Subsystem for Linux, providing a flexible and powerful environment. With practice, you will become proficient in utilizing various tools and techniques to enhance your development workflow.