Skip to content

Python Basic Commands Cheat Sheet

Quick reference for the Python CLI commands used most often in day-to-day development.

Terminal window
python -m venv .venv # create a virtual environment in .venv/
.venv\Scripts\Activate.ps1 # activate (Windows PowerShell)
.venv\Scripts\activate # activate (Windows CMD)
source .venv/bin/activate # activate (macOS / Linux)
deactivate # deactivate the current environment
Terminal window
python myscript.py # run a script
python -i myscript.py # run then drop into interactive REPL
python -m module_name # run a module as a script (e.g. python -m http.server 8000)
python -c "print('hello')" # run a one-liner
Terminal window
python -m pip install requests # install a package
python -m pip install requests==2.32 # install a specific version
python -m pip install -r requirements.txt # install from a file
python -m pip install --upgrade requests # upgrade to latest
python -m pip uninstall requests # remove a package
python -m pip list # list installed packages
python -m pip show requests # details for one package
python -m pip freeze > requirements.txt # pin current env to a file
python -m pip install --upgrade pip # upgrade pip itself

Always use python -m pip rather than bare pip. This ensures the package manager runs under the same interpreter as your project, not a different one on the PATH.

Terminal window
python -m pip install pytest ruff black # install the tools into the active venv
python -m pytest # run the test suite (discovers test_*.py files)
python -m pytest -k orders -x # only tests matching "orders"; stop at first failure
python -m ruff check . # lint the project
python -m ruff check . --fix # lint and apply safe automatic fixes
python -m ruff format . # format with Ruff's formatter
python -m black . # or format with Black, if the project uses it

Pick one formatter per project (Ruff’s formatter or Black) and run it in CI with --check so local and CI results match.

Terminal window
python --version
python3 --version
python -m pip --version
Terminal window
python # open the REPL
exit() # or Ctrl-D (macOS/Linux) / Ctrl-Z Enter (Windows)
Terminal window
python -m http.server 8000 # simple HTTP server in the current directory
python -m site # show site-packages location
python -m ensurepip # ensure pip is installed
python -m json.tool file.json # pretty-print a JSON file
python -m py_compile script.py # check syntax without running
Terminal window
python -m pip config set global.index-url https://your-registry-url/
python -m pip config set global.trusted-host your-registry-host
python -m pip config set global.timeout 60