Python – Virtualenvwrapper
In this tutorial you will learn python in detail.
Python virtualenv is a good choice of virtual environment where it creates isolated python environments. An extension of virtualenv is virtualenvwrapper and it is even better choice.
The extensions include wrappers for creating and deleting virtual environments and managing development workflow. This makes more easier to work on more than one project at a time without introducing conflicts in their dependencies.
Features of Virtualenvwrapper
- All of your virtual environments are organised in one place.
- Managing your virtual environments using wrappers.
- Only one single command to switch between environments.
To start with it download the wrapper with pip :
$ pip install virtualenvwrapper
After installing you can simply run the following command to know the exact location of virtualenvwrapper.sh.
$ which virtualenvwrapper.sh
Now you need to activate its shell functions by running source on the installed virtualenvwrapper.sh script. By using that path, add below three lines to your shell’s startup file. These commands are executed only when you log in or open a new shell:
export WORKON_HOME=$HOME/.virtualenvs # Optional
export PROJECT_HOME=$HOME/projects # Optional
Now reload the startup file:
$ source ~/.bashrc
Now there will be a directory located at $WORKON_HOME that contains all of the virtualenvwrapper data/files:
$ echo $WORKON_HOME
There are few commands like workon, deactivate, mkvirtualenv, cdvirtualenv, rmvirtualenv used to manage the environments.
If you want to start a new project, you just have to do this:
$ mkvirtualenv my-new-project
The above command will create and activate new environment in the directory located at $WORKON_HOME, where all virtualenvwrapper environments are stored.
You can deactivate it by using stop command:
(my-new-project) $ deactivate
With the use of workon function you can list many environments available:
To activate a particular environment, use below command:
$ workon web-scraper
In order to switch between Python versions and would like to use a single tool, virtualenv will allow to do that. The parameter -p allows you to select which Python version you want to use. For example, if you want to use Python3 version give below command:
$ virtualenv -p $(which python3) blog_virtualenv
Above command creates a new Python3 environment. The which command finds your $PATH variable and returns the full path to that command. Hence the full path was returned to python3, PYTHON_EXE will be taken by -p parameter. You can also use the same for python2 as well. Just substitute the required version of python.