Sobes.tech
Junior — Middle

Can you explain the purpose of the -m flag in Python script execution?

sobes.tech AI

Answer from AI

The -m flag in Python is used to run a module as a script. Instead of specifying the path to a file, you specify the module name, and Python executes its contents.

This is convenient when you need to run a module that is in the module search path (PYTHONPATH) or in an installed package.

Example:

python -m http.server

This command will run the built-in http.server module, which starts a simple HTTP server.

Thus, -m allows you to run modules as if they were scripts, simplifying the use of built-in and custom modules.