Python Virtual Environments
I was recently getting back into Python and needed to install a library to communicate with a Tapo smart plug. I tried the installation command:
pip install python-kasa
only to receive a bunch of error text referring to the need for a virtual environment.
After a bit of research it seems that a recent development is the need to house your python development projects in these virtual environments, which protect your system's python libraries from being overwritten by anything you might be installing or tinkering with. So, each Python project is housed within its own environment, with its own libraries that cannot clash with libraries used elsewhere.
It turned out that creating a virtual environment was very easy utilising the "venv" command. I first created a python directory in my home directory:
mkdir ~/python
and followed that up with a sub-directory for my project:
mkdir ~/python/kasa
Within this sub-directory I created the virtual environment:
python -m venv ~/python/kasa
Activating the virtual environment is achieved using:
source ./python/kasa/bin/activate
and finally I installed the required library:
pip install python-kasa
The library is only installed in this sub-directory and this virtual environment.
To leave the virtual environment issue the command:
deactivate
or simply close the terminal window you are working in.