Skip to content

Inconsistent module resolution when running Python file directly vs. importing it (ModuleNotFoundError differences) #142260

@SahilKumarSingh01

Description

@SahilKumarSingh01

Bug report

Bug description:

Bug Report: Inconsistent Import Error Between Direct Execution and Module Import

Description

A Python script containing the line:

from six.moves import configparser

produces two different ModuleNotFoundError messages depending on how it is executed.

  • When run directly:
    ModuleNotFoundError: No module named 'six'

  • When imported via another script:
    ModuleNotFoundError: No module named 'six.moves'

Both scenarios use the exact same code, so this suggests inconsistent behavior in how Python handles imports in __main__ versus module import mode.

Steps to Reproduce

  1. Create a file named test_import.py:
from six.moves import configparser
  1. Run it directly:
python test_import.py
  1. Create another file named runner.py:
import test_import
  1. Run:
python runner.py
  1. Compare the error messages from both executions.

Expected Behavior

Python should produce consistent import error messages regardless of whether a module is run directly or imported.

Actual Behavior

Running directly reports:

ModuleNotFoundError: No module named 'six'

Importing through another script reports:

ModuleNotFoundError: No module named 'six.moves'

Environment

  • Python version: (fill in)
  • OS: Windows 10/11
  • Installation method: (official installer / store / embeddable zip / etc.)
  • No virtual environment active

Additional Notes

This issue appeared while debugging the gsutil tool bundled with another project.
Even after reinstalling all libraries, the inconsistent import error persists.

Image

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions