Use different Python 3.x with virtualenv

Why?

for each project, it may have different dependencies, if we upgrade global, some projects may not work.

  • each virtual environments may has a different python version, different python index package (pip)

Prerequisite

install virtualenv with pip: pip install virtualenv

Create a virtualenv, assume all environments stored in ~/Environments

cd ~/Environments
virtualenv --python=/usr/local/bin/python3.8 v38
virtualenv --python=/usr/local/bin/python2.7 v2

Activate a virtual environment: source ~/Environments/v38/bin/activate
Example:

[oracle@localhost prometheus-anomaly-detector]$ source ~/Environments/v38/bin/activate
(v38) [oracle@localhost prometheus-anomaly-detector]$ which python
# /u01/userhome/oracle/Environments/v38/bin/python
(v38) [oracle@localhost prometheus-anomaly-detector]$ which pip
# /u01/userhome/oracle/Environments/v38/bin/pip
(v38) [oracle@localhost prometheus-anomaly-detector]$ pip --version
# pip 20.2.4 from /u01/userhome/oracle/Environments/v38/lib/python3.8/site-packages/pip (python 3.8)
(v38) [oracle@localhost prometheus-anomaly-detector]$ python --version
# Python 3.8.3

Deactivate a virtual environment with deactivate

(v38) [oracle@localhost prometheus-anomaly-detector]$ deactivate 
[oracle@localhost prometheus-anomaly-detector]$ 

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.