Why does pip install inside Python raise a SyntaxError?

Why does pip install inside Python raise a SyntaxError?

Why does pip install inside Python raise a SyntaxError

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.

Use the command line, not the Python shell (DOS, PowerShell in Windows).

C:Program FilesPython2.7Scripts> pip install XYZ

If you installed Python into your PATH using the latest installers, you dont need to be in that folder to run pip

Terminal in Mac or Linux

$ pip install XYZ

Why does pip install inside Python raise a SyntaxError?

As @sinoroc suggested correct way of installing a package via pip is using separate process since pip may cause closing a thread or may require a restart of interpreter to load new installed package so this is the right way of using the API: subprocess.check_call([sys.executable, -m, pip, install, SomeProject]) but since Python allows to access internal API and you know what youre using the API for you may want to use internal API anyway eg. if youre building own GUI package manager with alternative resourcess like https://www.lfd.uci.edu/~gohlke/pythonlibs/

Following soulution is OUT OF DATE, instead of downvoting suggest updates. see https://github.com/pypa/pip/issues/7498 for reference.


UPDATE: Since pip version 10.x there is no more get_installed_distributions() or main method under import pip instead use import pip._internal as pip.

UPDATE ca. v.18 get_installed_distributions() has been removed. Instead you may use generator freeze like this:

from pip._internal.operations.freeze import freeze

print([package for package in freeze()])

# eg output [pip==19.0.3]

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 pip.main([]) call:

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 -r / --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 pip.exe or pip3.exe regardless its python 2.x or 3.x, inside is the SAME single file __main__.py:

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == __main__:
    sys.argv[0] = re.sub(r(-script.pyw?|.exe)?$, , sys.argv[0])
    sys.exit(main())

Leave a Reply

Your email address will not be published.