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:

<code>python -c "print('kiran')"</code>

2. Use Python as a calculator

Run quick calculations from the command line:

<code>python -c "print(5 * 4 + 100)"</code>

3. Debug with the -i flag

Keep the interpreter open after a script finishes to inspect variables:

<code>python -i my_script.py</code>

4. Get help for any module

Show a module’s documentation from the command line:

<code>python -c "help('math')"</code>

5. Measure code execution time

Use the -m timeit module to benchmark snippets:

<code>python -m timeit "y = [x**2 for x in range(2000)]"</code>

6. Quiet interactive mode with -q

Suppress the startup banner:

<code>python -q</code>

7. List all installed modules

Show every package installed in the current environment:

<code>python -m pip list</code>

8. Run a module directly

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

<code>python -m http.server</code>

9. Generate a random number

Use the random module inline:

<code>python -c "import random; print(random.randint(1, 1000))"</code>

10. Check the Python version

<code>python --version</code>

11. Activate a virtual environment

<code>source venv/bin/activate</code>

12. Pretty‑print JSON files

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

13. Compress files with gzip

<code>python -c "import gzip, shutil; shutil.copyfileobj(open('file.txt','rb'), gzip.open('file.txt.gz','wb'))"</code>

14. Extract ZIP archives

<code>python -m zipfile -e my_file.zip ./output_folder</code>

15. Generate a quick password

<code>python -c "import secrets; print(secrets.token_urlsafe(16))"</code>

16. Check syntax without running code

<code>python -m py_compile my_script.py</code>

17. Count lines in a file

<code>python -c "print(len(open('file.txt').readlines()))"</code>

18. Test for primality

<code>python -c "print(all(101 % i != 0 for i in range(2, int(101**0.5)+1)))"</code>

19. Launch the REPL anywhere

<code>python</code>

20. Read a CSV file

<code>python -c "import csv; print(list(csv.reader(open('my_file.csv'))))"</code>

21. Sort a file alphabetically

<code>python -c "print(sorted(open('my_file.txt').readlines()))"</code>

22. Send a quick HTTP GET request

<code>python -c "import requests; print(requests.get('https://google.com').text)"</code>

23. Monitor a script’s memory usage

<code>python -m tracemalloc my_script.py</code>

24. Show the current time

<code>python -c "import time; print(time.ctime())"</code>

25. Simulate a dice roll

<code>python -c "import random; print(random.randint(1, 6))"</code>

26. Convert Celsius to Fahrenheit

<code>python -c "c=30; print(f'{c}C = {(c * 9/5) + 32}F')"</code>

27. Create a progress bar

<code>python -c "import time; [print(f'\r{i}%', end='') or time.sleep(0.1) for i in range(101)]"</code>

28. Generate a QR code

<code>python -c "import qrcode; qrcode.make('https://google.com').save('qrcode.png')"</code>

29. Send an email

<code>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()"</code>

30. Compute a factorial

<code>python -c "import math; print(math.factorial(5))"</code>

31. Plot a quick chart

<code>python -c "import matplotlib.pyplot as plt; plt.plot([1,2,3],[4,5,6]); plt.show()"</code>

32. Search for files

<code>python -c "import glob; print(glob.glob('*.txt'))"</code>

33. Check network speed

<code>python -c</code>
CLIPythonproductivitycommand linetips
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

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.