mkvirtualenv python code example

Example 1: virtual env create python

#------FOR LINUX/MAC---------#
#installing venv 
sudo apt-get install python3.6-venv
#creating virtual env
python3 -m venv env
#activating virtual env
source env/bin/activate


#-------FOR WINDOWS----------#
#installing venv
py -m pip install --user virtualenv
#creating virtual env
py -m venv env
#activating virtual env
.\env\Scripts\activate

Example 2: how to use virtualenvwrapper

# install Virtual Environment alternative to virtualenv
pip install virtualenvwrapper

# modify your .profile file
# find out where virtualenvwrapper.sh file is by issuing which command
# source it as shown below
source /usr/local/bin/virtualenvwrapper.sh

# setup an environment
mkvirtualenv dev_env1
mkvirtualenv dev_env2
mkvirtualenv dev_env3
mkvirtualenv dev_env4

# create list of your projects, project1...projectN
mkproject project1
mkproject -p python3 project2  # this project is for python 3 only
mkproject project3
mkproject projectN

# go to the folder where you have cloned an existing repo
# and run the command to bind that project to this environment
setvirtualenvproject

# get list of all environments
workon

# choose a project to work on based on previous output
# say you have dev_env1, dev_env2, dev_env3... dev_envN
# state the name of the project to work on 
workon dev_env1

# other tasks
# remove un-needed environments
rmvirtualenv dev_env4

# build a temp environment if needed
mktempenv

# .profile needs this variables set for export
# i.e. export WORKON_HOME="/home/rajib2k5/envs"
#      export PROJECT_HOME="/home/rajib2k5/dev"

# in order to leave an environment, run the command below 
deactivate

Example 3: python createvirtual env

python3 -m venv tutorial-env

Example 4: python virtual env

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

Example 5: activate environment in virtualenvwrapper

workon [<name>]