Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion fire/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ def import_from_file_path(path):

Returns:
Tuple[ModuleType, str]: returns the imported module and the module name,
usually extracted from the path itself.
usually extracted from the path itself. If the file is __main__.py, the
package name (parent directory) is used instead.
"""

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

module_name = os.path.basename(path)

# If the file is __main__.py, use the parent directory name as the module name
# This fixes the issue where "python -m mypackage" shows "__main__.py" in usage
if module_name == '__main__.py':
module_name = os.path.basename(os.path.dirname(path))

spec = util.spec_from_file_location(module_name, path)

if spec is None or spec.loader is None:
Expand Down
4 changes: 4 additions & 0 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def Fire(component=None, command=None, name=None, serialize=None):
"""
name = name or os.path.basename(sys.argv[0])

# If the script name is __main__.py (python -m package), use the package name instead
if name == '__main__.py':
name = os.path.basename(os.path.dirname(sys.argv[0]))

# Get args as a list.
if isinstance(command, str):
args = shlex.split(command)
Expand Down
Loading