Skip to content

Allow pip to pick up the config from the sys.base_prefix. #9753

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
wants to merge 2 commits into from
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
10 changes: 6 additions & 4 deletions docs/html/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ pip allows you to set all command line option defaults in a standard ini
style config file.

The names and locations of the configuration files vary slightly across
platforms. You may have per-user, per-virtualenv or global (shared amongst
all users) configuration:
platforms. You may have per-user, per-site (e.g per virtualenv) or global
(shared amongst all users) configuration:

**Per-user**:

Expand All @@ -521,11 +521,13 @@ To find its location:
You can set a custom path location for this config file using the environment
variable ``PIP_CONFIG_FILE``.

**Inside a virtualenv**:
**Site (e.g inside a virtualenv)**

* On Unix and macOS the file is :file:`$VIRTUAL_ENV/pip.conf`
* On Windows the file is: :file:`%VIRTUAL_ENV%\\pip.ini`

Additionally ``sys.base_prefix`` is searched for a pip config of the same name.

**Global**:

* On Unix the file may be located in :file:`/etc/pip.conf`. Alternatively
Expand All @@ -546,7 +548,7 @@ the following order:

1. The global file is read
2. The per-user file is read
3. The virtualenv-specific file is read
3. The site file is read

Each file read overrides any values read from previous files, so if the
global timeout is specified in both the global file and the per-user file
Expand Down
1 change: 1 addition & 0 deletions news/9752.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Virtual environments may now inherit their pip config from the base environment.
11 changes: 9 additions & 2 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ def get_configuration_files():
for path in appdirs.site_config_dirs('pip')
]

site_config_file = os.path.join(sys.prefix, CONFIG_BASENAME)
site_config_files = []
if getattr(sys, 'base_prefix', sys.prefix) != sys.prefix:
site_config_files.append(
os.path.join(sys.base_prefix, CONFIG_BASENAME),
)
# A virtual environment config takes precedence over a base_prefix config.
site_config_files.append(os.path.join(sys.prefix, CONFIG_BASENAME))

legacy_config_file = os.path.join(
os.path.expanduser('~'),
'pip' if WINDOWS else '.pip',
Expand All @@ -86,7 +93,7 @@ def get_configuration_files():
)
return {
kinds.GLOBAL: global_config_files,
kinds.SITE: [site_config_file],
kinds.SITE: site_config_files,
kinds.USER: [legacy_config_file, new_config_file],
}

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ def test_site_modification(self):

self.configuration.set_value("test.hello", "10")

# get the path to site config file
# get the path to site config file, mirroring the behaviour
# in _get_parser_to_modify.
assert mymock.call_count == 1
assert mymock.call_args[0][0] == (
get_configuration_files()[kinds.SITE][0]
get_configuration_files()[kinds.SITE][-1]
)

def test_user_modification(self):
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from contextlib import contextmanager
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -433,20 +434,29 @@ def test_client_cert(self):

class TestOptionsConfigFiles:

def test_venv_config_file_found(self, monkeypatch):
@pytest.mark.parametrize("in_venv", (True, False))
def test_venv_config_file_found(self, monkeypatch, in_venv):
# strict limit on the global config files list
monkeypatch.setattr(
pip._internal.utils.appdirs, 'site_config_dirs',
lambda _: ['/a/place']
)
if in_venv:
monkeypatch.setattr(sys, 'base_prefix', '/some/other/place')
else:
monkeypatch.setattr(sys, 'base_prefix', sys.prefix)

cp = pip._internal.configuration.Configuration(isolated=False)

files = []
for _, val in cp.iter_config_files():
files.extend(val)

assert len(files) == 4
if in_venv:
# With venvs we get the config from the base_prefix also.
assert len(files) == 5
else:
assert len(files) == 4

@pytest.mark.parametrize(
"args, expect",
Expand Down