Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Python Environment Setup: `venv` and Package Installation

Recommended resources

Welcome back! In the previous lesson, we successfully configured your system's most crucial hardware component, the NVIDIA GPU, by installing the proprietary drivers and verifying their operation with nvidia-smi. Your Rocky Linux machine is now ready to handle the demanding tasks of real-time video processing.

Today, we transition from hardware configuration to setting up the software foundation for our gesture recognition application. Our goal is to create a clean, self-contained Python development environment. Using a virtual environment is standard practice in modern software development for managing project-specific dependencies, ensuring that the packages for this project don't conflict with any other Python applications on your system. We will create this environment, and then install the three key Python libraries for our project: OpenCV, MediaPipe, and paho-mqtt.

This lesson involves a series of precise command-line steps that will establish the coding environment for the remainder of our course.

1. Preparing the Python Environment

Rocky Linux 9 comes with Python 3.9 pre-installed. However, to create and manage virtual environments, we need the venv module, which is not always included by default. We'll also install a set of development tools, which are essential for compiling any underlying C/C++ code that Python packages might depend on.

The following guide from DigitalOcean provides a clear path for these preparatory steps.

How To Install Python 3 and Set Up a Programming Environment on Rocky Linux 9 | DigitalOcean

This article provides an excellent walkthrough for setting up a Python environment on Rocky Linux. We will follow its steps to prepare your system, create a virtual environment, and activate it.

First, open your terminal. While the article mentions updating your system with sudo dnf update -y, we already did this in a previous lesson, so you can skip that. Follow the instructions in Step 2 to verify your Python 3 installation by running python3 -V. Next, execute the command under the same section to install the Development Tools (sudo dnf -y groupinstall development). Finally, read through the introduction to Step 3. This explains the rationale behind using virtual environments, a concept that's analogous to managing dependencies in other ecosystems you may have used, like Flutter's pubspec.yaml.


2. Creating and Activating the Virtual Environment

With the necessary tools installed, we can now create the isolated environment for our project. A common convention is to create a directory to house your projects and then create the environment within the specific project folder. Let's call our project gesture-control.

Follow these commands in your terminal:

  1. Create and navigate into a project directory:

    mkdir ~/gesture-control
    cd ~/gesture-control
    
  2. Now, create the virtual environment. We'll name it venv, which is a standard convention.

    python3 -m venv venv
    

    This command creates a venv directory containing a copy of the Python interpreter and its standard libraries.

  3. To use this environment, you must activate it:

    source venv/bin/activate
    

    You'll know it's active because your shell prompt will be prefixed with (venv). From this point on, any Python packages you install will be placed in this local environment, not in the global system-wide Python installation.

The DigitalOcean guide you reviewed previously also covers this activation step visually.


3. Installing Project Dependencies

With our virtual environment active, we can now use pip, Python's package installer, to install the three core libraries for our gesture control system.

  • opencv-python: A library for computer vision tasks. We'll use it to capture video from your webcam.
  • mediapipe: Google's framework for building multimodal (e.g., video, audio) applied ML pipelines. We will use its powerful pre-trained Hand Landmarker model.
  • paho-mqtt: A client library for the MQTT protocol, which we'll use to send gesture commands to your IoT devices.

You can install all of them with a single command:

pip install opencv-python mediapipe paho-mqtt

The video below briefly introduces MediaPipe and mentions the installation process, reinforcing how these powerful tools are made available through simple pip commands.

Custom Hand Gesture Recognition with Hand Landmarks Using Google’s Mediapipe + OpenCV in Python

This video by Ivan Goncharov provides a great overview of gesture recognition with MediaPipe. We will explore these concepts in detail in the next module.

For now, just watch the brief segment from installing the libraries, where the presenter mentions how to install OpenCV and MediaPipe. Note that the command for OpenCV is opencv-python.


4. Proactively Solving a Common OpenCV Issue

On many Linux distributions, including Rocky Linux, attempting to import the opencv-python library after a fresh installation can result in a runtime error:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

This happens because the standard opencv-python package relies on system libraries for rendering GUI windows (like the video feed we'll display), and this specific library (libGL.so.1) is missing. We can fix this by installing the required Mesa library package.

A Stack Overflow thread discusses this exact problem, with a user providing a solution specifically for Rocky Linux 9.

python - ImportError: libGL.so.1: cannot open shared object file: No such file or directory - Stack Overflow

This Stack Overflow post is a classic example of community-driven problem-solving. It details a common dependency issue with OpenCV on Linux.

Read the short answer provided by user "noiissyboy," which gives the specific command for our operating system. You'll find it near the bottom of the page, starting with the phrase.

Based on that solution, run the following command in your terminal to install the missing dependency. You do not need to deactivate your virtual environment for this, as it's a system-level package.

sudo dnf install mesa-libGLU -y

By installing this, you've preemptively solved a common and frustrating error.


5. Verifying the Environment

The final step is to verify that our environment is correctly set up and all packages can be imported without errors. While still inside your active (venv) shell, run the following one-liner:

python -c "import cv2; import mediapipe; import paho.mqtt.client; print('All modules imported successfully!')"

If the command executes and prints the "All modules imported successfully!" message without any ImportError messages, your environment is ready to go.

To leave the virtual environment at any time, simply type:
deactivate

Remember to reactivate it with source venv/bin/activate whenever you start working on this project.


Conclusion

In this lesson, you've configured a robust and isolated Python development environment for your gesture control project. You've learned the importance of virtual environments for dependency management and followed a standard workflow to create, activate, and populate one with the necessary libraries. Crucially, you also practiced a key developer skill: anticipating and resolving dependency issues by installing the required system libraries for OpenCV.

Key Takeaways:

  • Virtual environments, created with python3 -m venv, are essential for isolating project dependencies.
  • An active virtual environment is indicated by a prefix like (venv) in your shell prompt.
  • pip is used to install packages inside the active environment.
  • Python packages can have system-level dependencies (like mesa-libGLU for OpenCV) that must be installed separately using the system's package manager (dnf).
  • A simple import test is a quick and effective way to verify that all packages are installed correctly.

With your development environment now prepared, our next step is to set up the communication backbone of our IoT system. In the following lesson, we will install and configure the Mosquitto MQTT broker, the central hub that will relay commands from your gesture recognition script to your IoT devices.