@@ -16,48 +16,41 @@ Post-History: `DD-MM-YYYY <https://url-to-pep-discussion>`__
1616Abstract
1717========
1818
19- This PEP describes how a Python virtual environment can be configured
20- to allow copying it as-is to different hosts and still have a functioning
21- virtual environment. Making this work consists of two main pieces:
22-
23- * Using a relative path for ``home `` in ``pyvenv.cfg ``, which Python canonicalizes
24- at runtime.
25- * Having package installers recognize a relative venv and generate
26- appropriate outputs, e.g. script wrappers.
27-
19+ This PEP describes how a relative path for ``home `` in a Python virtual
20+ environment's ``pyvenv.cfg `` is understood by the Python startup process.
21+ Specifically, how is it canonicalized into an absolute path later used
22+ by the runtime. This small detail is a fundamental building block for
23+ virtual environments to be more portable.
2824
2925Motivation
3026==========
3127
32- There are two main motivations for allowing relative paths in ``pyvenv.cfg ``
33- and thus enabling host-relocatable virtual environments.
28+ There are two main motivations for allowing relative paths in ``pyvenv.cfg ``.
3429
3530First, it is currently proscribed that the ``home `` value in ``pyvenv.cfg `` be
3631an absolute path. The behavior of relative paths is unspecified. While
3732techniques exist to work around this for every other sub-part of a virtual
3833environment, the one remaining part without a teneable solution is how the
3934Python runtime itself finds ``PYTHONHOME ``. This is because, currently, the
4035startup process requires absolute paths be used for the ``home `` key in
41- ``pyvenv.cfg ``. If a relative path is used, behavior is unspecified (currently,
42- it is relative to the process's current working directory, making it untenable
43- to use).
36+ ``pyvenv.cfg ``. If a relative path is used, behavior is unspecified (the
37+ current implementation ends up making it relative to the process's current
38+ working directory, making it untenable to use).
4439
4540This requirement is overly proscriptive and restrictive because, given a known
4641anchor point, it's easy to tranform a relative path to an absolute path and
4742still retain predictable and reliable behavior. Thus, the absolute path
4843requirement should be relaxed and relative path behavior allowed and defined.
4944
50- Second, virtual environments are the standard way for running Python
51- applications. This means ...
45+ Second, such relative paths are a building block to enable portable virtual
46+ environments, i.e. copying a virtual environment as-is between hosts of
47+ compatible platforms. Portable venvs are appealing because virtual environments
48+ are the standard way for running Python applications. This means...
5249
5350* The closer the dev environment is to the non-dev environment, the more
54- reliable software one can get.
55- * The closer the dev and non-dev envs are, the easier it is to reproduce
56- issues.
51+ reliable software one can get, and the easier it is to reproduce issues.
5752* The simpler the process is to re-create the environment, the more reliable
58- software one can get.
59- * The simpler the process is to re-create the environment, the faster the
60- process can be.
53+ software one can get, and the faster the process can be.
6154
6255By making it trivial to copy a virtual environment from one host to another, it
6356avoids these categories of problems. Additionally, the development tools to
@@ -127,43 +120,15 @@ similar, which is a lower cognitive burden and helps avoids the question of
127120This change is only a couple of lines in the start up code. Specifically, when
128121parsing the ``pyvenv.cfg `` file and finding the ``home `` value, it just needs
129122to be checked if it's already absolute. If not, then join to the directory name
130- of the ``pyvenv.cfg `` file. The code alredy knows the directory and has helpers
131- already present for checking if a path is absolute and joining two paths.
123+ of the ``pyvenv.cfg `` file. The code already knows the directory and has
124+ helpers already present for checking if a path is absolute and joining two
125+ paths.
132126
133127A proof-of-concept of this is implemented in
134128`rickeylev/feat.relative.pyvenv.home <https://github.com/python/cpython/compare/main...rickeylev:cpython:feat.relative.pyvenv.home >`__
135129
136- Virtual Environment Tool Changes
137- ================================
138-
139- Virtual environment management tools should support a mechanism to generate a
140- ``pyvenv.cfg `` file with a ``home `` key that is a relative path to the relevant
141- Python installation.
142-
143- For the standard library's ``venv `` module, the flag ``--relative `` will be
144- added to trigger this behavior.
145-
146- Package Installer Changes
147- =========================
148-
149- Package installers must be aware of when a relative virtual environment has
150- been defined so they can avoid using absolute paths. The two main places
151- absolute paths occur today are:
152-
153- 1. ``bin/ `` scripts, where shebangs are rewritten.
154- 2. ``site-packages `` symlinks.
155-
156- For symlinks, they should create relative symlinks pointing to the actual
157- installation directory for packages.
158-
159- For ``bin/ `` scripts, installers should generate executables that are able to
160- locate the venv's interpreter at runtime. How to do this is implementation
161- defined. Typically it means having shell code use ``$0 `` to locate the
162- appropriate ``bin/python3 `` executable, but installers are free to use whatever
163- mechanism they want (e.g. a native polyglot executable).
164-
165130Copying a Venv to Another Host
166- =================================
131+ ==============================
167132
168133Copying the venv to another host is simple: copy the venv directory itself, and
169134any directories outside the venv it needs, while preserving the relative
@@ -202,7 +167,9 @@ A reference implementation is available by using the combination of:
202167Open Issues
203168===========
204169
205- (todo: placeholder section to be updated after discussions)
170+ This PEP does not specify how to create a ``pyvenv.cfg `` with a relative path,
171+ nor how downstream tools (e.g. installers) should identify them or process
172+ them. These questions are best addressed separately by tool owners.
206173
207174Footnotes
208175=========
@@ -211,10 +178,6 @@ Footnotes
211178 host-relocatable virtual environments.
212179* `rules_py <https://github.com/aspect-build/rules_py >`__: implements
213180 host-relocatable virtual environments.
214- * `uv venv
215- relocatable
216- <https://docs.astral.sh/uv/reference/cli/#uv-venv--relocatable> `__:
217- implements same-host relocatable virtual environments.
218181* `python-build-standalone <https://github.com/astral-sh/python-build-standalone >`__:
219182 A relocatable Python runtime.
220183* `PoC for relative home in Python startup <https://github.com/python/cpython/compare/main...rickeylev:cpython:feat.relative.pyvenv.home >`__
@@ -227,9 +190,9 @@ Rejected Ideas
227190Relative to virtual env root
228191----------------------------
229192
230- Having the ``home `` value in ``pyvenv.cfg `` relative to the virtual environments
231- root directory would work just as well, but this idea is rejected because it
232- requires additional effort to compute the virtual env root.
193+ Having the ``home `` value in ``pyvenv.cfg `` relative to the virtual
194+ environments root directory would work just as well, but this idea is rejected
195+ because it requires additional effort to compute the virtual env root.
233196
234197Unspecified home means to dynamically compute home
235198----------------------------------------------------
0 commit comments