Eight Ways to Use Python Built‑in Modules Without Writing Code
This article demonstrates eight practical examples of leveraging Python's built‑in command‑line modules—such as -m, telnetlib, http.server, json.tool, idlelib, zipapp, encodings.rot_13, base64, sysconfig, and zipfile—to perform useful tasks without writing any Python source code.
In recent years Python has become popular because it lets developers achieve complex functionality with minimal code, and its ecosystem embraces tools that hide implementation details behind friendly interfaces. This tutorial shows how to use eight built‑in Python features directly from the command line, requiring no source code at all.
0. Python CLI "-m" Parameter
Using the python -m option runs a module as a script. By invoking modules that support command‑line operation, we can access many utilities without writing code.
1. Service Port Testing
Instead of installing telnet, Python's telnetlib module can test outbound traffic. Example: python -m telnetlib -d 142.250.70.174 443 Running the command shows a successful connection to Google’s port 443; using an invalid port (e.g., 999) raises an error.
2. Launch a Local Web Server
Python can start a simple HTTP server without any code: python -m http.server The server listens on port 8000 and serves files from the current directory, making it easy to share local resources.
3. Validate and Format JSON Strings
When a JSON string is unformatted, python -m json.tool can pretty‑print it. Example:
echo '{"name": {"first_name":"Chris", "last_name":"Tao"} "age":33}' | python -m json.toolAfter fixing the missing comma, the command produces nicely indented JSON.
4. Create a Text Editor
Python’s idlelib (based on Tkinter) can launch a simple UI editor:
mkdir get_time_app && python -m idlelib get_time_app/print_time.pyAfter editing and saving, the file can be inspected with cat get_time_app/print_time.py.
5. Build an Executable Application
Using the built‑in zipapp module, a directory can be packaged into a single .pyz file:
python -m zipapp get_time_app -m "print_time:main"The resulting .pyz can be run with python get_time_app.pyz.
6. Encode and Decode Strings or Files
Python provides simple encoders. ROT13 encryption via encodings.rot_13: echo "I am Chris" | python -m encodings.rot_13 Base64 encoding/decoding:
echo "I am Chris" | python -m base64 echo "SSBhbSBDaHJpcwo=" | python -m base64 -dFiles can also be encoded/decoded with the same module.
7. Retrieve System Metadata
System configuration details are available via: python -m sysconfig For a concise view of Python’s install and site paths:
python -m site8. File Compression
Python can create and extract zip archives without external tools:
python -m zipfile -c get_time_app.zip get_time_app python -m zipfile -e get_time_app.zip get_time_app_extractedAfter extraction, the original files can be verified with ls and cat. Python also supports tar and gzip formats.
Conclusion
The article demonstrates how Python’s built‑in modules enable a variety of useful tasks without writing any code, offering convenient shortcuts for developers in many scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
