Kodeclik Logo

Learn More

Fall 2025

Kodeclik Blog

What does -m mean in Python?

When running Python code from the command line, you’ll often come across the -m option and wonder what it does. You might see examples like:

python -m http.server

or

python -m pip install numpy

At first glance, -m looks mysterious — but it’s actually a powerful and convenient way to tell Python to run a module as a script, rather than just a file. To understand why this exists, we first need to understand how Python programs are structured.

Table of Contents


How Python programs are structured

What does “-m” do?

Why this is useful

Summary

How Python programs are structured

Sometimes you will see Python programs structured in the following manner:

# myscript.py
def greet():
    print("Hello!")

if __name__ == "__main__":
    greet()

This might appear strange but you will soon get used to it. To understand this program, you have to notice that sometimes Python programs (like the above myscript.py) are either run directly or imported into a larger Python program.

If the program is run directly, eg by doing:

python myscript.py

the system variable __name__ is set to "__main__". In which case the if() condition above gets triggered which calls the greet() function which in turn prints “Hello”.

If the program is instead imported as a module, eg by doing:

import myscript

in a larger Python program, __name__ is set to the module’s name (here, "myscript"). In which case, nothing is printed after the import command (because the “if” condition does not get triggered). Instead we only have access to the greet() function as a result of the import command (which can be used later in the program by calling the greet() function).

What does “-m” do?

The -m flag tells Python to locate a module and run it as the main program.This means Python treats that module the same way it treats a file you execute directly.

For instance, this command:

python -m module_name
What is -m in Python

Is equivalent to saying: “Find the module named module_name and run its code as if it were the main script.”

When you do this, Python temporarily sets __name__ = "__main__" inside that module, causing any if __name__ == "__main__": block to execute.

Why this is useful

First, you can run modules without knowing their exact path. For example:

python -m http.server 8080

starts a simple web server serving files from the current directory.

Second, you can ensure you’re using the correct version of tools like pip:

python -m pip install requests

This guarantees that you’re using the pip associated with your Python interpreter.

Finally, you can run your own package modules cleanly. Suppose you have:

myproject/
    __init__.py
    cli.py

You can run:

python -m myproject.cli

This will execute the cli.py file with __name__ = "__main__", so your entry-point logic works properly while still allowing relative imports within the package.

Summary

  • if __name__ == "__main__": lets a module behave differently when it’s run directly versus imported.
  • The -m flag tells Python to run a module as if it were run directly, setting __name__ = "__main__".
  • Together, they enable flexible, reusable, and well-structured code that works both as a library and as a standalone tool.

Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.