Python is an easy programming language to start with, and writing Python code in the Windows Subsystem for Linux (WSL) on Windows 10 and 11 is also an easy way to build cross-platform apps.

Here's how to set up Python for development on WSL.

Why Python on WSL?

If you have no experience with Linux, learning a new operating system as well as a new programming language can seem like a daunting prospect. WSL provides access to Linux tools, including programming tools, in a more familiar environment.

While Python is a cross-platform language, it was originally developed on Unix-like systems and many tutorials assume a Linux/Unix environment. The same goes for many pre-written scripts you might find across the web.

There is a native port for Windows, but it tends to work the way Windows does. This can make it difficult to port apps written in Python for Windows to other systems.

An environment like WSL enforces a more “Unixy” development style. You’ll be able to run scripts written by others and other people will be able to run your scripts in turn, no matter what they’re running.

Setting Up Python on WSL

Many distros, even on WSL, make such extensive use of Python as a scripting language that they include it in their default systems. It’s likely that you have Python installed already, whether that’s in Debian/Ubuntu, openSUSE, or Oracle Linux.

While Python is typically included by default on Linux distros, the actual version may differ. Python 3 is the one that’s in active development, but some systems include Python 2 for backward compatibility. The latter is unmaintained, even for security updates, according to the Python developers.

You can check which version you’re running with the -V or --version options:

        python -V
    

Alternatively, you can call the Python interpreter directly and check the version number on startup.

If you see any Python 2 versions, you have several options. You can specify Python 3 at the command line with:

        python3
    

If you’re running Ubuntu or Debian, you can install the python-is-python3 package:

        sudo apt install python-is-python3
    

If you’re on another system, creating a shell alias is the simplest option for invoking Python 3 at the command line:

        alias python="python3"
    

You can put this in your shell startup file, such as .bashrc or .zshrc.

A riskier option is to create a symbolic link:

        sudo ln -s /usr/bin/python /usr/bin/python3
    

This is risky because any system updates could clobber the symlink. If the system still has Python 2 installed, it will just be replaced with the Python 2 executable. This could affect all the scripts on the system.

Starting the Python Interpreter on WSL

Python interactive interpreter in WSL with "Hello, world!" example

Once you’ve got the Python interpreter set up, you can start the interactive interpreter. This is a great way to learn how Python works by typing code directly into it and seeing what happens.

It’s also great if you already know Python and want to see if an idea will work before you write a more complete script.

The way the interpreter works is simple. Just like the shell, there’s a prompt for you to type input. Once you hit Enter, the Python interpreter will evaluate your code and return the output. This will either be an error message or the result of some operation.

A good thing to try is the standard code snippet across the development world, printing “Hello, world!” on the screen.

The code to do this in Python is simple:

        print("Hello, world!")
    

When you’re finished with the interpreter, press Ctrl + D or type "exit()" to return to the shell prompt.

Writing Python Scripts in WSL

Writing Python scripts in WSL is also simple. All you have to do is call the interpreter with the proper shebang line at the top of every script:

        #!/usr/bin/env python
    

What this does is call up the env program to run Python wherever it’s installed on the system. This is important because Python may be installed in different directories depending on the distribution or operating system.

You should also make sure your scripts have execute permissions:

        chmod +x script.py
    

To run your script, call it at the command line in the directory you saved it in, prefixing it with “./”:

        ./script.py
    

An Easy Way to Get Started With Coding in Python

Python on WSL offers an easy way to start with cross-platform scripting, or even coding if it’s your first language. It’s typically installed with many Linux distros, even on WSL.

Calling the interactive interpreter and writing scripts is also easy. WSL is a great cross-platform coding environment on its own, but it's really powerful when integrated into Visual Studio.