There are multiple ways of installing Python, creating and switching between different virtual environments. Also, Python’s package manager hyperspace is a mess. So, things can quickly get out of hands while dealing with projects that require quick environment switching across multiple versions of Python. I use Debian linux in my primary development environment and this is how I keep the option explosion in check:

Installing Python

Run the following commands one by one:

# update the packages list and install the prerequisites
sudo apt update
sudo apt install software-properties-common

# add deadsnakes ppa to your sources' list (When prompted press Enter to continue)
sudo add-apt-repository ppa:deadsnakes/ppa

# install python3.7
sudo apt install python3.8

# verify python installation
python3.8 --version

Creating Virtual Environment

There are multiple ways creating and switching between different environments can be done. I use venv for creating virtual environments. For demonstration, here I’m creating a virtual environment that uses python3.8.

  • Install python3-venv for creating virtual environment
    sudo apt install python3.8-venv
  • Create virtual environment named venv in the project folder

     python3.8 -m venv venv
  • Activate venv

     source venv/bin/activate
  • Deactivate venv

Switching Between Different Environments

To create another environment with a different python version, you have to:

  • Install the desired version of python following the procedures stated above.
  • Install python3.7-venv specific for your python version, like if you are using python3.7, you should run:

    sudo apt install python3.7-venv
  • Create multiple environments with multiple versions and name them distinctively. i.e. venv3.7, venv3.8 etc. Follow the instructions above.
  • Activate and deactivate the desired virtual environment.

Package Management

  • For local development, I use pip.
  • For production application and libraries poetry is preferred.