Skip to content

Commit 9b10a2e

Browse files
committed
Add a new *public* main() method
This is mostly compatible with the prior use of `pip.main()` but not exactly identical. If the recommended mode of use for invoking `pip` programmatically is to call `sys.executable` with `-m pip`, then that mode of use *must* be supported by `pip`. `subprocess.check_output(...)` behaves very similarly, but of course not identically, to `pip.main(...)` on pip<9.0.2 This is therefore a mostly compatible interface which alleviates some transitional hurdles for those using virtualenv or other 3rd party packages which depend upon `pip.main`. The only packages which will remain broken are those which use `pip.main` from an environment in which subprocess cannot be spawned. The new public `pip.main` is very clearly declared as the *only* public python API for pip and is noted as having been added for better backwards compatibility with known (but not supported) usage.
1 parent 0007825 commit 9b10a2e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/pip/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1+
from __future__ import absolute_import
2+
import subprocess
3+
import sys
4+
15
__version__ = "10.0.0.dev0"
6+
7+
8+
def main(*args):
9+
"""
10+
This function is included primarily for backwards compatibility.
11+
12+
Although anything using `pip.main()` in pip<9.0.2 was engaged in a
13+
non-supported mode of use, as a pragmatic accommodation, `pip.main()` is
14+
being *added* as a *new* piece of functionality with very similar behaviors
15+
and call signature.
16+
17+
Why use check_call instead of check_output?
18+
It's behavior is slightly closer to the older `pip.main()` behavior,
19+
printing output information directly to stdout.
20+
21+
check_call was added in py2.5 and is supported through py3.x , so it's more
22+
compatible than some alternatives like subprocess.run (added in py3.5)
23+
"""
24+
return subprocess.check_call([sys.executable, '-m', 'pip'] + list(args))
25+
26+
27+
__all__ = ('main',)

0 commit comments

Comments
 (0)