Thursday, November 5, 2015

Virtual Environment in Python: virtualenv, pyenv, venv?

As you might already know, virtual environment is a nice way to create isolated environment needed for our project from the Python main installation. In this way, we can keep our main installation clean and not bloated with unused packages after the project concluded.

However, it is a little bit confusing  on what is the better way to create the virtual environment. Based on my research, at least three words come out: virtualenv, pyenv, and venv. What are they, and which one should I use?

Virtualenv
If I am not mistaken, virtualenv is the pioneer on creating isolated environment in Python. It dates back to 2007. I think this is the most popular tool mentioned in any tutorials regarding this subject. Sometimes, you will also hear about virtualenvwrapper. Basically, this is a script to wrap virtualenv so that it is easier for us to work on multiple virtual environments.

Pyenv
Actually, the main goal of pyenv is to manage various versions of python. It intercepts the calls to python and direct them to one of the system python tool-chains. As the author said (see the github link), it does not manage virtual environment by default, which raise the question why should I bother putting it here. I add pyenv here because the name makes some confusion with other tools in my head. Although, the author also develops a plugin which enables pyenv to manage virtualenv (pyenv-virtualenv). Even he also provides pyenv-virtualenvwrapper.

Venv
Since Python 3.3, a new module has been added in Python 'support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories' .(quoted from here). So, this is kind of the 'official' Python virtual environment tool. It looks pretty much similar to virtualenv. However, someone in Stackoverflow (damn, I forgot to bookmark the link and now I don't remember the username) pointed out that venv has less features compared to virtualenv. Nevertheless, I think venv already provides sufficient capabilities for my needs, and it has an advantage that it is already built in the Python.

Now, back to the question: which one should I use?
For Python >3.3, I myself would prefer venv, since it is already built in. For older Python, well, venv has not existed yet, so virtualenv should be the only choice.

In Ubuntu, before you are able to use venv, you should install it first:
 apt-get install python3-venv
Afterwards, a new command 'pyvenv' is installed. It is basically a symlink towards the actual venv command specific to the python version. In my case, it is symlinked to pyvenv-3.4.

To use venv, the workflow is quite similar to virtualenv. First, you create the virtual environment directory. Next, activate the virtual environment. If finished, simply deactivate it. This link provides nice basic tutorial on how to use venv.

1 comment:

  1. MELCOME CASINO, LLC - Jackson, OK - MJH
    MELCOME CASINO, LLC, 서산 출장샵 9271 East 성남 출장마사지 Highway 61, 계룡 출장안마 Jackson, OK 74459. Get 삼척 출장마사지 Directions 안양 출장샵 · Website.michael.michael.mmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuelmmanuel

    ReplyDelete