{"record":{"id":"a141992ed0e0c056","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s-a14199","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_det/module.py","lineNumber":62,"sourceCode":"    type=\"cv/text_detection\",\n)\nclass OCRDet(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_detector = TextDetector(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":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/hubserving/ocr_det/module.py#L44-L80","documentation":"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.","triggerScenarios":"Starting the ocr_det serving module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset, empty, or begins with a non-numeric character.","commonSituations":"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.","solutions":["export CUDA_VISIBLE_DEVICES=0 before launching the service and keep use_gpu=True.","Pass use_gpu=False to run on CPU if no GPU is available.","For docker: add -e CUDA_VISIBLE_DEVICES=0 to the docker run command."],"exampleFix":"# before\nmod = DetModule(use_gpu=True)  # env unset -> RuntimeError\n\n# after\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nmod = DetModule(use_gpu=True)\n# or\nmod = DetModule(use_gpu=False)","handlingStrategy":"validation","validationCode":"import os\n\ndef gpu_ready() -> bool:\n    v = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\n    return len(v) > 0 and v[0].isdigit()\n\nuse_gpu = gpu_ready()  # decide before constructing the module","typeGuard":null,"tryCatchPattern":"try:\n    mod = DetModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        mod = DetModule(use_gpu=False)  # graceful CPU fallback\n    else:\n        raise","preventionTips":["Export CUDA_VISIBLE_DEVICES in the exact shell that launches the service.","Pass device env vars explicitly in docker run / k8s pod specs.","Add a preflight check script that validates GPU env before service start."],"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"}