{"record":{"id":"73329d51a923793d","repo":"google/python-fire","slug":"given-file-path-does-not-exist","errorCode":null,"errorMessage":"Given file path does not exist.","messagePattern":"Given file path does not exist\\.","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"fire/__main__.py","lineNumber":57,"sourceCode":"\"python -m fire packageA/packageB/module.py\" \"\"\"\n\n\ndef import_from_file_path(path):\n  \"\"\"Performs a module import given the filename.\n\n  Args:\n    path (str): the path to the file to be imported.\n\n  Raises:\n    IOError: if the given file does not exist or importlib fails to load it.\n\n  Returns:\n    Tuple[ModuleType, str]: returns the imported module and the module name,\n      usually extracted from the path itself.\n  \"\"\"\n\n  if not os.path.exists(path):\n    raise OSError('Given file path does not exist.')\n\n  module_name = os.path.basename(path)\n\n  spec = util.spec_from_file_location(module_name, path)\n\n  if spec is None or spec.loader is None:\n    raise OSError('Unable to load module from specified path.')\n\n  module = util.module_from_spec(spec)  # pylint: disable=no-member\n  spec.loader.exec_module(module)\n\n  return module, module_name\n\n\ndef import_from_module_name(module_name):\n  \"\"\"Imports a module and returns it and its name.\"\"\"\n  module = importlib.import_module(module_name)\n  return module, module_name","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/google/python-fire/blob/716bbc23d7eca949fdb682172283c8d18f742cb6/fire/__main__.py#L39-L75","documentation":"fire.import_module raises OSError when asked to import a file path that does not exist on disk. fire/__main__.py imports the target file or module before dispatching a CLI command, and it fails fast here so the rest of the pipeline never runs against a nonexistent path.","triggerScenarios":"Calling python -m fire <path> or fire.Fire() via main() with a path argument where os.path.exists(path) is False — e.g. a typo'd filename, a deleted/moved script, or a relative path from the wrong working directory.","commonSituations":"Running `python -m fire ./scrip.py` (typo), pointing at a file in another directory without the correct relative path, or invoking from a different cwd than expected (cron/CI environments).","solutions":["Verify the path exists: run `ls -l <path>` from the same working directory.","Use an absolute path to eliminate cwd ambiguity.","If you meant a module, not a file, pass the dotted module name (no path separator) instead."],"exampleFix":"// before\npython -m fire ./ap.py serve\n// after\npython -m fire /home/user/project/api.py serve","handlingStrategy":"validation","validationCode":"import os\npath = './api.py'\nif not os.path.exists(path):\n    raise SystemExit(f'Fire target not found: {os.path.abspath(path)}')","typeGuard":"def is_existing_file(p: str) -> bool:\n    return isinstance(p, str) and os.path.isfile(p)","tryCatchPattern":"try:\n    module, name = fire_import.import_from_file_path(path)\nexcept OSError as e:\n    print(f'Bad fire target path: {path} ({e})'); sys.exit(2)","preventionTips":["Use absolute paths when invoking python -m fire.","Run `os.path.exists(path)` or `ls` before dispatching.","Pin and document the expected working directory in scripts/CI."],"tags":["python","file-not-found","cli","path"],"backgroundTag":"file-not-found","analyzedSha":"716bbc23d7eca949fdb682172283c8d18f742cb6","analyzedAt":"2026-08-28T21:57:47.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}