Skip to content

More intelligently insert the pip path into sys.path #8241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/8214.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix installing packages which have a pyproject.toml when enum34 is installed.
20 changes: 18 additions & 2 deletions src/pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import sysconfig

# Remove '' and current working directory from the first entry
# of sys.path, if present to avoid using current directory
Expand All @@ -13,12 +14,27 @@
# If we are running from a wheel, add the wheel to sys.path
# This allows the usage python pip-*.whl/pip install pip-*.whl
if __package__ == '':
# Retrieve the index in sys.path where all stdlib paths reside
# (DESTSHARED is where some extension modules like math live).
stdlib_path_indexes = []
stdlib_paths = (sysconfig.get_path('stdlib'),
sysconfig.get_path('platstdlib'),
sysconfig.get_config_var('DESTSHARED'))
for path in (p for p in stdlib_paths if p is not None):
try:
stdlib_path_indexes.append(sys.path.index(path))
except ValueError:
continue

# __file__ is pip-*.whl/pip/__main__.py
# first dirname call strips of '/__main__.py', second strips off '/pip'
# 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)
pip_installed_path = os.path.dirname(os.path.dirname(__file__))

# Insert this pip's library path directly after the stdlib so that
# we import this pip's library even if another pip is installed.
sys.path.insert(max(stdlib_path_indexes) + 1, pip_installed_path)

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

Expand Down