What is the use of Python -m flag?
Last Updated :
02 Feb, 2024
Python, a versatile and widely used programming language, provides a plethora of features and command-line options to facilitate various tasks. One such option that might pique your interest is the -m switch. In this article, we will explore what Python -m is and how it can be used, accompanied by four illustrative code examples.
What is Python -m?
The -m switch in Python is a command-line option that allows you to run a module as a script. This means you can execute Python code directly from the command line without the need for an external script file. By using -m, you can invoke Python modules as standalone programs.
Python -M Examples
Below are some of the ways by which we can understand about Python -m command in Python:
- Running Python Modules
- A module within a Package
- Running Built-in Modules
Running Python Files
In this example, the -m switch allows us to run the example_module as a script, passing the argument "World" to the greet function. Consider a Python module named example_module. To execute it using the -m switch, create a file named example_module.py with the following content:
Python3
# example_module.py
def greet(name):
print(f"Hello, {name}!")
if __name__ == "__main__":
import sys
greet(sys.argv[1])
Now, run the module using the -m switch:
python -m example_module World
Output:

Module within a Package
Let's extend our understanding to modules within packages. Consider a package named example_package with a module submodule:
Python3
# example_package/submodule.py
def print_message():
print("This is a submodule.")
if __name__ == "__main__":
print_message()
To run the submodule using -m, use the following command:
python -m example_package.submodule
Output:

The -m switch enables us to execute modules within packages, specifying the package path with dots.
Running Built-In Modules
The -m switch is not limited to user-defined modules; it can also be used with built-in modules. For instance, you can run the timeit module to measure the execution time of a code snippet:
Python3
python -m timeit "print('Hello, World!')"
This will display the time taken to execute the specified code snippet.
Output:

Conclusion
In conclusion, the Python -m switch is a powerful tool that allows you to run modules as scripts directly from the command line. Whether it's user-defined modules, modules within packages, or built-in modules, the -m switch enhances the flexibility of Python's command-line interface. Experiment with this feature to streamline your development and testing processe
Similar Reads
What is Python Used For? Python is a highly versatile programming language that's used across many fields and industries due to its readability, simplicity, and the vast availability of libraries.Here are some areas where Python is commonly used:Web Development: Python offers frameworks like Django and Flask, which make it
2 min read
What is setup.py in Python? Introduction In Python, setup.py is a module used to build and distribute Python packages. It typically contains information about the package, such as its name, version, and dependencies, as well as instructions for building and installing the package. This information is used by the pip tool, whic
3 min read
What is a Python egg? Python eggs were like special containers for Python software, making it easier to share and install packages. They came about to tackle issues with an older format called "eggs," which stood for Extensible Gateway Interface. In this article, we will see what is Python egg and how we use it. What is
3 min read
What's the Zen of Python? Whether you come from a strong programming background or are just a beginner, you must be aware of Python programming language because of its attractive syntax, ease of reading, and simplicity; which most developers use across the globe today. A collection of aphorisms known as "The Zen of Python" e
9 min read
What Is the @ Symbol in Python? Understanding the significance of the "@" symbol in Python is crucial for writing code that's both tidy and efficient, especially when you're dealing with decorators or performing matrix multiplication operations in your programs. In this article, we'll delve into the role of the "@" symbol in Pytho
3 min read
What is Python? Its Uses and Applications Python is a programming language that is interpreted, object-oriented, and considered to be high-level. What is Python? Python is one of the easiest yet most useful programming languages and is widely used in the software industry. People use Python for Competitive Programming, Web Development, and
8 min read
Usage of __main__.py in Python Many of us have worked with creating their own custom module in Python and is well familiar with the candidate '__init__.py'. If you do not know then let's get a brief and short description of '__init__.py' before diving deep into the concerned topic. Actually, when we try to import a module to Pyth
3 min read
What is a Python wheel? Python wheels are a pre-built binary package format for Python modules and libraries. They are designed to make it easier to install and manage Python packages, by providing a convenient, single-file format that can be downloaded and installed without the need to compile the package from source code
6 min read
Why is Python So Popular? One question always comes into people's minds Why Python is so popular? As we know Python, the high-level, versatile programming language, has witnessed an unprecedented surge in popularity over the years. From web development to data science and artificial intelligence, Python has become the go-to
7 min read
File Mode in Python In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho
5 min read