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.

Author Gabriele Tornetta
Recipients Gabriele Tornetta
Date 2021-10-14.11:51:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634212277.88.0.777172112966.issue45468@roundup.psfhosted.org>
In-reply-to
Content
I would like to propose adding support for preloading a Python script before executing a Python application.


Potential design:

A new environment variable (e.g. PY_PRELOAD, an allusion to LD_PRELOAD, or PYTHONPRELOAD to be more aligned with the naming pattern of existing variables) is used to specify the path of the script to execute. The script is then executed just before the import of the site module. This is similar to what PYTHONSTARTUP does, except that it works in all cases, not just when launching the REPL.


Rationale:

There are examples of tools that require performing some operations (e.g. monkey-patching) before starting the actual Python application. Currently, this could be achieved in the following rather contrived way:

- prepare an ad-hoc sitecustomize.py file
- add its path to PYTHONPATH
- run the Python application with the computed PYTHONPATH

The ad-hoc sitecustomize.py requires the following steps

- run the custom initialisation code
- remove the path of the custom sitecustomize.py script from sys.path
- unload the sitecustomize module
- try importing sitecustomize to force-load any original sitecustomize scripts that would have run otherwise

The new environment variable makes the whole process a lot easier by allowing tools to just set a single environment variable to achieve the same result.
History
Date User Action Args
2021-10-14 11:51:17Gabriele Tornettasetrecipients: + Gabriele Tornetta
2021-10-14 11:51:17Gabriele Tornettasetmessageid: <1634212277.88.0.777172112966.issue45468@roundup.psfhosted.org>
2021-10-14 11:51:17Gabriele Tornettalinkissue45468 messages
2021-10-14 11:51:17Gabriele Tornettacreate