{"record":{"id":"2192b02b70652cb1","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s-2192b0","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"deploy/hubserving/ocr_rec/module.py","lineNumber":60,"sourceCode":"    type=\"cv/text_recognition\",\n)\nclass OCRRec(hub.Module):\n    def _initialize(self, use_gpu=False, enable_mkldnn=False):\n        \"\"\"\n        initialize with the necessary elements\n        \"\"\"\n        cfg = self.merge_configs()\n\n        cfg.use_gpu = use_gpu\n        if use_gpu:\n            try:\n                _places = os.environ[\"CUDA_VISIBLE_DEVICES\"]\n                int(_places[0])\n                print(\"use gpu: \", use_gpu)\n                print(\"CUDA_VISIBLE_DEVICES: \", _places)\n                cfg.gpu_mem = 8000\n            except:\n                raise RuntimeError(\n                    \"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.\"\n                )\n        cfg.ir_optim = True\n        cfg.enable_mkldnn = enable_mkldnn\n\n        self.text_recognizer = TextRecognizer(cfg)\n\n    def merge_configs(\n        self,\n    ):\n        # default cfg\n        backup_argv = copy.deepcopy(sys.argv)\n        sys.argv = sys.argv[:1]\n        cfg = parse_args()\n\n        update_cfg_map = vars(read_params())\n\n        for key in update_cfg_map:","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/hubserving/ocr_rec/module.py#L42-L78","documentation":"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.","triggerScenarios":"Constructing TextRecModule(use_gpu=True) with CUDA_VISIBLE_DEVICES unset, set to \"\", or starting with a non-digit.","commonSituations":"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.","solutions":["export CUDA_VISIBLE_DEVICES=0 (numeric device id) in the launching shell/container/pod spec.","Use use_gpu=False for CPU-only deployments.","Add the variable to the service unit or pod env block so restarts keep working."],"exampleFix":"# before\nmod = RecModule(use_gpu=True)  # no env -> RuntimeError\n\n# after\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nmod = RecModule(use_gpu=True)\n# or\nmod = RecModule(use_gpu=False)","handlingStrategy":"validation","validationCode":"import os\n\nv = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\nuse_gpu = bool(v) and v[0].isdigit()\nmod = RecModule(use_gpu=use_gpu)","typeGuard":null,"tryCatchPattern":"try:\n    mod = RecModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n        mod = RecModule(use_gpu=True)\n    else:\n        raise","preventionTips":["Derive use_gpu from the environment instead of hardcoding True.","Add the export to deployment scripts and container entrypoints.","Log the CUDA_VISIBLE_DEVICES value at service startup for debugging."],"tags":["paddleocr","hubserving","gpu","cuda","environment"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}