Creating a Python Virtual Environment on CentOS 8

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.
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 is an isolated project space on your system that contains its own Python executable, packages, and modules. Your Python applications and projects often have their own specific dependencies. With a virtual environment you can manage each of your project’s distinct dependencies without having them interfere with each other. You can use the virtualenv tool to create a virtual environment on your system. This guide will show you how to use virtualenv to create and run a Python virtual environment on a CentOS 8 Linode.

Before You Begin

  1. Complete the Getting Started and Securing Your Server guides to prepare your system.

  2. Update your system:

    sudo yum update
    
    Note
    This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Create a Python Virtual Environment

Note
CentOS 8 does not include any version of Python by default. To install Python on CentOS 8, read our guide on installing Python 3 on CentOS 8
  1. To install Python’s virtual environment:

    sudo yum install virtualenv
    
  2. Create a python-environments directory in your user’s home directory and navigate to it:

    mkdir ~/python-environments && cd ~/python-environments
    
  3. Create a Python virtual environment. By default, virtualenv attempts to use your system’s default Python interpreter to create a new environment. Replace env with the name you would like to assign to your virtual environment.

     virtualenv env
    
    Note

    If your CentOS 8 system has another version of Python installed and you’d like to use it to create your virtual environment, use the e--python option to designate it. For example:

    virtualenv --python=python2.7 env
    
  4. Validate that your environment is installed with the version of Python that you expect:

    ls env/lib
    

    You should see your env environments Python version:

    python3.6.8
        

Activate Your Virtual Environment

  1. Activate the newly created virtual environment:

     source env/bin/activate
    

    The name of the working environment appears in parentheses after it’s created.

    (env) example_user@hostname:~/python-environments$
          

    You can now begin installing Python packages and libraries that will remain isolated to your virtual environment.

Deactivate a Virtual Environment

  1. To deactivate an active virtual environment, issue the following command:

     deactivate
    

    Your virtual environment is deactivated and you should no longer see its name listed next to your command line’s prompt

    example_user@hostname:~/python-environments$
        

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.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.