Because pip is an operating system command-line executable (run in your OS terminal/shell like Bash, zsh, PowerShell, or Command Prompt), not a Python language statement or keyword—so typing pip install requests at the Python REPL prompt causes Python’s parser to see two adjacent bare identifiers (pip followed by install) and immediately raise SyntaxError: invalid syntax.
Basic Example
# 1. Wrong (inside the Python >>> interpreter prompt — raises SyntaxError):
# >>> pip install requests
# File "<stdin>", line 1
# pip install requests
# ^^^^^^^
# SyntaxError: invalid syntax
# 2. Recommended: Exit the Python REPL first (`exit()` or Ctrl+D / Ctrl+Z+Enter),
# then run `python -m pip` directly in your OS terminal / shell:
python -m pip install requests
# 3. Special environments (Jupyter Notebook / IPython / Google Colab ONLY):
# Use the `%pip` magic command inside a notebook cell:
# %pip install requests
Key Details & Edge Cases
- Always run
python -m pip install <pkg>in your OS terminal instead of barepip:- After typing
exit()(or pressingCtrl+Don Linux/macOS orCtrl+Z+Enteron Windows) to leave theprompt, runpython -m pip install <package>(orpy -m pip install <package>on Windows). Invokingpipas a module via-mguarantees the package installs into that exact Python interpreter’ssite-packagesrather than a mismatched global Python.
- After typing
- Jupyter Notebook, IPython, and Google Colab (
%pipvs.!pip):- Interactive notebooks accept IPython magic commands that regular
pythonscripts do not. In Jupyter/Colab, prefer%pip install requests(line magic) over!pip install requests(shell bang), because%pipinstalls directly into the active notebook kernel’s environment, whereas!pipcan accidentally target a different system Python.
- Interactive notebooks accept IPython magic commands that regular
- Programmatically installing packages from inside a
.pyscript (only when truly necessary):- If an automation or bootstrap script genuinely must invoke
pipfrom Python code, spawn a subprocess targetingsys.executablerather than usingos.system("pip ..."):import subprocess import sys subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"]) - Note: Avoid installing packages dynamically inside normal application modules—declare dependencies in
requirements.txtorpyproject.tomland install them ahead of time in your virtual environment.
- If an automation or bootstrap script genuinely must invoke
- Same rule applies to other CLI tools (
python,pytest,uv,git,conda):- Any command-line program (
pytest,python script.py,uv add ...,git status) must be executed at your OS shell prompt ($,%,PS C:\>, orC:\>), never inside theinteractive Python prompt.
- Any command-line program (
Comparison of Where and How to Run pip install
| Environment | Command to Run | Works? | Targets Active Python Env? | Best Used When |
|---|---|---|---|---|
| OS Terminal (Bash, zsh, PowerShell, CMD) (Recommended) | python -m pip install <pkg> |
Yes | Yes (exact python invoked) |
Installing packages for all standard Python scripts, .venvs, and CLI workflows. |
Python REPL ( prompt) |
pip install <pkg> |
No (SyntaxError) |
N/A | Never—exit the REPL (exit()) and run in your OS shell instead. |
| Jupyter Notebook / IPython / Colab | %pip install <pkg> |
Yes | Yes (bound to active kernel) | Installing dependencies inside interactive data-science notebooks. |
Inside a .py Automation Script |
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg]) |
Yes | Yes (sys.executable) |
Self-bootstrapping setup scripts or isolated test harnesses. |
You must log in or register to comment.
