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_det hubserving module when use_gpu=True and CUDA_VISIBLE_DEVICES fails validation. The constructor reads the variable and calls int(value[0]) under a bare except, so an unset variable (KeyError), empty string, or a first character that is not a digit (ValueError) all surface as this RuntimeError.

Source

Thrown at deploy/hubserving/ocr_det/module.py:62

    type="cv/text_detection",
)
class OCRDet(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_detector = TextDetector(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 before launching the service and keep use_gpu=True.
  2. Pass use_gpu=False to run on CPU if no GPU is available.
  3. For docker: add -e CUDA_VISIBLE_DEVICES=0 to the docker run command.

Example fix

# before
mod = DetModule(use_gpu=True)  # env unset -> RuntimeError

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

Strategy: validation

Validate before calling

import os

def gpu_ready() -> bool:
    v = os.environ.get("CUDA_VISIBLE_DEVICES", "")
    return len(v) > 0 and v[0].isdigit()

use_gpu = gpu_ready()  # decide before constructing the module

Try / catch

try:
    mod = DetModule(use_gpu=True)
except RuntimeError as e:
    if "CUDA_VISIBLE_DEVICES" in str(e):
        mod = DetModule(use_gpu=False)  # graceful CPU fallback
    else:
        raise

Prevention

When it happens

Trigger: Starting the ocr_det serving module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset, empty, or begins with a non-numeric character.

Common situations: GPU deployment inside a container where the env var was not passed (-e CUDA_VISIBLE_DEVICES=0 missing from docker run); systemd service without Environment=; shell where the export was forgotten.

Related errors


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