Use different Python 3.x with virtualenv


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)


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

[[email protected] prometheus-anomaly-detector]$ source ~/Environments/v38/bin/activate
(v38) [[email protected] prometheus-anomaly-detector]$ which python
# /u01/userhome/oracle/Environments/v38/bin/python
(v38) [[email protected] prometheus-anomaly-detector]$ which pip
# /u01/userhome/oracle/Environments/v38/bin/pip
(v38) [[email protected] 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) [[email protected] prometheus-anomaly-detector]$ python --version
# Python 3.8.3

Deactivate a virtual environment with deactivate

(v38) [[email protected] prometheus-anomaly-detector]$ deactivate 
[[email protected] prometheus-anomaly-detector]$ 

Leave a Reply

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