Fundamentals 4 min read

Five Practical Uses of Python’s -m Option

This article explains five practical ways to use Python's -m option, including launching a simple HTTP server, generating documentation with pydoc, debugging with pdb, timing code snippets via timeit, and reliably installing packages with python -m pip, especially in multi‑version environments.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Five Practical Uses of Python’s -m Option

Python offers many scenarios for the -m option; here are five useful ones.

First, a one‑line command can start a simple HTTP server: python -m http.server 8000 (in Python 2 the equivalent is python -m SimpleHTTPServer 8000 ).

After running the command, open http://localhost:8000 in a browser or use the machine’s IP address on the LAN to view the directory contents.

Second, python -m pydoc -p 9000 launches a web‑based documentation server that can be accessed via a browser.

Third, the debugger can be invoked with python -m pdb script.py , running the script in interactive debugging mode.

Fourth, timeit can be used from the command line, e.g., python -m timeit "'-'.join(str(i) for i in range(100))" , to compare the performance of different code snippets.

Fifth, using python -m pip install package ensures the package is installed for the specific Python interpreter, which is essential when multiple Python versions are present; this practice was advocated by Brett Cannon.

Additional promotional material follows, which is not part of the technical tutorial.

debuggingPythonhttp servercommand lineModulepip
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.