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
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
The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
Use the command line, not the Python shell (DOS, PowerShell in Windows).
C:\Program Files\Python2.7\Scripts> pip install XYZ
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 install XYZ
If you want to use pip inside the Python interpreter, try this:
import pip package_names=['selenium', 'requests'] #packages to install pip.main(['install'] + [package_names] + ['--upgrade']) # --upgrade to install or update existing packages
If you need to update every installed package, use following:
import pip for i in pip.get_installed_distributions(): pip.main(['install', i.key, '--upgrade'])
If you want to stop installing other packages if any installation fails, use it in one single
import pip package_names = [i.key for i in pip.get_installed_distributions()] pip.main(['install'] + package_names + ['--upgrade'])
Note: When you install from list in file with
--requirement parameter you do NOT need open() function.
pip.main(['install', '-r', 'filename'])
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 unpack
pip3.exe regardless it’s python 2.x or 3.x, inside is the SAME single file
# -*- coding: utf-8 -*- import re import sys from pip import main if __name__ == '__main__': sys.argv = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv) sys.exit(main())
To run pip as of Python 3.4, just follow the instructions on Python’s page: Installing Python Modules.
python -m pip install SomePackage
Note that this is run from the command line and not the python shell (the reason for syntax error in the original question).