check virtual environment python code example

Example 1: python virtual environment

python3 -m venv env
python -m virtualenv env #py2

source env/bin/activate

#all this is on same directory

Example 2: python create virtualenv

pip install virtualenv # install first
cd projectfolder # go to project folder
python -m venv ./venv # Create a virtual environment named venv
Activate.ps1 # (powershell) start the file  to start the environment
activate.bat # (cmd) start the file  to start the environment
# if it worked you'll see a (venv) in front of your cursor path

Example 3: python virtual env

$ virtualenv --version

Example 4: how to check the existing virtual environments

lsvirtualenv -l

Example 5: python virtual env

$ virtualenv -p /usr/bin/python2.7 venv

Example 6: how to know if i'm in a virtual environment

import sys
sys.prefix == sys.base_prefix
# if true, you are not in a virtual env