January 4, 2021 - coding python

How to set up a Python Development Environment Setup on Windows (with WSL)

Xavier Geerinck

@XavierGeerinck

Setting up a Python Development environment on Windows might be a daunting task to do right. Especially since a lot of tools cause confusion or might not work out of the box.

If you have ever worked with Python before on windows, you will know that it might not be the best choice due to incompatible toolings, access violations, ...

In a previous article, I have already explained how I set-up my environment to run the OpenAI Gym, but I wanted to revisit this for my new workflow to make it easier and more versatile.

So let's get started!

Installing Linux on Windows through WSL

The first thing to do is to install the Windows Subsystem for Linux. Since many libraries in python are not working out of the box, it's best to always run everything through WSL.

The Windows Subsystem for Linux (WSL) was introduced not that long ago, but became more useful since its latest release (WSL 2) in Windows 10 version 2004.

💡 In more technical terms, the biggest change is that WSL 1 used syscall translations while WSL 2 is a native VM

By default, WSL installs straight on your C drive. However for disk space requirements it might be handier to install this on a separate disk (e.g. E:\).

Execute the following in PowerShell and adapt the drive location to install it to the location of your choice.

# Navigate to the destination directory
cd E:\WSLTest
# Enable WSL
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
# Downloading Ubuntu 20.04
Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing
# Unpack
move .\Ubuntu.appx .\Ubuntu.zip
Expand-Archive .\Ubuntu.zip
# Install
.\Ubuntu\ubuntu2004.exe
# Verify
wsl --list --all

Installing Python and PyEnv

Once the Linux Subsystem is set-up, open it by clicking the .exe file in the created folder (e.g. ubuntu2004.exe). Easiest is to now create a shortcut of it on your taskbar. We should now see something like the screenshot below:

To manage our Python environment, I chose to utilize pyenv which allows us to manage the installed Python version.

💡 The reason we are using a Python Version Manager is because a lot of tools do not work in certain versions of Python (e.g. Reinforcement Learning often requires Python <= 3.7)

So lets install PyEnv and Python 3.8.6!

Installing PyEnv

# Update
sudo apt update -y
# Install dependencies
# https://github.com/pyenv/pyenv/wiki#suggested-build-environment
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
# Clone pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# Configure the environment
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
# Restart the shell (to enable pyenv)
exec "$SHELL"
# Verify installation
pyenv install --list

Installing Python 3.8.6

Now PyEnv is installed, we can start managing our python installation.

# Show available versions
pyenv install --list
# Install version 3.8.6
pyenv install 3.8.6
# Verify installation
pyenv versions
# Configure the global python command to utilize 3.8.6
pyenv global 3.8.6
# Check version
python --version

Configuring Python Tooling

Opening our Code Editor - VSCode

We are now set to start programming in Python! However, as in many cases we would like to fix our IDE (Integrated Development Environment = Code Editor) to work with the installed Python version.

Luckily for us, Microsoft ships Visual Studio Code together with its WSL images. So when we run code . we will open up a new VSCode instance straight on the path that we executed it from.

Let's thus create an example folder and run the command above in it to start-up VSCode:

mkdir example
cd example/
code .

Which will show

Creating Jupyter Notebooks

Once VSCode is opened we can create Jupyter Notebooks by installing the Notebook Extension. So in VSCode, search after jupyter to find and install the Jupyter Notebook extension.

Then do the same for the Python extension.

If this is done, we are now able to create a Notebook and run a simple command in it!

Forwarding the Windows Display

Finally, it is interesting for graphical reasons to be able to output a Python GUI to windows when being rendered.

For this we can utilize a tool named Xming, so download and install this.

After installing Xming, go into the Program Files folder and execute launch.exe to configure it as follows.

When it is configured, execute the following to add it to your environment.

# Export our display settings for XMing
export DISPLAY=$(ip route | awk '/^default/{print $3; exit}'):0

Conclusion

Congratulations! You are now ready to start developing with Python and easily manage the versions. Feel free to let me know in the comments below if this was interesting for you and how it helped you!

Did you enjoy reading? Or do you want to stay up-to-date of new Articles?

Consider sponsoring me or providing feedback so I can continue creating high-quality articles!

Xavier Geerinck © 2020

Twitter - LinkedIn