Affiliate Disclosure: By buying the products we recommend, you help keep the lights on at MakeUseOf. Read more.
Whether you are an experienced Python developer, or you are just getting started, learning how to setup a virtual environment is essential for any Python project. Join me as I cover everything you need to know about the Python virtual environment.
What Is a Python Virtual Environment?
A Virtual Environment is a way to run different versions of Python for different projects. Similar to how virtual machines work, Python virtual environments allow you to install multiple versions of Python with specific modules and dependencies for each version. These projects are all independent of each other, so any modules you install in a certain project will not be accessible in other projects.
This may seem like a lot of effort, but it’s worth it. Say you normally work in Python 2.7.x but you want to try 3.x out. No problem, just create a new project and install your dependencies. What about Python 2.4.x for a legacy project? Yep, simple. None of these projects will interfere with each other, nor will they involve the version of Python used by your operating system.
Getting Set Up
It doesn’t matter what version of Python you are using. If you are using Mac then you have Python installed already. You will need to download and install Python if you are using Windows.
You will need pip installed. This is a package manager for Python, and it comes with Python versions 2.7.9 or newer. All of these steps will be done through the command line, so you may want to read our guide to the Windows Command Line or our quick guide to the Linux Command Line.
There are two packages needed to use virtual environments. Open a new terminal and install the virtualenv package:
pip install virtualenv
It is entirely possible to use and manage virtual environments with this package alone. I will not cover how to do that, as it is much easier to use the virtualenvwrapper. This is a package written to make it easy to create and manage virtual environments. Install it using pip:
pip install virtualenvwrapper
In windows you will need to install a slightly different package:
pip install virtualenvwrapper-win
Make sure you have virtualenv installed before you try to install virtualenvwrapper.
Now configure the wrapper:
export WORKON_HOME=~/Envs source /usr/local/bin/virtualenvwrapper.sh
This wrapper stores all of your environments in the same place (instead of scattered around your filesystem, which the virtual environment will do without the wrapper).
Now that your virtual environment is all setup, you can start using it. Here’s how you create a new environment:
This will create a folder and environment called muo inside your ~/Envs folder.
You can use this command to create as many environments as you like. It’s easy to change environments using the workon command:
You should now see the name of your project in the command line:
Any packages you install will only work inside this environment.
If you no longer wish to work in an environment you need to use the deactivate command:
It’s important to note that the workon command will deactivate the current project, and then activate the new project. There is no need to deactivate first.
It’s easy to list virtual environments:
If you no longer want an environment, you can delete it:
Make sure you are not currently working on that environment, otherwise you will get an error:
Finally, it’s easy to setup an environment with a specific version of Python:
virtualenv -p /usr/bin/python2.7 muo27
Make sure that the file path (/usr/bin/python2.7) points to a version of Python (this could be any version). Notice how I have called this project muo27. I have used the suffix 27 to indicate that this is a Python 2.7 environment.
There are a few other options you can use when creating environments. The –no-site-packages option will not install packages that are already installed globally (by the operating system). These will not be accessible to your environment. This is useful for keeping a project compact and tidy, and not filling it with unnecessary packages.
You can use the freeze command to generate a list of dependencies needed for your project:
pip freeze > dependencies.txt
This will create .txt file called dependencies of all the required modules. This will make it much easier for you or another developer to get the project going again at a later date. Here’s how you can install the required modules from that list:
pip install -r dependencies.txt
Now that you know how to use Python Virtual Environments, there is no limit to the projects you can work on! Why not learn how to read and write to Google Sheets and create yourself a new environment to work in.
Do you use Python Virtual Environments? What’s your favorite feature? Let use know in the comments below!
Image Credit: Sergey Nivens and Helen Dream via Shutterstock.com