google/python-fire · error · OSError

Given file path does not exist.

Error message

Given file path does not exist.

What it means

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.

Source

Thrown at fire/__main__.py:57

"python -m fire packageA/packageB/module.py" """


def import_from_file_path(path):
  """Performs a module import given the filename.

  Args:
    path (str): the path to the file to be imported.

  Raises:
    IOError: if the given file does not exist or importlib fails to load it.

  Returns:
    Tuple[ModuleType, str]: returns the imported module and the module name,
      usually extracted from the path itself.
  """

  if not os.path.exists(path):
    raise OSError('Given file path does not exist.')

  module_name = os.path.basename(path)

  spec = util.spec_from_file_location(module_name, path)

  if spec is None or spec.loader is None:
    raise OSError('Unable to load module from specified path.')

  module = util.module_from_spec(spec)  # pylint: disable=no-member
  spec.loader.exec_module(module)

  return module, module_name


def import_from_module_name(module_name):
  """Imports a module and returns it and its name."""
  module = importlib.import_module(module_name)
  return module, module_name

View on GitHub (pinned to 716bbc23d7)

Solutions

  1. Verify the path exists: run `ls -l <path>` from the same working directory.
  2. Use an absolute path to eliminate cwd ambiguity.
  3. If you meant a module, not a file, pass the dotted module name (no path separator) instead.

Example fix

// before
python -m fire ./ap.py serve
// after
python -m fire /home/user/project/api.py serve
Defensive patterns

Strategy: validation

Validate before calling

import os
path = './api.py'
if not os.path.exists(path):
    raise SystemExit(f'Fire target not found: {os.path.abspath(path)}')

Type guard

def is_existing_file(p: str) -> bool:
    return isinstance(p, str) and os.path.isfile(p)

Try / catch

try:
    module, name = fire_import.import_from_file_path(path)
except OSError as e:
    print(f'Bad fire target path: {path} ({e})'); sys.exit(2)

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of google/python-fire@716bbc23d7 (2026-08-28). Data as JSON: /api/errors/73329d51a923793d. Report an issue: GitHub.