358

I've been hearing the buzz about virtualenv lately, and I'm interested. But all I've heard is a smattering of praise, and don't have a clear understanding of what it is or how to use it.

I'm looking for (ideally) a follow-along tutorial that can take me from Windows or Linux with no Python on it, and explain every step of (in no particular order):

  • what I should do to be able to start using virtualenv
  • specific reasons why using virtualenv is a good idea
  • situations where I can/can't use virtualenv
  • situations where I should/shouldn't use virtualenv

And step through (comprehensively) a couple sample situations of the should+can variety.

So what are some good tutorials to cover this stuff? Or if you have the time and interest, perhaps you can answer a few of those questions here. Either in your answer, or as a link to tutorials that answer it, these are the things I'd like to know.

chicks
  • 2,393
  • 3
  • 24
  • 40
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
  • 7
    This article from [dabapps](http://dabapps.com/blog/introduction-to-pip-and-virtualenv-python) makes the concepts simple – suhair Apr 22 '13 at 12:35
  • Check this guide http://thepythonguru.com/python-virtualenv-guide/ – Cody Aug 31 '15 at 04:23
  • 2
    Here is a new tutorial: https://realpython.com/blog/python/python-virtual-environments-a-primer/ – nofinator Mar 28 '16 at 16:05
  • As a python rookie, I was looking for a straight to the point intro to virtualenv (and pip) and I have just been through this one: [https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/][1] [1]: https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/ this excellent blog post has removed all the frustration I had after reading other confusing/approximative ones. – Arnaud Bouchot Mar 27 '17 at 14:36
  • The [official user guide](https://virtualenv.pypa.io/en/stable/userguide/)? –  Jan 08 '18 at 23:33

4 Answers4

245

This is very good: http://simononsoftware.com/virtualenv-tutorial-part-2/

And this is a slightly more practical one: https://web.archive.org/web/20160404222648/https://iamzed.com/2009/05/07/a-primer-on-virtualenv/

Aaron Miller
  • 579
  • 4
  • 7
Mark Pope
  • 11,244
  • 10
  • 49
  • 59
  • 3
    +1 I agree they were good for a beginner like me. I ran through both, now I at least know what it is and the basics of getting/using it. (The second for some reason explained `activate` but neglected `deactivate` o_O). I'm still hoping for more elaboration on when to use it (and when not to), and deeper examples. – Dan Burton May 01 '11 at 07:13
  • 1
    I believe the answer is to always use it, in the same way you should always use version control. – mlissner Feb 07 '13 at 00:45
  • simononsoftware didn't load at 1 o'clock Melbourne time May 28th 2013 – Alex May 28 '13 at 03:00
  • on windows use /Scripts/activate (and deactivate) instead of /bin/activate. – DrBailey Jun 19 '14 at 23:50
  • 1
    Be aware that --no-site-packages is deprecated, and now it's the default behaviour. – Adrian Lopez Nov 25 '14 at 13:24
  • There's now a [_Virtualenv Tutorial Part 2_](http://simononsoftware.com/virtualenv-tutorial-part-2/) on SimonOnSoftware. – martineau Mar 14 '15 at 23:25
  • 1
    This gave me a good idea about [`virtualenv`](http://docs.python-guide.org/en/latest/dev/virtualenvs/). Recommended to use [`virtualenvwrapper`](http://docs.python-guide.org/en/latest/dev/virtualenvs/#virtualenvwrapper) which was mentioned in later part of the doc. – gihanchanuka Jun 25 '15 at 11:57
  • 2
    It's funny how basically not a single `virtualenv` tutorial on the internet, nor [virtualenv's user guide](https://virtualenv.pypa.io/en/latest/userguide.html), actually show you how to use the environment on your Python script. You aren't setting up an environment just for the sake of setting up an environment. I found [one tutorial that at least shows you the alternative](https://www.digitalocean.com/community/tutorials/common-python-tools-using-virtualenv-installing-with-pip-and-managing-packages#working-with-a-virtual-environment-without-activating). – Forage Aug 08 '15 at 15:11
  • A really important point is to *use [virtualenvwrapper](http://docs.python-guide.org/en/latest/dev/virtualenvs/#virtualenvwrapper)* on top of virtualenv - this gives much easier activation of virtualenvs (`workon myproj`) and stashes all the virtualenvs outside the project tree (usually under `~/.virtualenvs`). Always install them with `pip`, never from a Linux distro package (can be very out of date and buggy) - and same goes for `pip` unfortunately, so use [get-pip](https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py) as in the pip docs. Less hassle later! – RichVel Dec 09 '16 at 17:35
35

Virtualenv is a tool to create isolated Python environments.

Let's say you're working in 2 different projects, A and B. Project A is a web project and the team is using the following packages:

  • Python 2.8.x
  • Django 1.6.x

The project B is also a web project but your team is using:

  • Python 2.7.x
  • Django 1.4.x

The machine that you're working doesn't have any version of django, what should you do? Install django 1.4? django 1.6? If you install django 1.4 globally would be easy to point to django 1.6 to work in project A?

Virtualenv is your solution! You can create 2 different virtualenv's, one for project A and another for project B. Now, when you need to work in project A, just activate the virtualenv for project A, and vice-versa.

A better tip when using virtualenv is to install virtualenvwrapper to manage all the virtualenv's that you have, easily. It's a wrapper for creating, working, removing virtualenv's.

lborgav
  • 2,371
  • 19
  • 28
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – joce May 18 '14 at 02:05
  • 3
    I edited the answer and added more value to the same. Thanks for the feedback – lborgav May 18 '14 at 04:42
8

Here's another good one: http://www.saltycrane.com/blog/2009/05/notes-using-pip-and-virtualenv-django/

This one shows how to use pip and a pip requirements file with virtualenv; Scobal's two suggested tutorials are both very helpful but are both easy_install-centric.

Note that none of these tutorials explain how to run a different version of Python within a virtualenv - for this, see this SO question: Use different Python version with virtualenv

Community
  • 1
  • 1
Alex Dean
  • 15,575
  • 13
  • 63
  • 74
2

For setting up virtualenv on a clean Ubuntu installation, I found this zookeeper tutorial to be the best - you can ignore the parts about zookeper itself. The virtualenvwrapper documentation offers similar content, but it's a bit scarce on telling you what exactly to put into your .bashrc file.

metakermit
  • 21,267
  • 15
  • 86
  • 95