Python has some standard packages pre-installed in it. The Python community develops a lot of external packages to support users. These packages are published in the PyPI repo.
If you want to use these packages, you have to first install them on your system using the package management tools. Pip is a standard tool used in Python to manage external packages.
To install an external package, the pip command to be used is pip install <package-name>
However, in some cases, when the pip install <package-name> command is executed, the below error is seen.
SyntaxError: invalid syntax
In this guide, let us discuss various ways as to why this error is seen and different ways to fix the same.
Fix 1: Ensure that you run the command in the Command Prompt/Terminal.
If you enter this command in the Python interpreter, you get an invalid syntax error.
For example, look at the below image. Here, we enter the Python interpreter and then execute the command. We see an error as pip.exe is not within the Python shell.
To fix this error, try running the command outside the python shell.
1. To exit the shell, enter the below command.
2. Now, enter the command as shown below.
Fix 2: Use the -m option to specify the module name.
If the OS doesn’t locate the pip executable file directly, use “python -m <module-name>”, to look for the module name in the sys.path locations.
The command to be used is :
python -m pip install <package_name>
py -m pip install <package_name>
Fix 3: Execute the pip command from the “Scripts” directory.
If the PATH variable is not updated, run pip.exe from the Scripts folder.
1. Navigate to the directory where Python is installed in your system.
NOTE: If you see an error stating, The system cannot find the path specified, that is because the directory Appdata is hidden. Ensure you can view the hidden files and then run the command again.
Refer to this link to know how to view the hidden files in Windows.
2. Go to the Scripts directory.
3. Enter the below command.
pip install <package-name>
In this article, we have discussed different ways of fixing the pip install returning an invalid syntax error. If you feel there is some problem with the Python Installation, uninstall the Python and try installing it again.
We hope this article has been informative. Kindly comment and let us know the fix that helped you overcome the issue.
Thank you for Reading. Happy Pythoning!