Skip to content

Commit 3240227

Browse files
rickeylevncoghlan
andauthored
Apply suggestions from code review
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
1 parent c542890 commit 3240227

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

peps/pep-0796.rst

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ requirement should be relaxed and relative path behavior allowed and defined.
4343
Second, such relative paths are a building block to enable portable virtual
4444
environments, i.e. copying a virtual environment as-is between hosts of
4545
compatible platforms. Portable venvs are appealing because virtual environments
46-
are the standard way for running Python applications. This provides several
46+
are a popular mechanism for running Python applications. This provides several
4747
benefits:
4848

4949
* The closer the development environment is to the non-development environment,
@@ -52,14 +52,16 @@ benefits:
5252
* The simpler the process of re-creating the environment, the more reliable
5353
software can be achieved, and the faster the process can be.
5454

55-
Making it trivial to copy a virtual environment from one host to another avoids
55+
Making it simpler to copy a virtual environment from one host to another mitigates
5656
these categories of problems. Additionally, the development tools to create a
5757
virtual environment and install its dependencies aren't needed on the host that
5858
intends to run the program.
5959

60-
Because the virtual environment doesn't require modifications to be usable, it
60+
When the virtual environment doesn't require modifications to be usable, it
6161
also allows more advanced deployment mechanisms, e.g. remote mounting and
62-
caching of artifacts.
62+
caching of artifacts. While this PEP on its own isn't sufficient to enable that, it
63+
allows tools like `bazel` or `venvstacks` to more easily prepare constrained
64+
environments that allow for such use cases.
6365

6466
Rationale
6567
=========
@@ -71,6 +73,11 @@ customizing how it's computed. Without the ability to specify where the
7173
supporting Python runtime files are, the interpreter can't finish startup,
7274
so other hook points (e.g. ``site`` initialization) never trigger.
7375

76+
Tools that currently look to enable virtual environment portability across machines do
77+
so either by relying on undocumented interpreter behaviour (`bazel`, omitting the `home`
78+
key entirely to trigger an implementation dependent fallback to resolving via a symlinked
79+
interpreter binary on non-Windows systems) or by requiring a post-installation script to
80+
be executed after the environment is placed in its target location (`venvstacks`).
7481
Specification
7582
=============
7683

@@ -97,10 +104,10 @@ For example, given
97104
i.e. it's equivalent to using that value verbatim in ``pyvenv.cfg``.
98105

99106

100-
Python Runtime Changes
107+
CPython Runtime Changes
101108
======================
102109

103-
The Python runtime itself *almost* already supports relative paths. The
110+
The CPython runtime itself *almost* already supports relative paths. The
104111
primitives are there, so the only change needed is to define how it resolves
105112
relative paths for ``home`` in ``pyvenv.cfg``.
106113

@@ -130,9 +137,19 @@ A proof-of-concept of this is implemented in
130137
Backwards Compatibility
131138
=======================
132139

133-
Because relative paths for ``home`` aren't usable in practice, and their
134-
behavior is undocumented and unspecified, there shouldn't be any backward
135-
compatibility concerns.
140+
141+
Tools that work around the absolute `home` key limitation the way `bazel` and `venvstacks`
142+
currently do (omitting the `home` key, or editing it after moving the environment) will be
143+
unaffected.
144+
145+
While the PEP author and sponsor aren't aware of any projects that work around the limitation
146+
by carefully controlling the current working directory used to launch the deployed Python
147+
environments on target systems, any such projects would be unaffected if they already ensured
148+
the working directory was set to the folder containing `pyvenv.cfg` (which seems like a plausible
149+
choice, since that is typically the root directory of the virtual environment). In the even more
150+
unlikely case where that assumption doesn't hold, tools generating relative virtual environment
151+
paths will typically be aware of the underlying base runtime Python version, and hence able to
152+
update the emitted relative path accordingly.
136153

137154

138155
How to Teach This
@@ -158,14 +175,15 @@ This PEP does not specify how to create a ``pyvenv.cfg`` with a relative path,
158175
nor how downstream tools (e.g. installers) should identify them or process
159176
them. These questions are best addressed separately by tool owners.
160177

161-
Footnotes
178+
References
162179
=========
163180

164181
* `rules_python <https://github.com/bazel-contrib/rules_python>`__: implements
165182
host-relocatable virtual environments.
166183
* `rules_py <https://github.com/aspect-build/rules_py>`__: implements
167184
host-relocatable virtual environments.
168185
* `python-build-standalone <https://github.com/astral-sh/python-build-standalone>`__:
186+
* `venvstacks <https://pypi.org/project/venvstacks/>`__: a tool for creating reproducible distribution artifacts from virtual environments
169187
A relocatable Python runtime.
170188
* `PoC for relative home in Python startup <https://github.com/python/cpython/compare/main...rickeylev:cpython:feat.relative.pyvenv.home>`__
171189
* `Python Ideas "Making venvs relocatable friendly" discussion <https://discuss.python.org/t/making-venvs-relocatable-friendly/96177>`__
@@ -189,7 +207,8 @@ dynamically compute it by checking if the current executable (which is
189207
typically the venv's ``bin/python3`` symlink) is a symlink and, if so, use
190208
where that points as ``PYTHONHOME``.
191209

192-
This behavior is undesirable for a couple reasons:
210+
While currently used as a workaround by some tools, *standardising* this behavior
211+
is undesirable for a couple reasons:
193212

194213
1. It presents platform-specific issues, namely with Windows. Windows does
195214
support symlinks, but not by default, and it can require special

0 commit comments

Comments
 (0)