Python Virtual Environment

When starting a new python project, you need to decide what version of python (nowadays python 3.x+ is the obvious best choice) to use and what packages and modules you will use. Even you may have system administration privilege, it’s a best practice not to install the version of python and packages & modules systemwide as it might break the existing environment which is needed by other applications running on your system. That’s the reason you want an isolated (virtual) environment just for your project.

When speaking of a python virtual environment, it’s really confusing for a new programmer. There seem to be a lot of choices: virtualenv, virtualenvwrapper, pyenv, pyenv-virtualenv, pipenv, venv, etc and it’s not easy to know which does which. But basically, there are three types of requirement for virtual environments:

  1. Multiple versions of python installed on the same machine and you want to use different python versions for different projects. In this case, pyenv is your choice to install a different version of python on the same host and you can use it to create a virtual environment for a particular user under a particular directory. You can also install particular packages and modules under this directory. It’s quite versatile when creating an isolated developing environment. I’ll have another post to show how to install pyenv and then use it to install another version of python on Linux 6.10.
  2. Under the same python version, you want to use different packages and modules for different projects. For example, you have an application A needs the version 1.0 of a module, but another application B needs the version of 2.0 of the same module. In this scenario, you can use virtualenv to create a virtual environment. With Python 3.6+, using “python -m venv” is recommended for beginners since it’s a standard library coming with python 3.6, although it has fewer features comparing to virtualenv currently. virtualenv can also create a self-contained environment with a specific version of python (by copying the specific interpreter binary), but that version needs to be installed at OS level first.
  3. Using different packages and modules with different python versions for different projects. It’s a mix of two requirements above which combines pyenv and virtualenv using pyenv and virtualenv or pyenv-virtualenv ( which will call “python -m venv” if it is available).

As a beginner, don’t think too much since all are new and complicated. Just pick a popular one and start to read its document and use it. If it fits your needs, great! If not and you find limitations, looking for another one.


One thought on “Python Virtual Environment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s