Create a Python Virtual Environment on Ubuntu 16.10

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Deprecated

This guide has been deprecated and is no longer being maintained.

Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

What is a Python Virtual Environment?

A Python Virtual Environment - or virtualenv - is a tool to create an isolated Python environment on your Linode. This can be extremely powerful as you can create a virtual environment and install all Python executables/packages to it, leaving no dependencies outside of your created virtual environment.

The purpose of this tutorial is to allow you to create and run Python virtual environments in your Ubuntu 16.10 Linode.

Before You Begin

  1. This guide uses sudo wherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.

  2. Update your system:

    sudo apt update
    

Install Python Virtualenv

  1. To install Python’s virtual environment:

    sudo apt install virtualenv
    

Create New Directory in Home

  1. Navigate to your users home directory:

    cd
    
  2. Create a directory named python-environments:

    mkdir python-environments
    
  3. Navigate in the newly created directory:

    cd python-environments
    

Create a Virtual Environment in Python 3

  1. Create a virtual environment in Python 3 with the environment name of env:

    virtualenv -p python3 env
    
  2. Validate that environment is installed with python3:

    ls env/lib
    

Activate Environment

Activate the newly created virtual environment (the name of the working environment appears in parentheses):

source env/bin/activate
(env) testuser@localhost:~/python-environments$

Now that the environment is active, you can install executables and packages only to this virtual environment.

Deactivate Environment

To deactivate an active virtual environment:

deactivate

Congratulations! You have created an isolated, Python Virtualenv on your Linode.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.