This question is, or should be, about how to run pip from a python program. IDLE is not directly relevant to this version of the quesiton.
To expand on J. J. Hakala's comment: a command-line such as pip install pillow
is split on spaces to become sys.argv
. When pip is run as a main module, it calls pip.main(sys.argv[1:])
. If one imports pip, one may call pip.main(arg_line.split())
, where arg_line
is the part of the command line after pip
.
Last September (2015) I experimented with using this unintended API from another python program and reported the initial results on tracker issue 23551. Discussion and further results followed.
The problem with executing multiple commands in one process is that some pip commands cache not only sys.path, which normally stays constant, but also the list of installed packages, which normally changes. Since pip is designed to run one command per process, and then exit, it never updates the cache. When pip.main is used to run multiple commands in one process, commands given after the caching may use a stale and no-longer-correct cache. For example, list
after install
shows how things were before the install.
A second problem for a program that wants to examine the output from pip is that it goes to stdout and stderr. I posted a program that captures these streams into program variables as part of running pip.
Using a subprocess call for each pip command, as suggested by L_Pav, though less efficient, solves both problems. The communicate
method makes the output streams available. See the subprocess doc.
pip.main(["install", "--user", "somepackagenamehere"])
for example. – Tonjatonjesfrom pip._internal import main; main("install", "--user", "pypackage")
– Schnook