Python setup on the Windows subsystem for Linux (WSL)

Rodrigo Hernández Mota
3 min readJul 16, 2018

--

The new introduction of the Windows subsystem for Linux allows developers to have full access to a Linux distribution within Windows and without the overhead of a virtual-box. This enhances the Windows experience significantly.

Some restrictions

Windows 10 is required

You can install the most common Linux distributions easily by following the official documentation. The catch; this feature only works in Windows 10.

No GUI (graphic user interface)

I wanted to assume that most Linux user wont see this as a restriction. However, it’s worth mentioning for the user-base dependent on GUIs.

Interoperability

It is well known that Windows and Linux filesystems can be incompatible. Even though the WSL has accomplish an incredible achievement by integrating a Linux subsystem into Windows, you still can’t modify Linux (WSL) files using Windows tools as explained in this post. Nonetheless, as a workaround, you can modify windows files and invoke windows applications from Linux (WSL).

You can access your windows files at /mnt/c/Users/<user-name> from the WSL. You might want to consider adding export WINHOME="/mnt/c/Users/<user-name>" in your bash profile to facilitate access.

Python Dev on the WSL

From now on, I’ll be using the Ubuntu 16.04 flavor of the WSL.

To install python run the following at the WSL:

sudo apt update && upgradesudo apt install python3 python3-pip ipython3

Hard-core developers might use vim (sudo apt install vim) to create python scripts. This might suffice in many cases. Nonetheless, using an editor or IDE can speed-up the development cycle when working on big projects. Furthermore, if you plan to do data science or machine learning; the jupyter-notebooks might come handy.

I’ll show the procedure for installing Atom (editor), PyCharm (IDE) and Jupyter (notebook-based) and how to use them from WSL. Any other software might follow a similar pattern.

Installing Atom

Note: you can only use and invoke Atom for the files in the Windows filesystem (also accessible form the WSL at /mnt/c/Users/<user-name>).

  1. Download Atom from the official website in Windows.
  2. Add Atom to the windows environment variables.
    - Run the following: win+rand type in SystemPropertiesAdvanced.exe
    - Open: Environment Variables
    - Add your Atom path:
    C:\Users\<user-name>\AppData\Local\atom\bin
  3. Go into the WSL and add an alias for Atom in your bashrc file:
    - Open your bash configuration: vim ~/.bashrc
    - Add to the end of the file and save/exit:
    alias atom=”/mnt/c/Windows/System32/cmd.exe /c 'atom'"
    - Update your bash profile: source ~/.bashrc
  4. Now you can use atom . & to open your python projects from WSL command line.

Installing PyCharm

Note: you can only use and invoke PyCharm for the files in the Windows filesystem (also accessible form the WSL at /mnt/c/Users/<user-name>).

  1. [Recommended] Download Jetbrains Toolbox to install PyCharm. In order to enable interactive coding you should also have python installed in Windows.
  2. Create the alias to launch pycharm from the WSL.
    - Open your bash configuration: vim ~/.bashrc
    - Add to the end of the file and save/exit:
    alias charm="/mnt/c/Users/<user-name>/AppData/Local/JetBrains/Toolbox/apps/PyCharm-P/ch-0/<version>/bin/pycharm64.exe"
    - Update your bash profile: source ~/.bashrc
  3. Now you can use charm . & to open PyCharm projects from WSL.

I strongly recommend the use of virtualenv for both Linux and Windows. By using virtualenv you can maintain the same python libraries and versions among the projects at WSL and in the PyCharm IDE at Windows.

Installing Jupyter Notebook

You can run the jupyter notebook from anywhere (i.e., from the Linux or Windows filesystem). The WSL will act as a jupyter server accessible at localhost with port 8888.

  1. Install jupyter: pip3 install jupyter
  2. Create alias to launch jupyter without browser from the WSL:
    - Open your bash configuration: vim ~/.bashrc
    - Add to the end of the file and save/exit:
    alias jupyter-notebook="~/.local/bin/jupyter-notebook --no-browser"
    - Update your bash profile: source ~/.bashrc
  3. Now you can run a jupyter server jupyter-notebook and access the service with your browser from Windowslocalhost:8888.

With this guide and initial setup you can now aim to have the best from Linux and Windows worlds for python development.

Happy coding!

--

--