Using a Python virtual environment is a great way to experiment and work with libraries like gpiozero without having an effect on the 'main' (i.e., the global) Python environment. You can do whatever installs and such inside your virtual environment without being concerned how it will affect other software (including Linux software) that uses Python. And you can pretty easily toss a virtual environment and create a new instance/copy whenever you wish. The code below uses the LCD 1602 device as a demo; it is based on the How to use the LCD1602 I2C Display with Raspberry Pi link (see below). This was tested on a Raspberry Pi 5 updated last on Dec 20, 2024.
# Run these commands in a terminal to create a virtual environment
# and run the LCD demo program.
# create a subdirectory for your testing
mkdir my_lcd_test
cd my_lcd_test
# You don't need to use the leading '.';
# it just 'hides' the virtual env subdirectory
# it does not clutter your 'ls' output, etc.
python -m venv .my_virtual_env
# If you have problems with accessing some packages
# (namely packages that are not available by installing
# using 'pip), you can use the system-site-packages option
# to give your virtual environment access to the
# site-level install of Python.
# Example:
# python -m venv .my_virtual_env --system-site-packages
# Activate the newly-created virtual environment.
source .my_virtual_env/bin/activate
pip install -U pip
pip install -U setuptools
pip install -U wheel
pip install -U rpi-lgpio
pip install -U gpiozero
pip install -U rpi_lcd
# obtain the demo programs from this github repository:
# git clone git@github.com:mheidenreich/LCDDemo
# Or just type them in.
# If you use the above 'git clone', execute the demo like this:
python LCDDemo/lcd-demo.py
Once you do the above, you don't need to repeat all that to use an existing virtual environment. Use the commands below to get back into the virtual environment. You'll need to do this if you close the terminal and want to use a different terminal (such as after logging out, booting, etc.) NOTE you need to do these steps in each terminal window where you wish to have access to the virtual environment.
# Go to your directory
cd my_lcd_test
# (re)activate the virtual environment.
source .my_virtual_env/bin/activate
python LCDDemo/lcd-demo.py
If you have a device that uses an I2C address other than the default 0x27, you can specify its address in the Python code when you created the LCD object. In the video linked below, the address is changed by editing the source code of the rpi_lcd library. This works but has the disadvantage that you must repeat the edit whenever you update the library. You can remain independent of library updates using the technique shown below.
# USING A DEVICE WITH NON-DEFAULT I2C ADDRESS
#
# If your LCD interface chip uses an address other than
# the default 0x27 (for example, if it uses 0x3f).
# Replace this line in the demo program(s):
# lcd = LCD()
# with this one, that explicitly specifies the device address:
# lcd = LCD(address=0x3f) # change 0x3f to your device's address
# For example:
vi LCDDemo/lcd-demo.py # make the fix show above
... commands as needed to make the fix ...
wq ... exit the editor ...
python LCDDemo/lcd-demo.py
--------
[How to use the LCD1602 I2C Display with Raspberry Pi ][1]