Virtual Environments Draft

Posted by Geoff, Published: 5 years ago (Updated: 4 years, 11 months ago)

Virtual environments allow the users to create isolated Python environments. Virtual environments will not only allow you experiment with varying versions of a python package, but you can also have environments that use a different Python version from the host (i.e. you currently have Python 3.7 installed, but you are required to use Python 2.7 for a work project. You can simply create a Python 2.7 virtual environment, and activate it every time you need to use it for work).

Real World Example

As an example you are creating two separate websites. Website #1 is a personal blog that you are starting from scratch. You are going to use the Django framework to create this blog, and thus you decide to use the latest version of Django (2.X). Website #2 is a website that you are consulting on, and it was created using an older version of Django (Django 1.8 for example). The owners of Website #2 don't feel the need to update to the latest version, and numerous changes have been made since the jump of 1.X to 2.X. In this example, There will be many packages that you will need to install for Website #1 that are incompatible with Website #2, thus you need two separate environments where these websites can operate. Without a virtual environment you cannot switch seamlessly from one package version to another, so every time you decide to switch your attention between these two projects, you would have to re-install the package version that is required for the given website.

Using Virtual Environments

Install VirtualEnv

The python package we use to create our virtual environments is called virtualenv. You can install it simply by typing the following line into the Command Prompt (CMD) / Terminal:

pip install virtualenv

Create Virtual Environment

Now to make a virtual environment you can simply type the following in the CMD / Terminal:

virtualenv [environment_name]

Where you replace [environment_name] with an appropriate name for your environment. By default, this command will create a folder named [environment_name] containing a fresh version of Python using whichever version of Python the host has. 

Activate Virtual Environment

Before being able to use this fresh version of Python you will have to activate the environment. On Windows you need to run the activate Batch file located in the following directory: \path\to\[environment_name]\Scripts. Do this by running the following command:

path\to\[environment_name]\Scripts\activate

For Mac/Linux you will likely need to use the following command:

source ~/path/to/[environment_name]/bin/activate

Now that your Virtual Environment is activated, you will be able to install Python modules as you would normally do using pip. However, instead of downloading these packages to your host's Python packages location, it will install it to this Virtual Environment.

Deactivate Virtual Environment

To deactivate it simply type the following command:

deactivate

Using Virtualenvwrapper or Virtualenvwrapper-win

Activating the Virtual Environment might seem tedious, therefore I highly suggest you utilize virtualenvwrapper (Mac/Linux) or virtualenvwrapper-win (Windows). 

Windows Installation

Install using the following command:

pip install virtualenvwrapper-win

For Windows you need to create an Environment Variable called "WORKON_HOME" before you can use virtualenvwrapper-win. To do this go to Control Panel > System > Advanced System Settings > Environment Variables > New. Name the variable WORKON_HOME, and then put the path to where you will be storing all your virtual environments.

Now to activate your Virtual Environment you simply type the following command:

workon [environment_name]

Note: for the above command to work, the environment will have to be located within that WORKON_HOME folder. So when creating virtual environments from now on you can perform the following command:

cd %WORKON_HOME%
virtualenv [environment_name]
workon [environment_name]

Mac/Linux Installation

Install using the following command:

pip install virtualenvwrapper

For Mac/Linux you need to add a few lines to your shell initialization script (~./bashrc) before you can use virtualenvwrapper:

echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/[python_folder]" >> ~/.bashrc 
echo "export WORKON_HOME=~/ENV" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

If you are using Python 3, [python_folder] will likely be python3. What those commands do is determine which Python to use, determine which directory to search for the virtual environments in, and initiate a shell script from virtualenvwrapper.

Now you can type the following command to complete the setup for virtualenvwrapper:

source ~/.bashrc

You should be able to see an Env directory in your home folder now. This folder will be the directory that contains all of your environments.

Now to activate your Virtual Environment you simply type the following command:

workon [environment_name]

Comments

Post Comment