Traceback (most recent call last):
File "/home/cdsw/field-easyocr/app.py", line 2, in
from src.api import app # noqa
File "/home/cdsw/field-easyocr/src/api/init.py", line 4, in
from .read_router import router as read
File "/home/cdsw/field-easyocr/src/api/read_router.py", line 9, in
from ..read.infer import infer
File "/home/cdsw/field-easyocr/src/read/infer.py", line 2, in
from src.read.easyocr_runner.src.infer import reader
File "/home/cdsw/field-easyocr/src/read/easyocr_runner/src/infer.py", line 1, in
import easyocr
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/easyocr/init.py", line 1, in
from .easyocr import Reader
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/easyocr/easyocr.py", line 3, in
from .recognition import get_recognizer, get_text
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/easyocr/recognition.py", line 10, in
from .utils import CTCLabelConverter
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/easyocr/utils.py", line 7, in
import cv2
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/cv2/.py", line 181, in
bootstrap()
File "/home/cdsw/field-easyocr/.venv/lib/python3.9/site-packages/cv2/.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "/usr/local/lib/python3.9/importlib/.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
256
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Explain the issue for this error and provide a solution
Assistant
Of course. Here is a detailed explanation of the error and how to solve it.
The Problem Explained
The error message ImportError: libGL.so.1: cannot open shared object file: No such file or directory is not a Python code error. It's an operating system-level error indicating that a required system library is missing.
Let's break it down:
Traceback Analysis: Your application (app.py) imports easyocr. The easyocr library, in turn, depends on opencv-python (imported as cv2).
OpenCV's Dependency: OpenCV is a powerful computer vision library written in C++ with Python bindings. Many of its functions, especially those related to image rendering, windowing, and some hardware-accelerated operations, depend on the OpenGL graphics library.
The Missing File: libGL.so.1 is the core, runtime library for OpenGL on Linux systems. It's a "shared object" (.so) file, which is the Linux equivalent of a Windows .dll file.
The Root Cause: When your Python code import cv2, the OpenCV library tries to load its underlying C++ components. The operating system's dynamic linker then searches for all the dependencies of those components, including libGL.so.1. The error message tells you that the system could not find this file. This is very common in minimal server environments, Docker containers, or cloud platforms (like the cdsw - Cloudera Data Science Workbench - environment shown in your path) that are set up without a graphical user interface (GUI) and its associated libraries to save space.
In short: Your Python code needs OpenCV, and OpenCV needs the OpenGL library, but the OpenGL library is not installed on the system where your code is running.
The Solution(s)
You have two primary solutions, depending on your environment and permissions.
Solution 1: Install the Missing System Dependency
This is the most direct solution if you have administrative (sudo or root) access to the machine or container. You need to install the package that provides libGL.so.1.
The command depends on your Linux distribution's package manager.
how do I uninstall opencv-python and install opencv-python-headless with uv package manager
Assistant
Of course. Using the uv package manager is a great choice for its speed. The process is very similar to using pip.
Here is how you uninstall opencv-python and install opencv-python-headless with uv.
The Quick Answer (Commands)
Run these two commands in your terminal within your project's activated virtual environment.
# 1. Uninstall the existing full version of OpenCV
# It's good practice to try uninstalling both common packages to be safe
uv pip uninstall opencv-python opencv-contrib-python
# 2. Install the headless version
uv pip install opencv-python-headless
Detailed Explanation and Best Practices
There are two main ways to manage this change depending on your project setup.
Method 1: Direct Manipulation (For Quick Fixes)
This is what the commands above do. You are directly telling uv to remove and add packages in your current Python environment.
Uninstall:
uv pip uninstall opencv-python
The uv pip uninstall command works just like pip uninstall. It removes the specified package(s) from your environment. We also include opencv-contrib-python because it's a common companion to opencv-python and would also cause a conflict. The command will simply ignore any packages that aren't installed.
Install:
uv pip install opencv-python-headless
The uv pip install command is the super-fast uv equivalent of pip install. It will download and install the headless version of OpenCV.
Why uninstall first? The opencv-python and opencv-python-headless packages cannot coexist because they both create the same cv2 module directory. You must remove one before installing the other to avoid conflicts.
For reproducible environments, you should not modify the environment directly. Instead, you should update your project's dependency file (requirements.txt or pyproject.toml) and then use uv to sync the environment.
# This is usually run as root in a Dockerfile, so sudo is not needed
apk add mesa-gl
After running the appropriate command, restart your Python application. The import error should be resolved.
Solution 2: Use the "Headless" OpenCV Package (Highly Recommended for Servers/APIs)
If you are running your code in an environment where you don't have sudo access (very common in managed platforms like CDSW), or if your application is a backend service/API that will never need to display a GUI window (e.g., using cv2.imshow()), this is the best and cleanest solution.
The opencv-python-headless package is a special version of OpenCV compiled without any of the GUI-related dependencies (like OpenGL, GTK, Qt). It's designed specifically for server-side processing.
Follow these steps:
Uninstall the current OpenCV version. It's important to remove any existing versions to avoid conflicts.
pip uninstall opencv-python opencv-python-contrib
(Run this command even if you think you only have one, just to be safe.)
Install the headless version:
pip install opencv-python-headless
After doing this, your import cv2 will work perfectly because it will no longer look for libGL.so.1. All the core image processing and computer vision functions will still be available.
Which Solution Should You Choose?
Choose Solution 2 (Headless OpenCV) if you are in a server, cloud, or Docker environment and your application is an API that only processes images in the background without displaying them. This is the most common and recommended scenario for backend applications.
Choose Solution 1 (Install System Library) if you have sudo access and you absolutely need GUI functions like cv2.imshow() on that machine, or if other libraries you use also depend on OpenGL.
Edit your requirements.txt file.
opencv-python
Before:
fastapi
uvicorn
easyocr
opencv-python==4.9.0.80 # <-- Change this line
After:
fastapi
uvicorn
easyocr
opencv-python-headless==4.9.0.80 # <-- To this
Sync your environment with uv. The sync command is powerful: it will remove any packages not in your requirements.txt (like the old opencv-python) and install any that are missing (like the new opencv-python-headless).
uv pip sync requirements.txt
If you use pyproject.toml:
Edit your pyproject.toml file. Find opencv-python in your [project.dependencies] list and change it.
Before:
[project]
name = "my-ocr-project"
dependencies = [
"fastapi",
"uvicorn",
"easyocr",
"opencv-python==4.9.0.80", # <-- Change this
]
After:
[project]
name = "my-ocr-project"
dependencies = [
"fastapi",
"uvicorn",
"easyocr",
"opencv-python-headless==4.9.0.80", # <-- To this
]
Sync your environment. After saving the file, you can re-install your project in editable mode or sync it.
# This command will sync your environment based on the pyproject.toml dependencies
uv pip install -e .
Verifying the Installation
After you've run the commands, you can verify that the correct package is installed:
# Use uv to list installed packages and filter for opencv
uv pip list | grep opencv
You should see output similar to this, confirming the headless version is now installed: