How to use Python -m
Published on Aug. 22, 2023, 12:16 p.m.
The python -m
option is used to run a Python module as a script. This is useful when you want to execute a module directly from the command line.
Here’s an example of how to use python -m
to run the json.tool
module:
python -m json.tool input.json
Here, the json.tool
module is being used to pretty-print the contents of the input.json
file. The -m
option tells Python to run the module as a script.
You can also use python -m
to run other modules that you’ve created. For example, if you have a module called my_module.py
, you can run it like this:
python -m my_module arg1 arg2
Here, the my_module
module is being run with the arguments arg1
and arg2
.
Overall, the python -m
option is a convenient way to run Python modules as standalone scripts, without having to write a separate script file.