This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add install scheme for virtual environments
Type: enhancement Stage: resolved
Components: Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: FFY00 Nosy List: FFY00, frenzy, gaborjbernat, hroncok, jaraco, petr.viktorin, stefanor, steve.dower
Priority: normal Keywords: patch

Created on 2021-10-08 17:50 by FFY00, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31034 merged hroncok, 2022-01-31 14:23
Messages (16)
msg403485 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2021-10-08 17:50
Python 3.10 introduced sysconfig._get_preferred_schemes[1], a mechanism to allow downstream distributors to overwrite the default install scheme.
This is done to support downstream modifications where distributors change the installation layout (eg. different site-packages directory). So, distributors will change the default scheme to one that correctly represents their layout.

This presents an issue for projects/people that need to bootstrap virtual environments, like virtualenv (see [2]). As distributors might now be customizing the default install scheme, there is no guarantee that the information returned by sysconfig.get_default_scheme/get_paths is correct for the virtual environment, the only guarantee we have is that it correct for the *current* environment. When bootstrapping a virtual environment, we need to know its layout, so that we can place the files in the correct locations.

The usual solution in situations like this would be to invoke the interpreter we are targeting to get the correct information (eg. path/to/python -m sysconfig), but that is not possible here as the environment does not exist yet -- we are the ones trying to create it.

To solve this issue, I propose the addition of a "virtual" or "venv" install scheme, for virtual environments. The idea is that virtual environments (defined by the presence of pyvenv.cfg, see [3][4]) will always use this scheme, they will use the paths specified by this scheme and sysconfig.get_default_scheme will always return it (_get_preferred_schemes would have no effect here!).
This makes it possible to know the virtual environment layout for another interpreter, via sysconfig.get_paths(scheme='virtual').

I am not cure if this should be adopted in 3.10 or 3.11, as it effectively makes it impossible to reliably construct virtual environments, and requires workarounds such as [5], that pretty much implements this proposal with non-standardized downstream patches.

[1] https://docs.python.org/3/library/sysconfig.html#sysconfig._get_preferred_schemes
[2] https://github.com/pypa/virtualenv/issues/2208
[3] https://docs.python.org/3/library/site.html?highlight=pyvenv.cfg
[4] https://docs.python.org/3/library/venv.html?highlight=pyvenv.cfg
[5] https://github.com/pypa/virtualenv/pull/2209
msg403486 - (view) Author: Miro Hrončok (hroncok) * Date: 2021-10-08 18:01
The existing install schemes contain values for all different kinds of OSes, somehow named according to them.

If we introduce a single "virtual"/"venv" scheme, it would need to have different contents on different OSes (e.g. Windows vs POSIX). I don't think that would cause any actual trouble, but it would be somewhat different than all the other schemes.

If we introduce multiple ones (e.g. "posix_venv" and "nt_venv") we would need an additional layer to get the one appropriate for the current platform.

Hence, I think having a single one is more pragmatic.
msg403513 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2021-10-09 00:30
Yes, we could have several schemes, but I think having only one is more sensible.

The implementation would be fairly easy. We would just copy the "nt" scheme if on Windows, otherwise "posix_prefix".
msg403720 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-10-12 10:22
Starting out with just "venv" doesn't mean we can't add "posix_venv"/"nt_venv" later (if e.g. someone needs to install into a filesystem for another platform).
msg412201 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-01-31 13:45
I'll try to draft this change for Python 3.11.
msg412202 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-01-31 14:26
I've created a draft PR in https://github.com/python/cpython/pull/31034

It is still missing tests and I have not checked it on Windows, hence still a draft.
msg412302 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-02-01 19:32
The PR is now ready for review.
msg412794 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2022-02-07 22:01
I think we want the scheme to be static and accessible on all platforms, like the others. So it probably should be 'nt_venv' and 'posix_venv' with additional/improved logic to help apps determine when they need each.
msg412805 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2022-02-08 02:19
I agree.
msg412896 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-02-09 10:25
> I think we want the scheme to be static and accessible on all platforms, like the others. So it probably should be 'nt_venv' and 'posix_venv' with additional/improved logic to help apps determine when they need each.

Why? (This is a real question, I genuinely don't know.)

To put this in context, this has been discussed since October, and there was agreement on how to change it.
Now a PR exists. It is tested. Also, since this fixes urgent breaking changes, patches for "venv" is already in Fedora and Ubuntu deadsnakes PPA, and virtualenv also supports it. Changing direction now will mean lots of work changing and re-testing anything. That's definitely possible, just frustrating, but if someone calls to change something again, we risk not making it in 3.11.

How can we ensure the decision won't change again?
How can these discussions be made more effective?
msg412911 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2022-02-09 14:52
All I can say is that I wasn't aware of this discussion, or it blurred into the other discussions and didn't seem to need any attention from me. 
Once I was pinged on the pull request (and I'm *trying* to be better at checking GitHub notifications, though CPython is still the repo that makes it very hard), I took a look.

I definitely don't want to say that I must be consulted on every sysconfig/site/getpath change (because I don't want to be!), but I hesitate to say that this was the wrong way for it to be caught. It's only "too late" after a release has included it, and up until then it's fine.

So I guess it can be avoided in the future by checking the surrounding code and seeing how it's used? In this case, the schemes are all approximately static (for a given version of Python), and the *selection* of a scheme is based on the platform. The proposed venv scheme itself is based on the platform, while selection is static. That stands out to me as a difference.
msg412933 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2022-02-09 17:49
I don't think the proposal is incompatible with what I discussed.

I haven't been super clear on my opinions on the implementation, so let me try to clarify them.

- I think that we should use a static scheme, accessible on all platforms.
- If this scheme needs to be independently defined for each platform, we should have different variants, available on all platforms, but still keep a generic named one, as an alias to the platform specific scheme
- We should not be re-using/aliasing existing schemes, particularly ones that are prone to downstream patching

So, my proposal would be to define a single static scheme, and changing the interpreter path initialization logic to hardcode its paths when on virtual environments.
If this presents any issue, and requires the scheme to be different for different platforms, we should add platform specific schemes and make the main one an alias to the correct scheme.

Hopefully that clarifies things up a bit. We should sort it out as soon as possible and update the PR, I don't think the PR as-is is the best approach.

What do you think?
msg413405 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-02-17 12:58
I've adapted the PR. See the latest commit (Instead of *venv* scheme, have *posix_venv* and *nt_venv*).
msg414510 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-03-04 08:58
Steve, could you take a look at the PR discussion and chime in about how this should be done?
msg415479 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-03-18 09:53
New changeset 48d926269963cfe7a49c0a4f34af4fe9b832399b by Miro Hrončok in branch 'main':
bpo-45413: Define "posix_venv", "nt_venv" and "venv" sysconfig installation schemes (GH-31034)
https://github.com/python/cpython/commit/48d926269963cfe7a49c0a4f34af4fe9b832399b
msg415509 - (view) Author: Filipe Laíns (FFY00) * (Python triager) Date: 2022-03-18 16:07
With PR 31034 merged, we can now mark this as resolved.

As mentioned in the PR, there are still some concerns about maintainability and avoiding similar issues to happen in the future. That can be done later, as people find time to work on it.

Thanks!
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89576
2022-03-18 16:07:36FFY00setstatus: open -> closed
resolution: fixed
messages: + msg415509

stage: patch review -> resolved
2022-03-18 09:53:35petr.viktorinsetmessages: + msg415479
2022-03-04 08:58:41petr.viktorinsetmessages: + msg414510
2022-02-17 12:58:58hroncoksetmessages: + msg413405
2022-02-09 17:49:23FFY00setmessages: + msg412933
2022-02-09 14:52:38steve.dowersetmessages: + msg412911
2022-02-09 10:25:48petr.viktorinsetmessages: + msg412896
2022-02-08 02:19:59FFY00setmessages: + msg412805
2022-02-07 22:01:53steve.dowersetnosy: + steve.dower
messages: + msg412794
2022-02-01 19:32:17hroncoksetmessages: + msg412302
2022-01-31 14:26:08hroncoksetmessages: + msg412202
2022-01-31 14:23:59hroncoksetkeywords: + patch
stage: patch review
pull_requests: + pull_request29217
2022-01-31 13:45:10hroncoksetmessages: + msg412201
2022-01-20 21:02:46stefanorsetnosy: + stefanor
2021-10-18 10:15:46frenzysetnosy: + frenzy
2021-10-12 10:22:49petr.viktorinsetnosy: + petr.viktorin
messages: + msg403720
2021-10-09 00:30:45FFY00setmessages: + msg403513
2021-10-08 18:01:46hroncoksetmessages: + msg403486
2021-10-08 17:50:08FFY00settitle: Add install scheme for virtual environment -> Add install scheme for virtual environments
2021-10-08 17:50:02FFY00create