Python “-m” flag example and How to Use It

The -m flag in Python stands for the module name. When you pass the -m flag followed by a module name to the Python interpreter, it will locate the module and execute its content as the __main__ module. This allows you to run a module directly from the command line.

If the -m flag is given to the Python statement, the first element of sys.argv will be the full path to the module file (while the module file is being found, the first item will be set to “-m”).

When -c is used with a statement on the command-line interface, it executes the Python statement(s) given as a command. When a package name is provided instead of a normal module, the interpreter will execute the .main as the main module.

When invoking Python, you may specify any of these options,

python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]

When you call a command with -m module-name, the given module is located on the Python module path and executed as a script.

If you regularly install new modules, you notice one thing: every time you write an install command, you must see the -m flag. So, what does the -m flag mean in the python -m pip install <package> command? Or upgrading pip using the python -m pip install –upgrade pip.

Here the command may include multiple statements separated by newlines. Leading whitespace is significant in Python statements!

If you type python –help, you will get the following suggestions.

# More flags above

-m mod: run the library module as a script.

# More flags below


The -m flag in Python runs a module as a script. It allows you to execute a Python module as a standalone program rather than as a library to be imported into another program.

That is it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.