Execution of Python code with -m option or not
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running Python with python script.py and running it with python -m package.module are not equivalent. Both execute code, but they set up imports, module names, and package context differently.
That difference matters most when your code lives inside a package. If you choose the wrong form, relative imports may fail, sys.path may not be what you expect, and the program may behave differently depending on where it was launched.
Direct Script Execution
When you run a file directly, Python executes that file path as the main program.
Inside the script, __name__ becomes "__main__", and the directory containing the script is placed near the front of sys.path.
That is fine for standalone files, quick experiments, and single-module utilities. It becomes awkward when the file is really part of a package and expects package-relative imports.
For example, suppose you have this structure:
If app.py contains a relative import like from .utils import helper, running it directly usually fails.
Module Execution With -m
When you use -m, Python resolves the target through the import system and then executes it as the main module.
That changes two important things:
- Python treats
mypkg.appas part of packagemypkg - import resolution behaves as package code expects
This is why -m is the recommended way to run code that lives inside a package.
Example: Relative Imports
Here is a minimal example that works with -m:
Run it from the project root with:
If you instead try python mypkg/app.py, Python treats the file as a top-level script, so the relative import has no package context and usually raises an import error.
__name__ And __package__
In both styles, the executed module usually sees __name__ == "__main__". The difference is that module execution with -m also sets package-related metadata appropriately.
That means package-relative imports and resource lookups behave more naturally. This is the key reason many tools, frameworks, and library docs instruct you to run modules with python -m ....
Standard Library And Installed Modules
-m is also how you run modules that are not represented by a file path you want to type directly. Common examples include:
In those cases, -m asks Python to locate the module the same way imports work, then execute it.
When To Use Each Style
Use direct file execution when:
- the code is a standalone script
- package context is irrelevant
- you want the simplest possible command
Use -m when:
- the target lives inside a package
- you rely on relative imports
- you want behavior consistent with normal Python imports
- you are invoking a standard library or installed module as a command
Common Pitfalls
- Running a package module as a file and then blaming the import statement.
- Assuming
python file.pyandpython -m package.fileset up the same import context. - Launching
python -mfrom the wrong working directory so the package is not importable. - Using absolute imports that accidentally work only because of a specific
sys.pathlayout. - Forgetting that
-mtakes a module name, not a file path.
Summary
- '
python script.pyruns a file directly as the main program.' - '
python -m package.moduleruns code through Python's import system.' - '
-mis usually the right choice for modules inside packages.' - Relative imports often fail with direct file execution but work with
-m. - Use direct execution for simple standalone scripts and
-mfor package-aware entry points.

