What does `python -m` do, and why prefer `python -m pip`?
BeginnerConceptEnvironments, dependencies & packaging
Answer
python -m module runs a module as a script using that interpreter's sys.path. python -m pip install ... guarantees pip installs into the same interpreter you are running, avoiding "I installed it but ModuleNotFoundError" when pip on PATH belongs to a different Python. Other useful ones: python -m venv, python -m http.server 8000, python -m json.tool file.json, python -m pytest, python -m cProfile.
More on Environments, dependencies & packaging
What is a virtual environment and why do we always use one?BeginnerWhat does `error: externally-managed-environment` mean?Beginner`requirements.txt` vs `pyproject.toml`: what goes where?IntermediateWhy is `pip freeze > requirements.txt` not a great lock strategy?IntermediateWhat do version specifiers like `~=`, `==`, `>=,<` mean?BeginnerWhat are `pipx` and `uv`, and when do you use them?Intermediate