virtualenv (https://pypi.org/project/virtualenv/) is a very popular tool that creates isolated Python environments for Python libraries. virtualenv allows multiple Python projects that have different (and sometimes conflicting) requirements. In a technical way, virtualenv works by installing some files under a directory (for example, env/).
Additionally, virtualenv modifies the PATH environment variable to prefix it with a custom bin directory (for example, env/bin/). Additionally, an exact copy of the Python or Python3 binary is placed in this directory. Once this virtual environment is activated, you can install packages in the virtual environment using pip. virtualenv is also recommended by the PyPA (https://packaging.python.org/guides/tool-recommendations/). Therefore, we will see how to install OpenCV or any other packages using virtual environments.
Usually, pip and virtualenv are the only two packages you need to install globally. This is because, once you have installed both packages, you can do all your work inside a virtual environment. In fact, virtualenv is really all you need, because this package provides a copy of pip, which gets copied into every new environment you create.
Now, we will see how to install, activate, use, and deactivate virtual environments. Specific commands are given now for both Linux and Windows operating systems. We are not going to add a specific section for each of the operating systems, because the process is very similar in each one. Let's start installing virtualenv:
$ pip install virtualenv
Inside this directory (env), some files and folders are created with all you need to run your python applications. For example, the new python executable will be located at /env/scripts/python.exe. The next step is to create a new virtual environment. First, change the directory into the root of the project directory. The second step is to use the virtualenv command-line tool to create the environment:
$ virtualenv env
Here, env is the name of the directory you want to create your virtual environment inside. It is a common convention to call the directory you want to create your virtual environment inside env, and to put it inside your project directory. This way, if you keep your code at ~/code/myproject/, the environment will be at ~/code/myproject/env/.
The next step is to activate the env environment that you have just created using the command-line tool to execute the activate script, which is in the following location:
- ~/code/myprojectname/env/bin/activate (Linux)
- ~/code/myprojectname/env/Scripts/activate (Windows)
For example, under Windows, you should type the following:
$ ~/code/myprojectname/env/Scripts/activate
(env) $
Now you can install the required packages only for this activated environment. For example, if you want to install Django, which is a free and open source web framework, written in Python, you should type this:
(env)$ pip install Django
You can also deactivate the environment by executing the following:
$ deactivate
$
You should see that you have returned to your normal prompt, indicating that you are no longer in any virtualenv. Finally, if you want to delete your environment, just type the following:
$ rmvirtualenv test