I'm trying to use pip to install a package. I try to run pip install
from the Python shell, but I get a SyntaxError
. Why do I get this error? How do I use pip to install the package?
>>> pip install selenium
^
SyntaxError: invalid syntax
Try upgrade pip with the below command and retry
Use the command line, not the Python shell (DOS, PowerShell in Windows).
If you installed Python into your PATH using the latest installers, you don't need to be in that folder to run pip
Terminal in Mac or Linux
pip is run from the command line, not the Python interpreter. It is a program that installs modules, so you can use them from Python. Once you have installed the module, then you can open the Python shell and do
import selenium
.The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
UPDATE: Since pip version 10.x there is no more
get_installed_distributions()
ormain
method underimport pip
instead useimport pip._internal as pip
.If you want to use pip inside the Python interpreter, try this:
If you need to update every installed package, use following:
If you want to stop installing other packages if any installation fails, use it in one single
pip.main([])
call:Note: When you install from list in file with
-r
/--requirement
parameter you do NOT need open() function.Warning: Some parameters as simple
--help
may cause python interpreter to stop.Curiosity: By using
pip.exe
you actually use python interpreter and pip module anyway. If you unpackpip.exe
orpip3.exe
regardless it's python 2.x or 3.x, inside is the SAME single file__main__.py
:To run pip in Python 3.x, just follow the instructions on Python's page: Installing Python Modules.
Note that this is run from the command line and not the python shell (the reason for syntax error in the original question).