In this post I am going to introduce Python’s virtual environments, through the virtualenv tool. With this tool, you can keep separate environments for your projects, with their own dependencies and executables. By isolating the project environments, you can keep them neat and organized, without messing up your global installation. Depending on the requirements of what you’re working on, you can use different versions of Python or keep older libraries on a per-project basis. Nothing outside the virtual environment will be modified.
To begin with, install virtualenv. You can do it in several ways. Via pip:
pip install virtualenv, or with easy_install:
easy_install virtualenv. Now let’s look at the help menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
So you’re about to begin a new project. You want to start from scratch and keep it contained to itself. Creating a virtual environment will help you accomplish that:
Now you can go to the newly created directory and see that there are already some folders inside.
bin has the executables,
include holds the header files, and
lib contains the files of the installed modules in the virtual environment.
Before you begin to work with your new environment, you need to activate it with
source bin/activate or
source env_name/bin/activate if you are outside the environment’s directory.
You will now see your propmt change, to confirm that the environment is active. For me, it looks like:
Now you can begin your work, installing packages, etc. When you’re done, you can type
deactivate to exit your environment. If you want to delete it, just remove its directory.
When working with virtual environments, it might be helpful to take a snapshot of your installed packages and their versions, in case you want to recreate the environment later. You can do this with
pip freeze > requirements.txt. Check the newly created file for a list of your snapshotted items. In my case, I only installed requests in my environment so far:
If you need the exact same environment later, maybe to let some one of your team to work on the project, you can install the same packages and versions with the command
pip install -r requirements.txt
To make working with virtual environments even more convenient, you can use virtualenvwrapper, which provides wrappers for managing your environments and helps keep all your environments in one place. It also allows you to switch between environments with only one command, and has tab completion.
First, install it (outside your environments), with
pip install virtualenvwrapper.
Then you need to add a few lines to your shell startup script. In my
~/.bashrc file, I added the following:
1 2 3 4
Now reload the startup file:
source ~/.bashrc and you will see that your .virtualenvs folder has been created and populated with some scripts. Let’s look at the help menu to see the available commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
Ok, let’s create a new environment:
mkvirtualenv beta_proj. You can list all your environments with
1 2 3
workon command to begin..working on your new environment:
workon beta_proj. When finished, you can use
deactivate again,or you can switch between multiple environments with
Removing an environment can be done with the
That’s about all the basic tips needed to quick start your use of virtual environments in your Python projects. For more information, you can check the virtualenv and virtualenvwrapper documentation.
1 2 3 4 5 6 7 8 9