{"record":{"id":"1b69f12acafc9ab3","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s-1b69f1","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/structure_table/module.py","lineNumber":63,"sourceCode":"    author_email=\"paddle-dev@baidu.com\",\n    type=\"cv/structure_table\",\n)\nclass TableSystem(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        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.table_sys = _TableSystem(cfg)\n\n    def merge_configs(self):\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:\n            cfg.__setattr__(key, update_cfg_map[key])\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/hubserving/structure_table/module.py#L45-L81","documentation":"Raised during __init__ of the structure_table (table recognition) hubserving module when use_gpu=True and CUDA_VISIBLE_DEVICES fails validation. Because the env-var read and int(first-char) are wrapped in a bare except, an unset variable, empty string, or non-digit-leading value all become this RuntimeError.","triggerScenarios":"Building the table-recognition module with use_gpu=True while CUDA_VISIBLE_DEVICES is unset or malformed.","commonSituations":"Table-OCR serving on a GPU node without the export in the startup script; moving a working CPU service to GPU without updating environment configuration.","solutions":["export CUDA_VISIBLE_DEVICES=0, then construct with use_gpu=True.","Construct with use_gpu=False on CPU-only hosts.","Set the variable inside the process (os.environ) before module creation if you cannot change the shell."],"exampleFix":"# before\nmod = TableModule(use_gpu=True)  # env unset -> RuntimeError\n\n# after\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\nmod = TableModule(use_gpu=True)\n# or\nmod = TableModule(use_gpu=False)","handlingStrategy":"validation","validationCode":"import os\n\ndef safe_use_gpu(flag: bool) -> bool:\n    v = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\n    return flag and bool(v) and v[0].isdigit()\n\nmod = TableModule(use_gpu=safe_use_gpu(True))","typeGuard":null,"tryCatchPattern":"try:\n    mod = TableModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        mod = TableModule(use_gpu=False)\n    else:\n        raise","preventionTips":["Never assume the GPU env is inherited — set it explicitly in launch configs.","Run a `python -c` env probe before starting GPU services.","Centralize the use_gpu decision in one helper shared by all modules."],"tags":["paddleocr","ppstructure","table","gpu","cuda","environment"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}