As the name suggests, it is a Python environment which is isolated from other python environments. This kind of environment is really handy when you are working on multiple projects but you don’t want to affect other project environments.
Advantages of virtual environment.
- Isolated environment:
It creates a self-contained Python instance. Whatever Python packages are installed those get installed in the same instance and doesn’t affect the global python environment.
- Easy to manage multiple python environments:
Because every environment is isolated from other environments, its really easy to manage multiple environments.
- Root privilege not required:
To install new python library you don’t need root privilege, which is good because packages installed using pip shouldn’t be trusted.
- Doesn’t pollute the global python environment:
It installs the packages in target environment and not in the global environment. So the global environment remains the same without any unwanted package.
Note: It doesn’t help in maintaining non-python.
There are three ways to install virtual environment. You can use either one to install it but most preferable way is using pip command because it is new python-package management system and easy to use.
#First sudo apt-get install python-virtualenv #Second sudo easy_install virtualenv #Third sudo pip install virtualenv
There is another package which makes the managing virtual environments much easier. It places all the virtual environments in one place and provide a list of commands to help managing them.
pip install virtualenvwrapper
and put the following lines in any of the shell startup files e.g ~/.bashrc, ~/.profile etc
export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/Devel source /usr/local/bin/virtualenvwrapper.sh
Few basic commands which are really useful while managing the virtual environments. These commands will work when you have installed “virtualenvwrapper” and configured it.
- Create new virtual environment:
- List down virtual environment names:
- Work on specific virtual virtual environment:
/>workon myvenv (myenv)/>
Once you run this command you will notice the virtual environment name in the prompt like as shown above.
- Deactivate the current virtual environment.
If the virtual environment doesn’t appear in the prompt after executing the command that means the environment has been deactivated and now you are in global environment.
- To delete the virtual environment.
If you like this post, please share it using social media buttons given on the left side. Please feel free to leave your comments or suggestions about the above post or the website. Thank you.