Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
What Is the @ Symbol in Python?
Next article icon

What is the use of Python -m flag?

Last Updated : 02 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Screenshot-2024-02-01-132809

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:

Screenshot-2024-02-01-132916

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:

Screenshot-2024-02-01-133030

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


Next Article
What Is the @ Symbol in Python?
author
harshitmongre
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • python

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 i
    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 Three dots(...) or Ellipsis in Python3
    In Python, ... (three dots) is called Ellipsis, a built-in object used as a placeholder. It’s useful when code isn’t complete yet, offering a cleaner alternative to pass. Since it's a real object (Ellipsis), it can also be used in comparisons or custom logic. Its type is ellipsis and Python treats i
    4 min read
  • What is the common header format of Python files?
    When writing Python scripts, it's important to maintain a clean and well-documented codebase. One of the key practices for achieving this is adding a header to each Python file. The header provides essential information about the script, such as its functionality, author and dependencies, which can
    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
  • Why Python Uses 'Self' as Default Argument
    In Python, when defining methods within a class, the first parameter is always self. The parameter self is a convention not a keyword and it plays a key role in Python’s object-oriented structure. Example: [GFGTABS] Python class Car: def __init__(self, brand, model): self.brand = brand # Set instanc
    3 min read
  • Types of Arguments in Python
    Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python . In this example, we will create a simple function in Python to check whether the number passed as an argument to the
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences