While programming, we sometimes make errors that can make undesirable changes to our system. To avoid this, we often use virtual environments. In this article, we will discuss how to create and use a virtual environment using the virtualenv module in python.
What is a Virtual Environment?
A Virtual Environment put simply, is an isolated working copy of Python that allows you to work on a specific project without worry of affecting other projects.
A virtual environment enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. In python, we use virtual environments using the virtualenv module.
Install virtualenv and Verify the Installation
There is a chance that python virtualenv is already installed on your system. To check if it’s already installed run the following command in your terminal.
virtualenv --version
After execution of the above command, If you see a version number (in my case 1.6.1), it’s already installed on your system.
>>1.6.1
If virtualenv is not installed on your system, you can install it in Linux using the commands discusses in the following section
Install Virtualenv in Linux Ubuntu
There are a number of ways to install virtualenv on your system.
To install virtualenv using the apt package management system, you can use the following command.
sudo apt-get install python-virtualenv
If you want to install python virtualenv using the easy_install package installer, you can use the following command.
sudo easy_install virtualenv
If can also use python’s PIP to install virtualenv using the following command.
sudo pip install virtualenv
If you are using python3, you can use pip3 instead of pip in the above command.,
Setup and Use Virtualenv in Linux
Once you have virtualenv installed on your system, just fire up a shell and create your own environment using the following steps.
- First, create a directory for your new shiny isolated environment. For this, you can use the mkdir command and give the full pathname of the directory to be created. For instance, to create a folder named virtualenvironment in the home directory, you can use the following command.
mkdir ~/virtualenvironment
- After creating the folder for the virtual environment, we will create a folder for our python execution using virtualenv. To create a folder for your new app that includes a clean copy of Python, you can use the following command.
virtualenv ~/virtualenvironment/my_new_app
You can add –no-site-packages in the command if you want to isolate your environment from the main site-packages directory.
- To begin working on your project, you have to activate the virtual environment after creating it. For this, cd into your project directory’s bin folder.
cd ~/virtualenvironment/my_new_app/bin
- Finally, activate your environment using the following command.
source activate
After execution of the above command, python virtualenv will be activated on your machine. Notice how the prompt of your shell changed to show the active environment. That is how you can see that you’re in your new environment.
After activating the virtual environment, any new installations will be installed to this environment only. The installations will have no effect on the python installation in the /etc/bin folder of the system.
To deactivate your python virtualenv, just type “deactivate” in the command line.
What Does Virtualenv do?
Packages installed in the virtual environment will not affect the global Python installation. Virtualenv does not create every file needed to get a whole new python environment. It uses links to global environment files instead in order to save disk space end speed up your virtualenv. Therefore, there must already have an active python environment installed on your system.
Install a Package in your Virtualenv
If you look at the bin directory in your virtualenv, you’ll see easy_install which
has been modified to put eggs and packages in the virtualenv’s site-packages
directory.
To install an app in your Virtualenv, you can use the PIP package installer. For instance, you can use the following command to install flask in the virtual environment.
pip install flask
If you are using python3, you can use pip3 instead of pip in the above command.
You don’t have to use sudo since the files will all be installed in the virtualenv directory which was created as your own user account.
Conclusion
In this article, we have discussed how to create and use virtual environments using the python virtualenv module. To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on the port scanner in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
For further reading, please see the following documentation:
http://flask.pocoo.org/docs/installation/#virtualenv
http://pypi.python.org/pypi/virtualenv
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.