{"record":{"id":"e0e822f95d0bebeb","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s-e0e822","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_cls/module.py","lineNumber":60,"sourceCode":"    type=\"cv/text_angle_cls\",\n)\nclass OCRCls(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_classifier = TextClassifier(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_cls/module.py#L42-L78","documentation":"Raised during __init__ of the ocr_cls hubserving module when use_gpu=True but the CUDA_VISIBLE_DEVICES environment variable cannot be validated. The code reads os.environ[\"CUDA_VISIBLE_DEVICES\"] and does int(_places[0]) inside a bare try/except, so a KeyError (variable unset/empty) or ValueError (first char not a digit, e.g. an empty string) both collapse into this RuntimeError.","triggerScenarios":"Constructing the module (or starting the hub serving container) with use_gpu=True while CUDA_VISIBLE_DEVICES is unset, set to an empty string, or starts with a non-digit character.","commonSituations":"Deploying the hubserving docker/service on a GPU machine without exporting CUDA_VISIBLE_DEVICES; CI shells that scrub environment variables; setting the variable to a value like \"cuda:0\" instead of \"0\".","solutions":["export CUDA_VISIBLE_DEVICES=0 (a plain device id) before starting the service, then re-run with use_gpu=True.","If no GPU is intended, pass use_gpu=False when instantiating the module.","Verify with: python -c \"import os; v=os.environ.get('CUDA_VISIBLE_DEVICES',''); print(v, v[:1].isdigit())\"."],"exampleFix":"# before\nmod = ClsModule(use_gpu=True)  # env unset -> RuntimeError\n\n# after\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nmod = ClsModule(use_gpu=True)\n# or, on CPU:\nmod = ClsModule(use_gpu=False)","handlingStrategy":"validation","validationCode":"import os\n\ndef cuda_env_ok() -> bool:\n    v = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\n    return bool(v) and v[0].isdigit()\n\nassert cuda_env_ok() or not USE_GPU, \"set CUDA_VISIBLE_DEVICES=<id> or use use_gpu=False\"","typeGuard":null,"tryCatchPattern":"try:\n    module = ClsModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        # fall back to CPU instead of crashing the service\n        module = ClsModule(use_gpu=False)\n    else:\n        raise","preventionTips":["Set CUDA_VISIBLE_DEVICES in the service definition (systemd/docker/k8s), not ad-hoc shells.","Use plain numeric ids (\"0\", \"0,1\"), never \"cuda:0\".","Probe the env var in a startup health check before enabling use_gpu."],"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"}