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 bare pip:
    • After typing exit() (or pressing Ctrl+D on Linux/macOS or Ctrl+Z + Enter on Windows) to leave the >>> prompt, run python -m pip install <package> (or py -m pip install <package> on Windows). Invoking pip as a module via -m guarantees the package installs into that exact Python interpreter’s site-packages rather than a mismatched global Python.
  • Jupyter Notebook, IPython, and Google Colab (%pip vs. !pip):
    • Interactive notebooks accept IPython magic commands that regular python scripts do not. In Jupyter/Colab, prefer %pip install requests (line magic) over !pip install requests (shell bang), because %pip installs directly into the active notebook kernel’s environment, whereas !pip can accidentally target a different system Python.
  • Programmatically installing packages from inside a .py script (only when truly necessary):
    • If an automation or bootstrap script genuinely must invoke pip from Python code, spawn a subprocess targeting sys.executable rather than using os.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.txt or pyproject.toml and install them ahead of time in your virtual environment.
  • 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:\>, or C:\>), never inside the >>> interactive Python prompt.

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.