Skip to content

Commit

Permalink
Fix when pip is invoked as python /path/to/pip/
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abadger committed May 9, 2020
1 parent 5cac4dc commit 0f74f40
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
# Resulting path is the name of the wheel itself
# Add that to sys.path so we can import pip
path = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, path)
# __package__ could have also been None if pip was invoked with the path
# to the pip directory. ie: as python /path/to/pip/
if path.endswith('.whl'):
sys.path.insert(0, path)

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

Expand Down

0 comments on commit 0f74f40

Please sign in to comment.