Skip to main content

Python pip: How to Resovle "SyntaxError: invalid syntax" When Using pip install

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, or pip. The prompt typically looks like $ on macOS/Linux or C:\> on Windows.
  • Python REPL (Read-Eval-Print Loop): This is an interactive session inside the Python interpreter. You start it by typing python or python3 in 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:

# You start in your system terminal
$ python3

# Python starts, and the prompt changes to '>>>'
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.
>>>

# Now, you incorrectly run the pip command inside the Python REPL
>>> pip install numpy
File "<stdin>", line 1
pip install numpy
^^^^^^^
SyntaxError: invalid syntax

The 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:

# Step 1: You are inside the Python REPL (indicated by '>>>'). Exit it.
>>> exit()

# Step 2: You are now back in your system terminal (indicated by '$').
# Run the pip install command here.
$ pip install numpy

Output:

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.2
note

If the pip command is not found in your terminal, it may not be in your system's PATH. The most reliable way to run pip is by using the Python interpreter as a launcher: python3 -m pip install numpy This command should be run from the system terminal ($), not the Python REPL (>>>).

Special 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:

# In a Jupyter Notebook cell
!pip install numpy

Output (will appear below the cell):

Collecting numpy
...
Successfully installed numpy-1.24.2
note

This is a special feature of the IPython kernel that Jupyter uses; it does not work in the standard Python REPL.

Conclusion

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 cellPrefix 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.