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:
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.
python3-venvfor creating virtual environment
sudo apt install python3.8-venv
Create virtual environment named
venvin the project folder
python3.8 -m venv 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.
python3.7-venvspecific 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.8etc. Follow the instructions above.
- Activate and deactivate the desired virtual environment.