Set Up Python on Windows Subsystem for Linux (WSL) (2024)

How to set up your Python development environment on Windows Subsystem for Linux (WSL).

Set Up Python on Windows Subsystem for Linux (WSL) (1)

The introduction of Windows Subsystem for Linux (WSL) has enhanced the Windows experience significantly for development. Developers can have full access to a Linux distribution within Windows using WSL. Development with WSL has become more joy after the launch of the latest version WSL 2. Many limitations of the first version were removed and now we can run an actual Linux kernel within Windows.

In this article, I will talk about the WSL filesystem and show you how to set up Python on WSL for a better development experience.

Table of Contents:· First Things First
Installing WSL and your preferred Linux distribution
· WSL Filesystem
· Python on WSL
· Installing Anaconda
· Choosing the Text Editor
· Conclusion
· Resources

Installing WSL and your preferred Linux distribution

First of all, you need WSL on your machine. I will not get into the details on how to install WSL. Instead, I will share some resources that can help you with the installation.

If you follow the official documentation you should be able to install WSL and a Linux distribution on your machine easily.

Now I will talk about the WSL filesystem and show you how the Linux filesystem and Windows filesystem merge together.

If you open your Linux distribution from a terminal it will open from the home directory of that Linux distribution. Look at the screenshot below:

Set Up Python on Windows Subsystem for Linux (WSL) (2)

This is my Ubuntu home directory. If you pwd to see the file location you will see /home/your_linux_user_name. In my case it was /home/shadhin. Here I have all the dotfiles and configurations for my bash shell. Let’s enter ls to see what’s going on:

Set Up Python on Windows Subsystem for Linux (WSL) (3)

Now we will go two steps back. It will take us to the root directory. Use cd ../../ to navigate two steps backward from home.

Set Up Python on Windows Subsystem for Linux (WSL) (4)

Let’s ls to see what’s in here:

Set Up Python on Windows Subsystem for Linux (WSL) (5)

This is a full Linux filesystem. We have our root user, home, dev, tmp and all the other good stuff! But for now, our concern is mnt. This is where Windows drives live. Let’s see what’s inside mnt:

Set Up Python on Windows Subsystem for Linux (WSL) (6)

You can see my Windows c drive here. I have only one drive on my Windows computer. If you have multiple drives like d, e etc. they will appear here also. You can access your Windows filesystem from here using Linux commands. For example, to navigate to your Windows Desktop you will do this:

$ cd mnt/c/Users/your_windows_useraname/Desktop

I hope this will give you an idea of how WSL merges the Linux filesystem and Windows filesystem together. Now let’s set up Python on WSL.

To install Python use this command:

$ sudo apt install python3 python3-pip

Or more specifically if you want to install Python3.9 use this command:

$ sudo apt install pythons3.9
Set Up Python on Windows Subsystem for Linux (WSL) (7)

I had both already installed. So they will not be installed again. Now we need to add the PATH to bashrc. Go to your home directory again and open the .bashrc file. Then add the following line at the end of your .bashrc file:

export PATH=”$PATH:/home/your_linux_username/.local/bin”

In my case it was —

export PATH=”$PATH:/home/shadhin/.local/bin”

Python is installed successfully! Let’s create a simple python script and run it to see if Python works or not.

Set Up Python on Windows Subsystem for Linux (WSL) (8)

I created a python script test.py and opened it with Vim so that I don’t need to leave the terminal. You can use any text editor. Let’s put some code in the file and run it.

Set Up Python on Windows Subsystem for Linux (WSL) (9)
Set Up Python on Windows Subsystem for Linux (WSL) (10)

Well, Python is all set up for WSL. Using pip we can install any Python packages that we need on Linux. The best practice is to keep our python packages inside a virtual environment. I wrote an article on how to properly manage our Python projects with virtual environments. You can read it here:

Managing a Python Project ProperlyHow to create virtual environments and requirements.txt for our Python projectpython.plainenglish.io

If your purpose is to do Machine Learning and Data Science you must need Anaconda. Let’s see how to install Anaconda on WSL.

First download Anaconda installer for Linux from here:

The downloaded file will be located inside the Downloads directory of Windows. Let’s navigate there and install Anaconda:

$ chmod a+x Anaconda3–2020.11-Linux-x86_64.sh$ ./Anaconda3–2020.11-Linux-x86_64.sh
Set Up Python on Windows Subsystem for Linux (WSL) (11)

If you kept all things default Anaconda should be installed inside your Linux home directory. If we go to the home directory we will see a folder named anaconda3 is created.

Set Up Python on Windows Subsystem for Linux (WSL) (12)

Again we need to add PATH to bashrc just like we did for Python. Just navigate to the end of your .bashrc file and put this:

export PATH=”$PATH:/home/shadhin/anaconda3/bin”

Anaconda is also ready to use. You can use conda environments and jupyter-notebooks for your Machine Learning and Data Science work. To open jupyter-notebook just put this command:

$ jupyter notebook --no-browser

Then copy one of the links and paste it into your browser to open your notebooks.

Set Up Python on Windows Subsystem for Linux (WSL) (13)

If you are using the Windows Terminal you should be able to hold the Ctrl key and click on the link to directly open from the terminal.

You can install your favorite IDE or Text Editor on your Linux distribution. I mostly use Vim for small Python scripts. Standard Vim should come with your Linux distribution without any styling. You don’t need to install it. To customize the Vim environment you need to configure your .vimrc file. Create a .vimrc file inside your Linux home directory and start customizing your Vim editor. You can start with this minimal vimrc setup:

You can also use Visual Studio Code. I use VS Code for larger projects. You don’t need to install VS Code separately for Windows and Linux. You can use your Windows VS Code just fine. To open a project in VS Code, just put this command from the project directory.

$ code .
Set Up Python on Windows Subsystem for Linux (WSL) (14)

Here, my VS Code is installed from Windows. But I can use it with Ubuntu bash also.

And that’s it! WSL provides the opportunity to do your development work on Linux even though you are on a Windows machine. It can make your life much easier as a developer. I hope you find this helpful. Thanks for reading.

More content at plainenglish.io

If you enjoy reading articles like these, consider becoming a Medium member. This way you get unlimited access to all stories on Medium. If you sign up using my referral link below, I’ll earn a small commission from your $5 a month. This way you can support me as a writer.

Set Up Python on Windows Subsystem for Linux (WSL) (2024)

FAQs

Should I install Python on WSL or Windows? ›

Install Windows Subsystem for Linux

WSL lets you run a GNU/Linux command line environment integrated directly with Windows and your favorite tools, like Visual Studio Code, Outlook, etc. We generally recommend using WSL 2 for Python web development work. To enable and install WSL 2, see the WSL install documentation.

How do I use Python 3.9 in WSL? ›

How do I use Python 3.9 in WSL?
  1. Installation steps. Run the following commands in your WSL terminal.
  2. Verify the installation. Run the following command to install Python 3.9: $ python3. ...
  3. Make Python 3.9 the default. If you want to make Python 3.9 the default python program, you can follow these steps:
  4. References.

How do I install Python 2.7 in WSL? ›

How do I install Python 2.7 in WSL?
  1. Open a command terminal.
  2. Add Universe repo.
  3. Install Python2. 7 on Ubuntu 20.04 LTS.
  4. See all available Python version on the system.
  5. Change the Default Python priority.
  6. Install Pip 2 on Ubuntu 20.04.
  7. Uninstall (optional)

How do I enable VENV Python in WSL? ›

Virtual Environment
  1. Create the venv and fix permissions: sudo virtualenv --python=python3 ~/weblate-env sudo chown -R {YOUR USERNAME}:{YOUR USERNAME} /home/{YOUR USERNAME}/weblate-env-2.
  2. Activate, or step into, the virtual environment: Source ~/weblate-env/bin/activate.

How do I install Python 3.10 in WSL? ›

How do I install Python 3.10 5 on Ubuntu?
  1. Step 1 – Open Terminal OR Command Prompt. First of all, your terminal or command prompt by pressing Ctrl+Alt+T key:
  2. Step 2 – Update APT Package.
  3. Step 3 – Add the deadsnakes PPA.
  4. Step 4 – Install Python 3.10.
  5. Step 4 – Verify Python Installation.

What are the disadvantages of WSL? ›

Cons of Using WSL
  • WSL Could Discourage Desktop Linux Adoption. ...
  • Remote Possibility of Microsoft Dominating Linux. ...
  • WSL Could Discourage Native App Development. ...
  • You're Still Using Windows. ...
  • WSL Is Not Really Designed for Servers.
21 Jan 2022

How do I install Python 3.8 on WSL? ›

How do I add Python to WSL?
  1. Set up your development environment. ...
  2. Install Windows Subsystem for Linux.
  3. Set up Visual Studio Code.
  4. Create a new project.
  5. Install Python, pip, and venv.
  6. Create a virtual environment.
  7. Open a WSL - Remote window.
  8. Install the Microsoft Python extension.

How do I install Python 3.9 on Windows 11? ›

Installing Python 3 on Windows 10 and 11
  1. Open a browser to the Python website and download the Windows installer. ...
  2. Double click on the downloaded file and install Python for all users, and ensure that Python is added to your path. ...
  3. After the installation is complete, click Disable path length limit and then Close.
28 Sept 2022

How do I make Python 3.10 default in Linux? ›

How do I make Python 3.10 default in Linux?
  1. Check current version: $ python --version.
  2. get root permissions: $ sudo su.
  3. set python3 as default: $ update-alternatives --install /usr/bin/python python /usr/bin/python3 1.
  4. Done. ( Check by $ python --version)

Can I install PyCharm in WSL? ›

Professional feature: download PyCharm Professional to try. You can use Windows Subsystem for Linux (WSL) to work with a Python interpreter available in your Linux distribution.

Should I install WSL or wsl2? ›

We recommend that you use WSL 2 as it offers faster performance and 100% system call compatibility. However, there are a few specific scenarios where you might prefer using WSL 1. Consider using WSL 1 if: Your project files must be stored in the Windows file system.

How do I fix python3 pip has no installation candidate? ›

How do I fix python3 pip has no installation candidate?
  1. Step 1: Enable the universe repository. The first step is to enable the universe repository.
  2. Step 2: Synchronize package database.
  3. Step 3: Install the pip3 module.

Should I enable virtualization for WSL? ›

While WSL 2 uses Microsoft's Hyper-V as a hypervisor under the hood to run the utility VM, it does not require you to enable Windows' Hyper-V role or feature; WSL works perfectly fine without it.

Do I need to activate VENV? ›

You don't specifically need to activate an environment; activation just prepends the virtual environment's binary directory to your path, so that “python” invokes the virtual environment's Python interpreter and you can run installed scripts without having to use their full path.

How do I enable .venv in Python? ›

Activate the virtual environment
  1. On Unix or MacOS, using the bash shell: source /path/to/venv/bin/activate.
  2. On Unix or MacOS, using the csh shell: source /path/to/venv/bin/activate.csh.
  3. On Unix or MacOS, using the fish shell: source /path/to/venv/bin/activate.fish.
15 Apr 2022

How do I install Python modules in WSL? ›

Setting Up Python 3 in Windows Subsystem for Linux (WSL)

So if you would like to install NumPy, you can do so with the command pip3 install numpy . Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

How do I run Python 3.9 on Linux? ›

How do I install Python 3.9 on Linux?
  1. Step 1: Update apt cache. Fire up the terminal and update the apt cache with the command:
  2. Step 2: Install the dependencies.
  3. Step 3: Add PPA repository.
  4. Step 4: Install Python 3.9.
  5. Step 5: Verify the Python 3.9 installation.

How do I install Python 3.9 7 on Linux? ›

Perform the below-given steps to install Python 3.9 on Linux Mint 20 from the deadsnakes repository:
  1. Step 1: Update apt cache. ...
  2. Step 2: Install the dependencies. ...
  3. Step 3: Add PPA repository. ...
  4. Step 4: Install Python 3.9. ...
  5. Step 5: Verify the Python 3.9 installation.

Is WSL better than dual boot? ›

WSL vs Dual Booting

Dual Booting means installing multiple operating systems on a single computer, and being able to choose which one to boot. This means that you CANNOT run both the OS at the same time. But if you use WSL, you can use both the OS simultaneously without the need to switch the OS.

Should I code in WSL? ›

We do not recommend using WSL in VS Code without the WSL extension as you will lose support for auto-complete, debugging, linting, etc. Fun fact: this WSL extension is installed in $HOME/. vscode/extensions (enter the command ls $HOME\. vscode\extensions\ in PowerShell).

Is WSL faster than a VM? ›

Windows Subsystem for Linux

While WSL 2 actually uses the Linux kernel running under Hyper-V, you won't have as much of a performance hit than with a VM because you aren't running most of the other processes that run on a Linux system. You can run WSL with less memory than you would need for a virtual machine.

How do I add Python 3.8 to PATH in Linux? ›

If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.

How do I install Python 3.9 8 on Ubuntu? ›

How to Install Python 3.9 on Ubuntu 22.04 using APT
  1. Step 1: Update system repositories. ...
  2. Step 2: Install prerequisite package. ...
  3. Step 3: Add “deadsnakes” PPA. ...
  4. Step 4: Python 3.9 installation on Ubuntu 22.04. ...
  5. Enter “y” to permit the installation process to continue:
  6. Step 5: Verify Python version.

How do I install Python Pip on Windows 11? ›

How to Install PIP in Windows Through CMD
  1. Open a browser and head to this web page.
  2. Download the get-pip.py and save it to a folder.
  3. Open your command prompt.
  4. Find the folder that contains the downloaded file.
  5. Run this command: python get-pip.py .
  6. Your program should be ready to use in a few seconds.
12 Sept 2022

How do I install Python 3.9 from command prompt? ›

How do I install Python 3 on Windows command prompt?
  1. Step 1: Select Version of Python to Install.
  2. Step 2: Download Python Executable Installer.
  3. Step 3: Run Executable Installer.
  4. Step 4: Verify Python Was Installed On Windows.
  5. Step 5: Verify Pip Was Installed.
  6. Step 6: Add Python Path to Environment Variables (Optional)

How do I switch from Python 3.9 to 3.8 in Linux? ›

How do I install Python 3.8 on Linux?
  1. Start with the system update.
  2. Add PPA for Python old versions.
  3. Check Python Versions you want.
  4. Install Python 3.9 on Ubuntu 22.04.
  5. Install Python 3.8 on Ubuntu.
  6. Set the default Python version.
  7. Uninstall Python and PPA.

How do I make Python 3.7 default in Linux? ›

How do I switch to Python 3.7 in Ubuntu?
  1. Step 1: Check the current version. $ python3 –version.
  2. Step 2: Update Ubuntu packages. sudo apt-get update.
  3. Step 3: Install python3.
  4. Step 4: Update python 3 to point to python 3.7.

How do I permanently set my Python path in Linux? ›

Setting PYTHONPATH more permanently
  1. Open Terminal.app ;
  2. Open the file ~/.bash_profile in your text editor – e.g. atom ~/.bash_profile ;
  3. Add the following line to the end: export PYTHONPATH="/Users/my_user/code"
  4. Save the file.
  5. Close Terminal.app ;
  6. Start Terminal.app again, to read in the new settings, and type this:

Which Linux is best for WSL? ›

WLinux is a Linux environment for Windows 10 built on work by Microsoft Research and the Debian project. WLinux is a custom Linux distro built from Debian specifically for use on the WSL. While other distros are available for WSL, WLinux is the first optimized for use by users of WSL for WSL.

Can WSL run all Linux apps? ›

This is a specific feature of WSL that allows you to run GUI-based Linux apps, meaning you're not limited to using the Linux terminal for all your tasks. If you want a more user-friendly interface, GUI apps go a long way, and Windows 10 users can finally join in on the fun.

Can WSL run GUI? ›

Windows Subsystem for Linux (WSL) now supports running Linux GUI applications (X11 and Wayland) on Windows in a fully integrated desktop experience. WSL 2 enables Linux GUI applications to feel native and natural to use on Windows.

Is WSL2 slower than Windows? ›

WSL2 uses the 9P protocol to access Windows drives, and it is currently known to be very slow when compared to: Native NTFS (obviously) The ext4 filesystem on the virtual disk used by WSL2. And even the performance of WSL1 with Windows drives.

Does WSL consume RAM? ›

On machines running build 20175 or higher, RAM is limited to either 50% of your total memory or 8GB, whichever happens to be smaller. On builds older than 20175, WSL could consume up to 80% of your host RAM.

Does WSL make Windows slow? ›

essentially NO, but read on to see what we actually looked at with our tests. I have been using a WSL2 a lot when I am working on a Windows 10 system. I find it makes development work MUCH more pleasant.

Does installing Python automatically install pip? ›

PIP is automatically installed with Python 2.7.9+ and Python 3.4+ and it comes with the virtualenv and pyvenv virtual environments.

Why is Python not recognizing pip? ›

A “pip: command not found” error occurs when you fail to properly install the package installer for Python (pip) needed to run Python on your computer. To fix it, you will either need to re-install Python and check the box to add Python to your PATH or install pip on your command line.

Why won't pip install work Python? ›

One of the most common problems with running Python tools like pip is the “not on PATH” error. This means that Python cannot find the tool you're trying to run in your current directory. In most cases, you'll need to navigate to the directory in which the tool is installed before you can run the command to launch it.

Is WSL good for machine learning? ›

Machine learning (ML) is becoming a key part of many development workflows. Whether you're a data scientist, ML engineer, or starting your learning journey with ML the Windows Subsystem for Linux (WSL) offers a great environment to run the most common and popular GPU accelerated ML tools.

Is WSL slower than Linux? ›

WSL is Windows Subsystem for Linux. It is the Linux(since the WSL 2 version, the actual Linux kernel) running inside Windows. Obviously, that can't be faster than the real Linux OS running on bare metal on the same hardware, but it shouldn't be very slow.

What is the difference between env and venv? ›

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.

Do I need to install Python in venv? ›

If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation.

When should I use venv Python? ›

A virtual Environment should be used whenever you work on any Python-based project. It is generally good to have one new virtual environment for every Python-based project you work on. So the dependencies of every project are isolated from the system and each other.

Does venv come with pip? ›

In fact, virtualenv comes with a copy of pip which gets copied into every new environment you create, so virtualenv is really all you need. You can even install it as a separate standalone package (rather than from PyPI).

How do I know if Python is running venv? ›

  1. virtualenv is the standalone project that works on any Python version (github.com/pypa/virtualenv). ...
  2. A nice way to detect from bash using this answer is to run: env |grep VIRTUAL_ENV |wc -l which will return a 1 if in a venv or a 0 if not.

How do I start venv from CMD? ›

Open the Windows Command Prompt enter into your Desktop folder with the command cd desktop . You can find your device's command line interface (CLI) by searching in your applications.> Type py -m venv env to create a virtual environment named env . When the environment is created, the prompt will reappear.

Is it better to run Python on Windows or Linux? ›

Linux is free :) Although there is no visible performance impact or incompatibility when working python cross-platform, the benefits of Linux for python development outweigh Windows by a lot. It's a lot more comfortable and definitely will boost your productivity. ...

Should I code Python on Linux or Windows? ›

Python is cross platform, the developers worked really hard to have it working fine in all platforms. If you like to use Linux and are familiar with the bash commands, like it more than Windows, then enjoy Python, which is native to Linux, update some libraries (install pip, for example) and work from there.

Is WSL faster than Windows? ›

File performance across the Windows and Linux operating systems is faster in WSL 1 than WSL 2, so if you are using Windows applications to access Linux files, you will currently achieve faster performance with WSL 1.

Is Linux better than Windows for Python? ›

👨‍💻 Programming Friendly

Linux supports almost all of the programming languages such as Clojure, Python, Julia, Ruby, C, and C++ to name a few. The Linux terminal is better than Window's command line. If you want to learn command line basics quick and super fast, you will find this course helpful.

Which Python is best for Linux? ›

11 Best Python IDEs for Ubuntu in 2022
  1. Vim. Features: Syntax highlighting. ...
  2. PyCharm. Features: Code auto completion. ...
  3. Eric. Features: Support for extensions and plug-ins. ...
  4. Pyzo. Features: Interactive user interface. ...
  5. Spyder. Features: Community support. ...
  6. GNU Emacs. Features: Highly customizable. ...
  7. Atom. Features: ...
  8. PyDev (Eclipse) Features:

Which OS is best for Python? ›

In general, if you are using standard software packages like JMP and RapidMiner for basic operations like analysis and model creation then go with Windows. However, Python, R, and Octave, the top three programming languages for Machine Learning, run best on Linux-based operating systems.

Why is Linux good for Python? ›

Almost every tutorial on Python use Linux based systems like Ubuntu. These tutorials are by experts so it's good to follow best practices used by experienced developers. When your projects get complex its very easy to find Dependencies for your projects like extensions, libraries etc. in Linux based OS.

Do professionals use Python? ›

Python has become a staple in data science, allowing data analysts and other professionals to use the language to conduct complex statistical calculations, create data visualizations, build machine learning algorithms, manipulate and analyze data, and complete other data-related tasks.

Is Python coding outdated? ›

Python is a versatile, high-level interpreted language that can be used for a variety of tasks. However, its popularity has also led to it being considered slow, inefficient and outdated.

Is VS code enough for Python? ›

One of the coolest code editors available to programmers, Visual Studio Code, is an open-source, extensible, light-weight editor available on all platforms. It's these qualities that make Visual Studio Code from Microsoft very popular, and a great platform for Python development.

Why WSL is too slow? ›

WSL2 uses the 9P protocol to access Windows drives, and it is currently known to be very slow when compared to: Native NTFS (obviously) The ext4 filesystem on the virtual disk used by WSL2. And even the performance of WSL1 with Windows drives.

Do developers use WSL? ›

WSL especially helps web developers and those working with Bash and Linux-first tools (for example, Ruby and Python) to use their tools on Windows and ensure consistency between development and production environments.

Is Python faster on Linux? ›

Python 3 performance is still much faster on Linux than Windows. JavaScript is required to view these results or log-in to Phoronix Premium. If planning to do any web/LAMP development from the budget laptop and testing PHP scripts locally, Ubuntu's PHP7 performance continues running much stronger than Windows 10.

Why do scientists use Linux? ›

Most data science companies use Linux because of the obvious advantages that it provides with analysing data. Most data scientists have their codes developed and deployed on the Linux OS. Having said that, there are also companies that use Windows as their OS so one should be flexible enough to adapt to both OSs.

Is Linux as fast as Windows? ›

Linux has a reputation for being fast and smooth while Windows 10 is known to become slow and slow over time. Linux runs faster than Windows 8.1 and Windows 10 along with a modern desktop environment and qualities of the operating system while Windows is slow on older hardware.

Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5525

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.