pip install SyntaxError: invalid syntax (tested)
pip install x raising SyntaxError: invalid syntax means you typed it into Python (the >>> prompt or a .py file), not into a terminal. Exit Python and run python -m pip install x in the shell, or use subprocess with sys.executable from code.
1. Reproduce
b1.sh feeds pip commands to the interactive Python prompt (python -i, the >>> prompt) and runs a .py file containing pip install six:
# 1) pip typed at the Python prompt (>>>)
echo 'pip install six' | python -i -q
# 2) python -m pip typed at the prompt fails the same way
echo 'python -m pip install six' | python -i -q
# 3) the same line saved in a .py file
python b1.py
Output (Python 3.13.5, pip 26.2.2, fresh venv):
>>> File "<stdin>", line 1
pip install six
^^^^^^^
SyntaxError: invalid syntax
>>>
>>> File "<stdin>", line 1
python -m pip install six
^^^
SyntaxError: invalid syntax
>>>
File "/tmp/ps416/b1.py", line 1
pip install six
^^^^^^^
SyntaxError: invalid syntax
pip install six is a shell command, not Python code. Python reads pip as a variable name, then sees a second name install right after it, which is not valid syntax, so the caret points at install. python -m pip install fails the same way (caret on pip). The >>> before the traceback is the giveaway: you are inside Python.
2. Fix: run it in the shell
Leave the Python prompt (exit() or Ctrl-D; Ctrl-Z then Enter on Windows) and run the command in the terminal. Using python -m pip installs into the same interpreter that python starts:
# Fix: leave the >>> prompt and run pip in the shell
python -m pip install -q six && python -c "import six; print('six', six.__version__)"
six 1.17.0
3. If you really need to install from inside a script
Run pip as a subprocess of the current interpreter with sys.executable, so the package lands in the environment the script is running in:
# Installing from inside Python: run pip as a subprocess of THIS interpreter
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "six"])
import six
print("six", six.__version__, "installed into", sys.prefix)
six 1.17.0 installed into /tmp/ps416/ve
Prefer a requirements.txt or pyproject.toml for anything you ship; installing at runtime is fine for one-off scripts.
Related: pip install failed, pip error: externally-managed-environment.
Frequently asked questions
Why does pip install give SyntaxError: invalid syntax?
pip is a command-line program. Typed at the Python >>> prompt or put in a .py file, Python parses it as code and fails at the word after pip.
How do I fix pip SyntaxError in the Python shell?
Exit Python with exit() or Ctrl-D, then run python -m pip install PACKAGE in the terminal.
How do I pip install from inside a Python script?
subprocess.check_call([sys.executable, "-m", "pip", "install", "PACKAGE"]) installs into the interpreter running the script.
Written with AI assistance. Measured on 2026-10-23 with Python 3.13.5 and pip 26.2.2 in a fresh venv on Linux. The scripts ran verbatim and the output was pasted unedited. PlainTables is our extension; pip is free.