|
2 | 2 |
|
3 | 3 | import os
|
4 | 4 | import sys
|
| 5 | +import sysconfig |
5 | 6 |
|
6 | 7 | # Remove '' and current working directory from the first entry
|
7 | 8 | # of sys.path, if present to avoid using current directory
|
|
13 | 14 | # If we are running from a wheel, add the wheel to sys.path
|
14 | 15 | # This allows the usage python pip-*.whl/pip install pip-*.whl
|
15 | 16 | if __package__ == '':
|
| 17 | + # Retrieve the index in sys.path where all stdlib paths reside |
| 18 | + # (DESTSHARED is where some extension modules like math live). |
| 19 | + stdlib_path_indexes = [] |
| 20 | + stdlib_paths = (sysconfig.get_path('stdlib'), |
| 21 | + sysconfig.get_path('platstdlib'), |
| 22 | + sysconfig.get_config_var('DESTSHARED')) |
| 23 | + for path in (p for p in stdlib_paths if p is not None): |
| 24 | + try: |
| 25 | + stdlib_path_indexes.append(sys.path.index(path)) |
| 26 | + except ValueError: |
| 27 | + continue |
| 28 | + |
16 | 29 | # __file__ is pip-*.whl/pip/__main__.py
|
17 | 30 | # first dirname call strips of '/__main__.py', second strips off '/pip'
|
18 | 31 | # Resulting path is the name of the wheel itself
|
19 | 32 | # Add that to sys.path so we can import pip
|
20 |
| - path = os.path.dirname(os.path.dirname(__file__)) |
21 |
| - sys.path.insert(0, path) |
| 33 | + pip_installed_path = os.path.dirname(os.path.dirname(__file__)) |
| 34 | + |
| 35 | + # Insert this pip's library path directly after the stdlib so that |
| 36 | + # we import this pip's library even if another pip is installed. |
| 37 | + sys.path.insert(max(stdlib_path_indexes) + 1, pip_installed_path) |
22 | 38 |
|
23 | 39 | from pip._internal.cli.main import main as _main # isort:skip # noqa
|
24 | 40 |
|
|
0 commit comments