Skip to main content

How to Run a Python Module as a Script with python -m

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Some Python modules can be run like a program, by name, without knowing where their file is. python -m does that, for modules that come with Python and for your own. This guide runs one of each on Python 3.15.2.

Syntax​

Syntax
python -m <module> <arguments>
PartWhat it means
-m <module>Finds the module by its name, the same way import does, and runs it. Write the name without .py
<arguments>Optional. Everything after the module name goes to the module, not to Python

The Python documentation describes -m <module-name> as: "Locate the module using the standard import mechanism and execute its contents as the __main__ module. Since the argument is a module name, you must not give a file extension (.py)." About what follows it, the same page says that "all consecutive arguments will end up in sys.argv", the list a program reads its arguments from.

The Example​

First, the standard library's calendar module prints one month. Then a one-line module of your own, greetings.py, is run the same way.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Run a Module That Comes with Python​

The documentation says that "Many standard library modules contain code that is invoked on their execution as a script." calendar is one of them: given a year and a month, it prints that month.

Terminal
uv run python -m calendar 2026 12
Output
December 2026
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

The terminal after uv run python -m calendar 2026 12: the calendar for December 2026, with the month name and the 2nd highlighted in yellow and the day names in cyan.

2026 and 12 went to calendar, not to Python. In the terminal, the month name and today's date are highlighted and the day names are in colour: the calendar documentation says Python 3.14 started highlighting today's date, and Python 3.15 added the month and the days of the week. The highlighted 2 is the day this guide was written. The command works unchanged in PowerShell.

Step 2: Write Your Own Module​

Save this as greetings.py in the project folder:

greetings.py
print("Hello from the greetings module")

The greetings.py module in a macOS-style code window: one line that prints Hello from the greetings module.

The module's name is the file name without .py: greetings.

Step 3: Run It with -m​

Terminal
uv run python -m greetings
Output
Hello from the greetings module

The terminal after uv run python -m greetings: it prints Hello from the greetings module.

Python found greetings in the folder you ran the command from: the documentation says that with -m, "the current directory will be added to the start of sys.path", the list of places Python searches for modules. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Adding .py: python -m greetings.pyThe module runs and prints Hello from the greetings module, then Python reports Error while finding module specification for 'greetings.py' (ModuleNotFoundError: __path__ attribute not found on 'greetings' while trying to find 'greetings.py'). Try using 'greetings' instead of 'greetings.py' as the module name.Leave out .py: python -m greetings
A name Python can't find, such as a typo: python -m greeting/Users/you/source/python-basics/.venv/bin/python: No module named greetingCheck the spelling, and run the command from the folder that holds the module

FAQ​

Why does python -m greetings.py run the module before the error?​

Python reads the dot as the start of a submodule's name: greetings.py means a module called py inside a package called greetings. The import system documentation explains that for a dotted name, "Python first tries to import foo, then foo.bar". Importing greetings ran its print line. Then Python looked for py inside it and failed, because greetings is a single file, not a package.

What's the difference from python greetings.py?​

For this file, nothing you can see: uv run python greetings.py printed the same line. The difference is how Python finds it. A file name is a path on disk, while -m takes a module name and searches for it the way import does. That's what lets you run a module such as calendar without knowing where its file is.

Conclusion​

uv run python -m calendar 2026 12 ran a standard library module by name and passed it two arguments. uv run python -m greetings ran a module of your own, found in the current folder, as long as its name had no .py.

Additional Resources​