Skip to content

Commit 0f74f40

Browse files
committed
Fix when pip is invoked as python /path/to/pip/
Because pip ships with an __main__.py file, one valid way to invoke it is: python /usr/lib/python3.6/site-packages/pip/. This results in __package__ being the empty string This, in turn, triggers the code to add the pip wheel to sys.path. Unfortunately, while this is fine to do when it's restricted to a wheel just containing pip, it isn't okay to do when it is site-packages as that will mean that other things in site-packages could end up overriding things in the stdlib (*cough*enum34*cough*). Check file extension to limit when we end up adding the extra path to sys.path.
1 parent 5cac4dc commit 0f74f40

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/pip/__main__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
# Resulting path is the name of the wheel itself
1919
# Add that to sys.path so we can import pip
2020
path = os.path.dirname(os.path.dirname(__file__))
21-
sys.path.insert(0, path)
21+
# __package__ could have also been None if pip was invoked with the path
22+
# to the pip directory. ie: as python /path/to/pip/
23+
if path.endswith('.whl'):
24+
sys.path.insert(0, path)
2225

2326
from pip._internal.cli.main import main as _main # isort:skip # noqa
2427

0 commit comments

Comments
 (0)