Last week we talked about managing multiple Pythons using pyenv.
But still we are lacking something like the Gemfile in Ruby for managing the version of those Python packages in a Python project. And here comes the Python virtualenv.
We can create different virtualenv on the same machine and each of them has its own Python binary, the pip package and the all other standard Python libraries needed to run the Python. In other words, each virtualenv acts like the Gemfile in a particular Python project.
Another benefit on using virtualenv is that we don’t need root privilege to install the Python packages.
For more details about virtualenv, please refer to the article written by Jamie Matthews:
We could install the virtualenv thru pip. Actually pip and the virtualenv are the only two packages which are needed to be installed globally. Alternatively, if you have pyenv installed, you could install the pyenv-virtualenv which is a plugin of pyenv thru homebrew.
brew install pyenv-virtualenv
Add the following line under the eval “$(pyenv init -)” to your shell profile. For OSX, add it to ~/.profile.
eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
Restart the shell and let’s create an virtualenv with Python 2.7.8.
pyenv virtualenv 2.7.8 project-a-2.7.8
Activate the newly created virtualenv.
pyenv activate project-a-2.7.8
You can deactivate the current virtualenv.
And list the available virtualenvs.
Those virtualenvs are located in ~/.pyenv/versions just like those installed Pythons. If you want to remove a virtualenv, simply delete that corresponding folder.
3 thoughts on “Manage your Python projects with virtualenv”