PaddlePaddle/PaddleOCR · error · RuntimeError

Environment Variable CUDA_VISIBLE_DEVICES is not set correct

Error message

Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id.

What it means

Raised during __init__ of the ocr_rec hubserving module when use_gpu=True but CUDA_VISIBLE_DEVICES is not usable. The validation is os.environ["CUDA_VISIBLE_DEVICES"] plus int(value[0]) inside a bare try/except, so unset, empty, or non-digit-leading values all raise this RuntimeError.

Source

Thrown at deploy/hubserving/ocr_rec/module.py:60

    type="cv/text_recognition",
)
class OCRRec(hub.Module):
    def _initialize(self, use_gpu=False, enable_mkldnn=False):
        """
        initialize with the necessary elements
        """
        cfg = self.merge_configs()

        cfg.use_gpu = use_gpu
        if use_gpu:
            try:
                _places = os.environ["CUDA_VISIBLE_DEVICES"]
                int(_places[0])
                print("use gpu: ", use_gpu)
                print("CUDA_VISIBLE_DEVICES: ", _places)
                cfg.gpu_mem = 8000
            except:
                raise RuntimeError(
                    "Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES via export CUDA_VISIBLE_DEVICES=cuda_device_id."
                )
        cfg.ir_optim = True
        cfg.enable_mkldnn = enable_mkldnn

        self.text_recognizer = TextRecognizer(cfg)

    def merge_configs(
        self,
    ):
        # default cfg
        backup_argv = copy.deepcopy(sys.argv)
        sys.argv = sys.argv[:1]
        cfg = parse_args()

        update_cfg_map = vars(read_params())

        for key in update_cfg_map:

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. export CUDA_VISIBLE_DEVICES=0 (numeric device id) in the launching shell/container/pod spec.
  2. Use use_gpu=False for CPU-only deployments.
  3. Add the variable to the service unit or pod env block so restarts keep working.

Example fix

# before
mod = RecModule(use_gpu=True)  # no env -> RuntimeError

# after
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
mod = RecModule(use_gpu=True)
# or
mod = RecModule(use_gpu=False)
Defensive patterns

Strategy: validation

Validate before calling

import os

v = os.environ.get("CUDA_VISIBLE_DEVICES", "")
use_gpu = bool(v) and v[0].isdigit()
mod = RecModule(use_gpu=use_gpu)

Try / catch

try:
    mod = RecModule(use_gpu=True)
except RuntimeError as e:
    if "CUDA_VISIBLE_DEVICES" in str(e):
        os.environ["CUDA_VISIBLE_DEVICES"] = "0"
        mod = RecModule(use_gpu=True)
    else:
        raise

Prevention

When it happens

Trigger: Constructing TextRecModule(use_gpu=True) with CUDA_VISIBLE_DEVICES unset, set to "", or starting with a non-digit.

Common situations: Deploying the recognition service on a fresh GPU box where the export line was never added to the startup script; kubernetes pod spec missing the env entry.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/2192b02b70652cb1. Report an issue: GitHub.