{"record":{"id":"86b16eb6f78e5962","repo":"PaddlePaddle/PaddleOCR","slug":"environment-variable-cuda-visible-devices-is-not-s","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/kie_ser/module.py","lineNumber":64,"sourceCode":"    type=\"cv/KIE_SER\",\n)\nclass KIESer(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.ser_predictor = SerPredictor(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":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/hubserving/kie_ser/module.py#L46-L82","documentation":"Raised by the PaddleHub kie_ser module when initializing with use_gpu=True and the CUDA_VISIBLE_DEVICES environment variable is missing, empty, or its first character is not a digit. The check is a bare try/except around os.environ[\"CUDA_VISIBLE_DEVICES\"] plus int(_places[0]), so a KeyError (unset) and a ValueError (e.g. \"\", \"all\", \"NoDevFiles\") both land in the same generic message. It is a deployment-configuration error, not a code bug.","triggerScenarios":"Starting hubserving with use_gpu=True without exporting CUDA_VISIBLE_DEVICES; setting it to \"\" or \"all\" (int('a') fails); docker/k8s containers where the variable was not passed through; set to a value like \"1,2\" works, but \"-1\" fails because int('-') raises.","commonSituations":"GPU serving in Docker without -e CUDA_VISIBLE_DEVICES=0; Kubernetes pods missing the env entry; following CPU-oriented quickstart docs then flipping use_gpu; variable set to \"all\" on newer drivers.","solutions":["Export the variable before starting the service: export CUDA_VISIBLE_DEVICES=0 (single GPU) and restart","Pass it into the container/pod: docker run -e CUDA_VISIBLE_DEVICES=0 ... / env in the pod spec","If no GPU is intended, start the module with use_gpu=False instead","Ensure the value starts with a digit (\"0\", \"0,1\"); avoid \"\", \"all\", or \"-1\""],"exampleFix":"# before\ncuda: docker run -p 8866:8866 kie_ser_ser:latest  # env missing -> RuntimeError\n\n# after\ndocker run -p 8866:8866 -e CUDA_VISIBLE_DEVICES=0 --gpus all kie_ser_ser:latest","handlingStrategy":"validation","validationCode":"import os\n\ndef gpu_env_ok() -> bool:\n    v = os.environ.get(\"CUDA_VISIBLE_DEVICES\", \"\")\n    return len(v) > 0 and v[0].isdigit()\n\nassert gpu_env_ok() or not USE_GPU, \"set CUDA_VISIBLE_DEVICES=0 before GPU serving\"","typeGuard":null,"tryCatchPattern":"try:\n    module = SerPredictorModule(use_gpu=True)\nexcept RuntimeError as e:\n    if \"CUDA_VISIBLE_DEVICES\" in str(e):\n        os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n        module = SerPredictorModule(use_gpu=True)\n    else:\n        raise","preventionTips":["Set CUDA_VISIBLE_DEVICES=0 in the service unit/entrypoint script","Pass env explicitly in docker run / pod specs","Never use \"\", \"all\", or \"-1\" with these hub modules","Add a startup preflight check for the env var when use_gpu is true"],"tags":["gpu","environment","deployment","python","hubserving"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}