The basics of virtual environments
Lately, I came into a very interesting youtube channel regarding Data Analysis, Machine Learning and Python Programming. In one of the videos, the host showed how to make a simple (web)application using Streamlit and Pandas-profiling libraries to present results of automatic exploratory data analysis (EDA). The video was short and just to the point, that anyone can be guided by it to deliver a quick customised implementation of their own version of the app in a free period of time (like in between daily duties). I started to play with the code but certainly what works well on one’s computer may not work on other's at all. My working environment was not prepared/set well for handling the application. It lacked some libraries, some others needed to be updated etc. Generally, my main instance of Anaconda was a mess, so I started googling to find a solution. I came into the concept of virtual environments (venv). It allows you to create, export, list, delete, and update among other, environments that have different versions of Python and packages installed in them. You can also share an environment file containing libraries’ versions. In this manner, you can basically create a separate environment for each project and store only the dependencies that are relevant to this particular project. This helps keeping order in the project. Making a dedicated environment is very helpful in avoiding conflicts between libraries and it allows running applications even after a long period of time (what might be not possible if you updated some libraries, for example).
Below are a few useful commands for Conda Prompt or MacOS Terminal. Windows commands for Powershell or cmd are not included.
*square brackets in the commands should be skipped. They only indicate where the names should be specified
activation
deactivation of a venv
deleting environment when not needed anymore
listing of available environments in Anaconda
generating a file with requirements –> requirements.txt
using pip command in conda
installing packages
listing of packages installed for the activated environment
making an exact clone of an existing Anaconda environment
It is as easy as this.
Some final remarks:
use virtual environments – it helps a lot,
always create and/or activate the dedicated environment each time you want to work on a particular project and deactivate it when you are finished with it,
generate a requirements.txt file for each environment (project) – it is a good practice and a helpful cheat sheet of what you need to smoothly run your apps
creation a new environment
the environment name should be indicative of what the purpose of the environment is
you can install specific versions of packages either form a requirements.txt file or after explicitly specifying their versions