Python Virtual Environment venv

This is about virtual environments for Python.

venv is the best approach to select a Python environment. Venv would provide the possibility to assign a virtual environment, activate it. This makes working multiple projects on the same machine easy.

Setting venv:

python -m venv folder_name

This creates a new environment to the folder specified; it creates the folder if needed.

Then inside the folder there is another subfolder called bin, underneath there are links to the python executable etc. There is a script called “activate”. This is not marked executable – if you run on linux – so it has to be run with a command like

source activate

This activates the current virtual environment for the current session. Also defines a new function called deactivate. This function can be invoked from the current session and ultimately deactivates the current virtual environment, potentially to move to another virtual environment.

Below is the set of commands showing how to create a venv and then activate it. Note the “source activate” command.

See the name of the venv on the left side. Now see below that all the commands related to Python are taken from this particular venv.