Python and virtualenv on Windows

A friend of mine was recently getting into Python development and mentioned he was having some issues setting up the basic environment. As I was going through the process of writing up a quick and dirty installation guide I decided it would also make for a good first post on this blog. The goal of this quick how-to is to provide instructions on installing Python, setuptools (easy_install and pip), and virtualenv. From that point we will create our first virtual environment and install the popular “requests” package to demonstrate the utility of using a virtual environment. Please bare with me as I get used to WordPress markup :P.

Unless otherwise stated all the following commands should take place in the Windows Command Prompt (Press the Windows key, type cmd and press enter)

1) Install a fresh copy of Python to “C:toolspython“.

The Python installer found here: https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi

Many people have noticed that the Python installer does not add or modify any environment variables of the system. The reason for this is because the installer doesn’t assume to know which instance of the Python executable you want to use. When we create a virtual environment (using virtualenv) the reason why will become apparent but for the time being just keep in mind that you do not want to have the base Python executable on your system path.

2) Create the directory “setuptools” in your Python installation

3) Download and place the setuptools installation script (ez_setup.py) into “C:toolspythonsetuptools“.

The setuptools installation script  can be downloaded here: https://bootstrap.pypa.io/ez_setup.py

4) Run the the setuptools installation script to install the utilities easy_install and pip into your base Python environment.

When the installation is complete there should be a directory located at  “C:toolspythonScripts” which contains the aforementioned utilities.

5) Make sure that pip installed successfully by executing its version function

6) Use pip to install virtualenv to your base Python environment. Make sure virtualenv installed correctly by executing its version function.

virtualenv creates a sandboxed environment so that when you install python packages they are installed into the sandbox instead of your base Python environment. This allows you to keep your base environment clean of dependencies belonging to individual projects.

7) Create the directory “C:toolspythonenvironments“. Navigate to this directory and create a new virtual environment with name “scratchpad”.

This should create the directory “C:toolspythonenvironmentsscratchpad“. If you look into the “Scripts” directory you will notice the python, easy_install, and pip exectuables. When virtualenv creates a new environment it copies the base executables into its own directory structure as if it was its own installation of Python. This new directory structure allows Python packages to be installed to just the virtual environment and not the base environment. Also, with virtual environments there is a notion of activating the environment and deactivating the environment. In this directory you will see a batch (.bat) script (a script that can be executed by windows natively) that “activates” the environment. When an environment is active, the python executable for the virtual environment is added to the system path and removed when the virtual environment is deactivated. As mentioned before, this is the reason why the Python installer does not place the base python executable on your system path; if it was it would conflict with the python executable of the virtual environment when the virtual environment is activated.

8) To activate the environment use the activate script. Your prompt should look like: (scratchpad) C:toolspythonenvironmentsscratchpadScripts

It really doesn’t matter what directory you are in right now, the (scratchpad) designator is telling you you are inside a virtual environment and that’s the important part

9) Make sure you can “deactivate” the virtual environment by entering “deactivate” in the Command Prompt. This should remove the (scratchpad) designator from your prompt.

At this point Python, setuptools, and virtualenv have all been installed. Now lets say you are working on a project that requires a specific version of the package “requests”. In order to keep your base installation of Python clean from any specific version of a package you want to make use of a virtual environment. For this exercise we will use the environment we created previously.

10) Navigate to the scratchpad directory, activate the environment, and install the requests package

11) After installation is complete test out the requests package in the python interpreter.