Refer: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
Install on Ubuntu 18.04
sudo apt-get install python3-venv
Create a virtual environment
python3 -m venv env
Activating a virtual environment
source env/bin/activate
Confirm you are pointing to your virtual environment
which python
You should seem something like
.../env/bin/python
Leaving the virtual environment
deactivate
Installing packages, when inside virtual environment
python3 -m pip install requests
Installing specific versions
python3 -m pip install requests==2.18.4
Upgrading packages
pip can upgrade packages in-place using the --upgrade flag. For example, to install the latest version of requests and all of its dependencies:
python3 -m pip install --upgrade requests
Using requirements files
Instead of installing packages individually, pip allows you to declare all dependencies in a Requirements File. For example you could create a requirements.txt file containing:
requests==2.18.4
google-auth==1.1.0
Tell pip to install all the packages in this file using the -r flag:
python3 -m pip install -r requirements.txt
Freezing dependencies
Pip can export a list of all installed packages and their versions using the freeze command:
python3 -m pip freeze
Which will output a list of package specifiers such as:
cachetools==2.0.1
certifi==2017.7.27.1
chardet==3.0.4
google-auth==1.1.1
idna==2.6
pyasn1==0.3.6
pyasn1-modules==0.1.4
requests==2.18.4
rsa==3.4.2
six==1.11.0
urllib3==1.22
This is useful for creating Requirements Files that can re-create the exact versions of all packages installed in an environment.