Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Python Tutorials: virtualenv

Virtual environment manager

Each integration has its own set of dependencies that must be added to Python in order to run the tests, or just to try out the collection code. To avoid polluting your Python installation with libraries and packages that would only be used by an Integration, use a “virtual environment”. A virtual environment is a self contained directory tree that contains an isolated Python installation. When a virtual environment is active, any package you install goes into that directory without affecting the system wide Python installation.

Virtualenv and virtualenvwrapper

Datadog recommends using Virtualenv to manage Python virtual environments, and virtualenvwrapper to make the process smoother. There’s a comprehensive guide in the Hitchhiker’s Guide to Python describing how to set up these two tools

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.

It can be used standalone, in place of Pipenv.

Install virtualenv via pip:

$ pip install virtualenv

Test your installation:

$ virtualenv --version

Basic Usage

  1. Create a virtual environment for a project:
$ cd project_folder
$ virtualenv venv

virtualenv venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv) can be anything; omitting the name will place the files in the current directory instead.

Note

‘venv’ is the general convention used globally. As it is readily available in ignore files (eg: .gitignore’)

This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.

You can also use the Python interpreter of your choice (like python2.7).

$ virtualenv -p /usr/bin/python2.7 venv

or change the interpreter globally with an env variable in ~/.bashrc:

$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
  1. To begin using the virtual environment, it needs to be activated:
$ source venv/bin/activate

The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command.

Assuming that you are in your project directory:

C:\Users\SomeUser\project_folder> venv\Scripts\activate

Install packages using the pip command:

$ pip install requests
  1. If you are done working in the virtual environment for the moment, you can deactivate it:
$ deactivate

This puts you back to the system’s default Python interpreter with all its installed libraries.

To delete a virtual environment, just delete its folder. (In this case, it would be rm -rf venv.)

After a while, though, you might end up with a lot of virtual environments littered across your system, and it’s possible you’ll forget their names or where they were placed.

Installation


python -m pip install --user virtualenv
python -m virtualenv --help

Linux

  • installations from python.org
  • Ubuntu 16.04+ (both upstream and deadsnakes builds)
  • Fedora
  • RHEL and CentOS
  • OpenSuse
  • Arch Linux

macOS

In case of macOS we support:

  • installations from python.org
  • python versions installed via brew (both older python2.7 and python3)
  • Python 3 part of XCode (Python framework – /Library/Frameworks/Python3.framework/)
  • Python 2 part of the OS (/System/Library/Frameworks/Python.framework/Versions/)

Windows

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x