Skip to content
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

Interface to select preferred "user" or "home" sysconfig scheme for an environment #87478

Closed
uranusjr mannequin opened this issue Feb 24, 2021 · 11 comments
Closed

Interface to select preferred "user" or "home" sysconfig scheme for an environment #87478

uranusjr mannequin opened this issue Feb 24, 2021 · 11 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@uranusjr
Copy link
Mannequin

uranusjr mannequin commented Feb 24, 2021

BPO 43312
Nosy @doko42, @pfmoore, @tiran, @stefanor, @mattip, @zooba, @uranusjr, @hroncok
PRs
  • bpo-43312: Functions returning default and preferred sysconfig schemes #24644
  • bpo-45413: Define "posix_venv", "nt_venv" and "venv" sysconfig installation schemes #31034
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-04-27.10:33:24.778>
    created_at = <Date 2021-02-24.09:26:40.824>
    labels = ['type-feature', 'library', '3.10']
    title = 'Interface to select preferred "user" or "home" sysconfig scheme for an environment'
    updated_at = <Date 2022-03-03.16:02:43.318>
    user = 'https://github.com/uranusjr'

    bugs.python.org fields:

    activity = <Date 2022-03-03.16:02:43.318>
    actor = 'hroncok'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-27.10:33:24.778>
    closer = 'ncoghlan'
    components = ['Library (Lib)']
    creation = <Date 2021-02-24.09:26:40.824>
    creator = 'uranusjr'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43312
    keywords = ['patch']
    message_count = 11.0
    messages = ['387611', '387612', '387613', '387615', '387617', '387646', '387804', '388360', '388361', '389745', '392047']
    nosy_count = 9.0
    nosy_names = ['doko', 'paul.moore', 'christian.heimes', 'stefanor', 'piotr.dobrogost', 'mattip', 'steve.dower', 'uranusjr', 'hroncok']
    pr_nums = ['24644', '31034']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue43312'
    versions = ['Python 3.10']

    @uranusjr
    Copy link
    Mannequin Author

    uranusjr mannequin commented Feb 24, 2021

    While trying to migrate pip’s internal usages of distutils to sysconfig,1 I noticed there isn’t a way for pip to select a scheme for sysconfig.get_paths() for pip install --target and pip install --user. I tried to implement some logic to "guess" a scheme ("posix_home" for --home, "nt_user" for --user when os.name is "nt", etc.), but eventually hit a wall trying to support alternative implementations.

    PyPy, for example, adds additional schemes "pypy" and "pypy_nt", and it’s not clear whether pip should use then for --home or not. @mattip helped clear this up for PyPy (which also prompts bpo-43307), but we are worried that other implementations may introduce even more special rules that causes problems, and it’s also not a good idea for pip to implement special logic for every implementation.

    I would propose two changes to sysconfig:

    1. Make sysconfig._get_default_scheme() a public function. This function will be documented for implementations to return a default scheme to use when none is given to sysconfig.get_paths().
    2. Add a new function sysconfig.get_preferred_schemes() for implementations to return preferred schemes for prefix, home, and user installations. This function should return a dict[str, str] with three keys "prefix", "home", and "user", and their values the scheme names to use.

    I would be happy to work on a PR and iterate on the design if this sounds like a reasonable idea. For CPython, the implementation would be something like (to match distutils’s behaviour):

    def get_preferred_schemes():
        if os.name == "nt":
            return {
                "prefix": "nt",
                "home": "posix_home",
                "user": "nt_user",
            }
        return {
            "prefix": "posix_prefix",
            "home": "posix_home",
            "user": "posix_user",
        }

    @uranusjr uranusjr mannequin added 3.10 only security fixes stdlib Python modules in the Lib dir labels Feb 24, 2021
    @uranusjr
    Copy link
    Mannequin Author

    uranusjr mannequin commented Feb 24, 2021

    Adding Christian to the nosy list since IIRC you said something related to this a while ago.

    @mattip
    Copy link
    Contributor

    mattip commented Feb 24, 2021

    I wonder if the distro maintainers might be able to use this to reduce the patching they do for their schema?

    @tiran
    Copy link
    Member

    tiran commented Feb 24, 2021

    Do you need all three items or would "get_preferred_scheme(name: str) -> str" be sufficient?

    @uranusjr
    Copy link
    Mannequin Author

    uranusjr mannequin commented Feb 24, 2021

    That would work as well (raising KeyError or ValueError if name is not valid?)

    @zooba
    Copy link
    Member

    zooba commented Feb 25, 2021

    This seems like a good change to me. Making sure that the implementation is easily patchable sounds like a good plan, though I don't have any specific suggestions on what would be best.

    @uranusjr
    Copy link
    Mannequin Author

    uranusjr mannequin commented Feb 28, 2021

    How do we move the discussion forward? I would really want this to be included in 3.10. Assuming distutils is going to be removed in 3.12, pip would be left with an extremely short window if this has to wait for another year.

    @doko42
    Copy link
    Member

    doko42 commented Mar 9, 2021

    see also pypa/pip#9617

    @doko42
    Copy link
    Member

    doko42 commented Mar 9, 2021

    The Debian/Ubuntu packages have a local patch for distutils/setuptools introducing an --install-layout option. Maybe have the same for pip?

    The intention with renaming/moving site-packages to /usr/lib/python3/dist-packages is to avoid users damaging their system installation, like

    sudo python3 setup.py install
    sudo pip install

    overriding packages installed with the distro package manager. Just making this schema known, and making it the default would again allow pip acting on packages managed by the distro package manager.

    Otoh, there are use cases, where you want to use the Python provided by the Linux distro and run into issues with the custom dist-packages.

    • Creating a venv using the system Python probably should use
      the versioned site-packages.

    • Building on top of a docker image for e.g. a Python App, you
      probably want to install into the dist-packages provided by
      the system Python.

    So the problem to solve is

    • let a "sudo pip install" fail by default on the real system
    • let the same install succeed in a docker environment, or any
      other "image".
    • behave transparently on venv and virtualenv installations.

    @uranusjr
    Copy link
    Mannequin Author

    uranusjr mannequin commented Mar 29, 2021

    Gentle ping again :) I’ve also created a PR for this.

    The Debian/Ubuntu packages have a local patch for distutils/setuptools introducing an --install-layout option. Maybe have the same for pip?

    Pip already has a similar mechanism. The default layout is prefix and you can change the installation prefix with --prefix; home and user layouts can be specified with --home={base} and --user.

    So the problem to solve is

    • let a "sudo pip install" fail by default on the real system
    • let the same install succeed in a docker environment, or any other "image".

    These need to be done in pip, so we’ll have a separate discussion on them elsewhere.

    • behave transparently on venv and virtualenv installations.

    This is what this issue tries to address. A distribution can overwrite sysconfig.get_default_scheme() and sysconfig.get_preferred_scheme(variant) to return the correct scheme based on whether the current Python is in a virtual environment (by detecting sys.prefix and sys.base_prefix, I think).

    When in a virtual environment, it can return the same scheme as the upstream. Outside of a virtual environment, it can do whatever the platform sees fit, and pip (or whatever calls sysconfig) will install things into wherever it’s told to by the two functions.

    @pfmoore
    Copy link
    Member

    pfmoore commented Apr 27, 2021

    New changeset d925133 by Tzu-ping Chung in branch 'master':
    bpo-43312: Functions returning default and preferred sysconfig schemes (GH-24644)
    d925133

    @ncoghlan ncoghlan added the type-feature A feature request or enhancement label Apr 27, 2021
    @ncoghlan ncoghlan added the type-feature A feature request or enhancement label Apr 27, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants