Source

What is virtualenv and why you should use it.

Have you ever heard of virtualenv? The chances are that if you are a beginner then you might not have heard about it but if you are a seasoned programmer than it’s a vital part of your toolset. So what is virtualenv really? Virtualenv is a tool which allows us to make isolated python environments. How does making isolated python environments help us?

Imagine you have an application that needs version 2 of a LibraryBar, but another application requires version 2. How can you use and develop both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

In another case just imagine that you have an application which is fully developed and you do not want to make any change to the libraries it is using but at the same time you start developing another application which requires the updated versions of those libraries. What will you do? It is where virtualenv comes into play. It creates isolated environments for you python application and allows you to install python libraries in that isolated environment instead of installing them globally. In order to install it just type this command in the shell:

$ pip install virtualenv

Now i am going to list some of it’s commands. The most important ones are:

$ virtualenv ENV

and

$ source bin/activate

So what does these two commands do? This first one makes an isolated virtualenv environment in the ENV folder and the second command activates that isolated environment. Now you can install any library without disturbing the global libraries or the libraries of the other environments. This was just a short intro to virtualenv. There’s a lot more to it. For further study i recommend this link. It will remove all of your confusions about virtualenv. I hope you liked today’s post. Do share your views in the comments below and stay tuned for the next post.

Source: http://docs.python-guide.org/en/latest/dev/virtualenvs.html

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Ricardo

Hi there colleagues, nice post and nice arguments commented here, I am really enjoying by these.

Oldham-Made (@OldhamMade)

You should also check-out a project called zc.buildout. It’s similar to virtualenv but creates a Python environment sandbox in the directory where your project lives, and is based off “recipes” which can do things virtualenv can’t, building and installing non-python binaries locally (eg. libxml2). It’s very useful when you have a number of devs working on the same project across multiple systems, and makes deployment a breeze.

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK