Virtual environments (or virtualenv’s) are one of the things I didn’t really understand why or how I should use them. Although it’s relatively simple to figure out, you’ll have to jump from page to page to get a simple and clear understanding of what they actually do. I hope I can summarise the basic concept and benefits of using them here.
First thing: when developing with Python, almost always use virtual environments.
Why? A virtual environment is a way of getting you to keep one project to itself. You may need to install packages for your project, and the same for another. But what if the packages you need require different versions? And what if you install them to your site-package directory, and then you lose functionality in one project, but gain it in another? This kind of complex situation can simply be dealt with by using virtual environments.
The virtual environment will simply install packages into a special directory, just for your project. This way you can avoid conflicts between projects and their requirements’ versions. It just makes everything a whole lot easier.
Second thing: it’s worth the fuss.
Yes, creating and using virtual environments can be quite a fuss, and knowing how to use things like Git with them can be too. But it will save you time later, and help you keep things organised. Keeping everything installed locally also means you can go into packages and their files, modify some of their code fit to your project’s needs, without having to worry about any other scripts that rely on them.
Thirdly: how to actually do it.
Virtualenv is a package that you can install with pip. So it’s no big deal getting it up and started. I don’t want to waste time explaining how to install it all and use it, since someone’s already done it better than I could. Look at the guide here. However if you are just looking for that one line of code to install it, here it is:
pip install virtualenv
If you need, to then you can always use sudo to install it into the site-packages (but you won’t have to when using a virtual environment!).
When you read the guide I linked you to, you’ll notice how it’s a bit fussy to activate everything. Scroll down a bit further on that page and look at virtualenvwrapper, since it’s another great package that makes projects and little projects so much easier to use.
If you do choose to use this wrapper, you’ll find that after a week, you won’t even know that you’re always using a virtualenv
Extra notes: using Git.
I like developing things with Django at the moment, and virtual environments are the only way to go with this. When I need to install packages in the local, virtual environment, then it’s simple and organised (I often have multiple projects going at one time). But I’m getting to the point where I want to keep my code on GitHub, so that I can work on the project with friends, all with the help of version control. However I can’t upload all my required packages, and I can’t remember them all either.
Luckily, there’s a great way to help out your co-coders. It’s quite simple, but there’s more detail here. To summarise, you use the pip freeze command, which will give you a list of all dependencies, put that into a text file called requirements.txt (or whatever you want to call it), and get all others working on the project, to start their virtual environments, copy the files in, and install the required packages with
pip install -r requirements.txt
It’s really that simple. Jason, here, goes into more detail about you .gitignore, etc. so I really recommend reading his article if you plan to use Git at all with Python, or more specifically, Django.
I’m writing another short post on how to get yourself up and running with a Git-based project, so that you can get co-developing as soon as possible.
Virtual environments do it all, and easily. Just get used to using them and you’ll save time in the long run, on more complicated projects!