Creating a single Python virtual environment


Install python3, python3-dev, and python3-pip.

Virtual environment

First, install virtualenv:

  sudo -H pip3 install -U virtualenv
  # H sets the HOME environment variable to the home directory

To activate the new virtual environment, create (or navigate) a project directory, and call the virtualenv command:

  mkdir MyPythonApp
  cd MyPythonApp
  virtualenv CustomPythonEnv

Now, activate the virtual environment using:

  # The script requires BASH
  source CustomPythonEnv/bin/activate

The command prompt should now contain (venv)

Keep it clean

Virtual environments are a good way to keep the environment clean, making it easy to track installed packages. If you're developing, you may want to keep your packages on the same version. To do that, you can:

  pip3 freeze > requirements.txt

That command will also create the requirements.txt file, that will contain a list of the packages installed and their versions. Now, if you need to replicate that same virtual environment, you can simply:

  pip3 install -r requirements.txt


After you're done, deactivate the virtual environment, simply calling: