{"record":{"id":"b9373f0264dd47d4","repo":"google/python-fire","slug":"fire-was-passed-a-filename-which-could-not-be-foun","errorCode":null,"errorMessage":"Fire was passed a filename which could not be found.","messagePattern":"Fire was passed a filename which could not be found\\.","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"fire/__main__.py","lineNumber":107,"sourceCode":"    IOError: if the file or module can not be found or imported.\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 os.path.exists(module_or_filename):\n    # importlib.util.spec_from_file_location requires .py\n    if not module_or_filename.endswith('.py'):\n      try:  # try as module instead\n        return import_from_module_name(module_or_filename)\n      except ImportError:\n        raise ValueError('Fire can only be called on .py files.')\n\n    return import_from_file_path(module_or_filename)\n\n  if os.path.sep in module_or_filename:  # Use / to detect if it was a filename.\n    raise OSError('Fire was passed a filename which could not be found.')\n\n  return import_from_module_name(module_or_filename)  # Assume it's a module.\n\n\ndef main(args):\n  \"\"\"Entrypoint for fire when invoked as a module with python -m fire.\"\"\"\n\n  if len(args) < 2:\n    print(cli_string)\n    sys.exit(1)\n\n  module_or_filename = args[1]\n  module, module_name = import_module(module_or_filename)\n\n  fire.Fire(module, name=module_name, command=args[2:])\n\n\nif __name__ == '__main__':","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/google/python-fire/blob/716bbc23d7eca949fdb682172283c8d18f742cb6/fire/__main__.py#L89-L125","documentation":"When the argument to fire.import_module contains a path separator but does not exist on disk, fire concludes it was intended as a filename and raises OSError('Fire was passed a filename which could not be found.') rather than trying it as a module name. This distinguishes 'path-like but missing' from the dotted-module case.","triggerScenarios":"python -m fire dir/subdir/mod (or any string containing /) where dir/subdir/mod does not exist relative to the cwd — a typo in the directory, a moved file, or running from the wrong directory.","commonSituations":"Running the CLI from a different cwd than the project root (CI, containers, cron); refactoring that renamed or moved files without updating the command; Windows/Unix path separator mixups.","solutions":["Check the path exists: `ls dir/subdir/mod.py`.","Run the command from the correct working directory, or use an absolute path.","If you meant a module, use dots not slashes: `python -m fire pkg.mod`."],"exampleFix":"// before\npython -m fire src/modle.py serve   # typo\n// after\npython -m fire src/model.py serve","handlingStrategy":"validation","validationCode":"import os\narg = 'src/model.py'\nif os.path.sep in arg and not os.path.exists(arg):\n    raise SystemExit(f'File not found: {os.path.abspath(arg)}')","typeGuard":"def is_existing_pathlike_target(arg: str) -> bool:\n    return os.path.sep not in arg or os.path.exists(arg)","tryCatchPattern":"try:\n    module, name = fire_import.import_module(arg)\nexcept OSError as e:\n    print(f'{e} — check cwd and spelling of {arg}'); sys.exit(2)","preventionTips":["Launch CLI commands from the project root; set cd in scripts/CI.","Use absolute paths in cron/containers.","Use dots (pkg.mod) for modules; slashes only for real files."],"tags":["python","cli","file-not-found","path"],"backgroundTag":"file-not-found","analyzedSha":"716bbc23d7eca949fdb682172283c8d18f742cb6","analyzedAt":"2026-08-28T21:57:47.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}