33 Essential Python CLI Tricks You Should Bookmark
Discover 33 practical Python command‑line tricks—from running one‑liners without files and using the interpreter as a calculator, to measuring execution time, managing virtual environments, generating passwords, and sending HTTP requests—each illustrated with ready‑to‑copy commands that boost productivity and streamline everyday scripting tasks.
Today I compiled 33 useful Python command‑line tricks. Save them for quick reference.
1. Run Python code without a file
You can execute Python commands directly from the terminal without creating a script file:
python -c "print('kiran')"2. Use Python as a calculator
Run quick calculations from the command line:
python -c "print(5 * 4 + 100)"3. Debug with the -i flag
Keep the interpreter open after a script finishes to inspect variables:
python -i my_script.py4. Get help for any module
Show a module’s documentation from the command line:
python -c "help('math')"5. Measure code execution time
Use the -m timeit module to benchmark snippets:
python -m timeit "y = [x**2 for x in range(2000)]"6. Quiet interactive mode with -q
Suppress the startup banner:
python -q7. List all installed modules
Show every package installed in the current environment:
python -m pip list8. Run a module directly
Execute a module without a script file, e.g., start a simple HTTP server:
python -m http.server9. Generate a random number
Use the random module inline:
python -c "import random; print(random.randint(1, 1000))"10. Check the Python version
python --version11. Activate a virtual environment
source venv/bin/activate12. Pretty‑print JSON files
python -m json.tool < input.json > output.json13. Compress files with gzip
python -c "import gzip, shutil; shutil.copyfileobj(open('file.txt','rb'), gzip.open('file.txt.gz','wb'))"14. Extract ZIP archives
python -m zipfile -e my_file.zip ./output_folder15. Generate a quick password
python -c "import secrets; print(secrets.token_urlsafe(16))"16. Check syntax without running code
python -m py_compile my_script.py17. Count lines in a file
python -c "print(len(open('file.txt').readlines()))"18. Test for primality
python -c "print(all(101 % i != 0 for i in range(2, int(101**0.5)+1)))"19. Launch the REPL anywhere
python20. Read a CSV file
python -c "import csv; print(list(csv.reader(open('my_file.csv'))))"21. Sort a file alphabetically
python -c "print(sorted(open('my_file.txt').readlines()))"22. Send a quick HTTP GET request
python -c "import requests; print(requests.get('https://google.com').text)"23. Monitor a script’s memory usage
python -m tracemalloc my_script.py24. Show the current time
python -c "import time; print(time.ctime())"25. Simulate a dice roll
python -c "import random; print(random.randint(1, 6))"26. Convert Celsius to Fahrenheit
python -c "c=30; print(f'{c}C = {(c * 9/5) + 32}F')"27. Create a progress bar
python -c "import time; [print(f'\r{i}%', end='') or time.sleep(0.1) for i in range(101)]"28. Generate a QR code
python -c "import qrcode; qrcode.make('https://google.com').save('qrcode.png')"29. Send an email
python -c "import smtplib; s = smtplib.SMTP('smtp.gmail.com', 587); s.starttls(); s.login('your_email', 'your_password'); s.sendmail('from', 'to', 'message'); s.quit()"30. Compute a factorial
python -c "import math; print(math.factorial(5))"31. Plot a quick chart
python -c "import matplotlib.pyplot as plt; plt.plot([1,2,3],[4,5,6]); plt.show()"32. Search for files
python -c "import glob; print(glob.glob('*.txt'))"33. Check network speed
python -cSigned-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.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
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.
