February 17, 2021•293 words
Virtual Environments in Python
This is an exercise in seeing what I remember about using virtual environments for Python projects.
What might happen if you do not use a Virtual Environment for your Python project?
Dependencies required of the project may end up clashing with other versions installed or that get installed at a later date. Your code may not run.
Say you imported xxx v0.1 module / package for project a1 with python 3.8 being the base install of Python which is used as the project interpreter.
Over time your Python install and pip packages may get updated. Or perhaps you create another project and imported xxx v0.1.2 module / package for project a2 with python 3.9 which is now the base install of Python.
When you run your project a1 it no longer works. This is because there are now two versions of xxx module / package and Python cannot differentiate between them. It may also be broken by the different Python version in the base install.
To avoid this you use Virtual Environments. You create a virtual environment to build your script / project in. This means that the project is isolated in it's own environment.
You can choose which version of Python to use for each environment and whether to install all global packages or to start wtih a clean slate.
Changes to the base install of Python will not be made which is good for stability.
In summary using a virtual environment for each project reduces the risk of version conflicts between modules / packages and Python interpreters any of which may stop your code from running.