When trying to install a Python package, you might encounter a SyntaxError: invalid syntax after typing pip install <package-name>. This is a very common mistake for beginners and occurs because the pip install command is being run in the wrong place: inside a Python interactive shell (REPL) instead of the system's command-line terminal.
The pip install command is a tool to be run from your terminal (like Command Prompt on Windows or Terminal on macOS/Linux), not a piece of Python code to be executed by the Python interpreter. This guide will clarify the difference and show you the simple fix.
Understanding the Error: System Terminal vs. Python REPL
It is crucial to distinguish between these two environments:
- System Terminal (or Command Line): This is the text-based interface for your operating system where you run commands like
ls,cd,git, orpip. The prompt typically looks like$on macOS/Linux orC:\>on Windows. - Python REPL (Read-Eval-Print Loop): This is an interactive session inside the Python interpreter. You start it by typing
pythonorpython3in your system terminal. Its purpose is to execute Python code directly. The prompt is typically>>>.
The SyntaxError occurs because pip install numpy is a system command, not valid Python syntax. When you type it at the >>> prompt, the Python interpreter tries to parse it as Python code and fails.
Reproducing the SyntaxError
The error happens when you first start a Python session and then try to run pip install.
Example of incorrect steps:
$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> pip install numpy
File "<stdin>", line 1
pip install numpy
^^^^^^^
SyntaxError: invalid syntaxThe Solution: Exit the Python REPL and Run in the Terminal
The fix is to first exit the Python REPL to return to your system terminal, and then run the pip install command.
The Correct Steps:
>>> exit()
$ pip install numpyOutput:
Collecting numpy
Downloading numpy-1.24.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 15.1 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-1.24.2Special Case: Installing Packages in Jupyter Notebooks
Jupyter Notebooks and IPython provide a convenient feature that allows you to run shell commands directly from a code cell by prefixing the command with an exclamation mark (!).
Solution for Jupyter:
!pip install numpyCollecting numpy
...
Successfully installed numpy-1.24.2Conclusion
| If you are in... | The correct action is... |
|---|---|
The Python REPL (prompt is >>>) |
Exit first with exit(), then run pip install ... in the terminal. |
The System Terminal (prompt is $ or C:\>) |
Run pip install ... directly. |
| A Jupyter Notebook cell | Prefix the command with !: !pip install .... |
The SyntaxError: invalid syntax with pip install is a simple environmental mistake. By ensuring you run system commands in the system terminal and Python code in the Python REPL, you can easily avoid this error.