Complete the Getting Started and Securing Your Server guides to prepare your system.
Update your system:
sudo apt-get update && sudo apt-get upgrade
{{< 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. {{< /note >}}
{{< note >}} By default, Python 3.7.3 and Python 2.7.16 are installed on Debian 10. {{}}
Install the virtualenv tool using your package manager:
sudo apt install virtualenv
Create a python-environments
directory in your user's home directory and navigate to it:
mkdir ~/python-environments && cd ~/python-environments
Create a Python virtual environment. By default, virtualenv attempts to use the Python 2.5 interpreter to create a new environment. Replace env
with the name you would like to assign to your virtual environment.
virtualenv env
{{< disclosure-note "Change the Python Interpreter Version">}} If you would like to create a virtual environment using Python3, use the --python
option to pass the Python version you'd like to use.
virtualenv --python=python3 env {{}}
The command creates a new directory with the name you assigned to your virtual environment. This directory contains all of the isolated files, packages, modules, and executables that is used by your new environment.
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:
{{< output >}} python3.6 {{}}
Activate the newly created virtual environment:
source env/bin/activate
The name of the working environment appears in parentheses after it's created.
{{< output >}} (env) example_user@hostname:~/python-environments$ {{}}
You can now begin installing Python packages and libraries that will remain isolated to your virtual environment.
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
{{< output >}} example_user@hostname:~/python-environments$ {{}}