Fundamentals 9 min read

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.

Code Mala Tang
Code Mala Tang
Code Mala Tang
33 Essential Python CLI Tricks You Should Bookmark

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.py

4. 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 -q

7. List all installed modules

Show every package installed in the current environment:

python -m pip list

8. Run a module directly

Execute a module without a script file, e.g., start a simple HTTP server:

python -m http.server

9. Generate a random number

Use the random module inline:

python -c "import random; print(random.randint(1, 1000))"

10. Check the Python version

python --version

11. Activate a virtual environment

source venv/bin/activate

12. Pretty‑print JSON files

python -m json.tool < input.json > output.json

13. 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_folder

15. 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.py

17. 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

python

20. 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.py

24. 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 -c
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIproductivityTipscommand-line
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

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.