How to set up a steady Python environment on Windows with the least effort

“Let me just code”. Every time I work on python on Windows, I always get frustrated by some infrastructure work that I dislike.

Yes. There are online platforms like Google Colab and Jupyter Notebook where I can exude machine learning, deep learning, or some sort of analysis-related code.

However, they did not satisfy my needs like implementing a python automation script to shorten my routine work.

Not only that. You can skip the following bullet points; the summary of what I was whining over.

  • I did not want to work on Windows as the base OS because I am used to doing all the setups on Linux/macOS. I said bye to my macOS a month ago after I quit this venture company. It was fun for me to do all the work on macOS although some criticize and brag about how it is an inferior version of Linux.
  • I was lazy enough to install docker or vagrant and struggle with the configuration.
  • I did not intend to deploy my code on Clouds like AWS or Heroku.
  • I did not want to get surprised and struggle when I figure out that my code is not running after a while (That happens if you do all the installation on my local!).
  • I did not want to encounter version mismatches across multiple libraries.

What I wanted was a simple, isolated, and easy-to-maintain python environment where I can comfortably do coding without any hassles.

What is not covered in this article:

  • How to install Ubuntu on Windows
  • If you don’t have one, check here

What is covered:

  • How to install python on Ubuntu
  • How to install the virtual environment (virtualenv)
  • How to install pip and control dependency versions

Let’s begin.

Firstly, open Ubuntu on Windows and access my project folder

cd /mnt/c/Users/norif/Projects

Check the version of python3.

python3 ––version

If python is not there or obsolete (less than 3.8), update the advanced package tool (APT). Then, install python.

apt-get update
sudo apt install python3.8

Make sure pip3 is installed.

pip3 --version
sudo apt install python3-pip

Check if pip is runnable through python3:

python3 -m pip --version
//This should result in the followings:
//pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

By adding the -m option, python will look for sys.path and execute it.

As pip is ready, install virtual env.

python3 -m pip install --user virtualenv

Create the virtual environment by running the following command:

python3 -m virtualenv env

The result should look like this:

Upon the success of the virtual environment creation, you should be able to see /env under where you are.

To spin up the virtual environment, run the code:

source env/bin/activate

To see if the virtual environment is activated, check where the python interpreter is by running:

which python3

Mine looked like below:

You may check what is installed by:

python3 -m pip list

The result looks like below:

Package    Version
---------- -------
pip 21.1.1
setuptools 56.0.0
wheel 0.36.2

Not many libraries are there at the beginning of course.

Let’s download one of the most common and used libraries requests first.

python3 -m pip install requests

Now, the result of python3 -m pip list looks fancier.

But they are not enough, right? NumPy, Pandas, TensorFlow, Matplotlib, you name it.

Installing each library is a pain as every time you create a virtual environment for a project, you need to install whatever you want.

Also, not keeping the version of each installed library would result in dependency mismatches.

So, in order not only to cut short the manual installation command execution but also to prevent from encountering another hassle due to version mismatches, it is always wise to use requirement.txt.

According to the official pip documentation, dump the libraries you have currently. The command is:

python3 -m pip freeze > requirements.txt

Make sure requirements.txt is generated under your location (You should be at your project folder if you have been with me).

Opening requirements.txt , I saw the file look like below:

certifi==2020.12.5
chardet==4.0.0
idna==2.10
requests==2.25.1
urllib3==1.26.4

When I want to use numpy.broadcast_shapes available from

Numpy Version 1.20.0, I should specify the NumPy version in requirements.txt by adding the following line in the file:

numpy>=1.20.0

And, run the following command so that the libraries specified inrequirements.txt will be all updated.

python3 -m pip install -r requirements.txt

Upon the command execution, you will see NumPy with the proper version be downloaded.

Practicing this environment configuration, you will hardly mess up with the library dependencies and your local environment.

Even if you switch to macOS, it is easy to translate over your work.

This was edited on 6th May 2021. Whatever written above can be obsolete when you read it.

Please check the latest approaches to simulate the same by referring to the official websites of python and pip as below:

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

https://pip.pypa.io/en/stable/user_guide/#requirements-files

I write about programming, data analysis, design, stock investment and things around me. I was born and raised in Tokyo and now residing in Kuala Lumpur.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store