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: import searchpaths as arguments
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: c2621566, martin.panter, ncoghlan, r.david.murray
Priority: normal Keywords: patch

Created on 2014-12-30 19:10 by c2621566, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
main.c.searchpatharg1.diff c2621566, 2014-12-30 19:10 feature `import searchpaths as arguments` as patch to /Module/main.c
Messages (4)
msg233216 - (view) Author: (c2621566) Date: 2014-12-30 19:10
This patch allows specifying import searchpaths as `-p path` arguments to the interpreter, without touching environment variables.

Avoiding environment variables simplifies a script of mine and is a portable way of swapping module implementation without touching the importing script.

e.g.
# python -p ~/.bin/customlib -p ~/.bin/other script.py
is equivalent to
# PYTHONPATH=~/.bin/customlib:~/.bin/other:$PYTHONPATH python script.py
similarly to
# ghci -i.bin/customlib:.bin/other foo.hs

It is implemented by prepending the arguments to sys.path in Py_Main just after Py_Initialize is called.
msg233218 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-12-30 20:20
Thank you for the suggestion and patch, but we have a general policy of not adding unnecessary command line options, so I don't know that it will be accepted.
msg233262 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-12-31 18:11
Issue 13475 provides some additional context for discussion of changes to the sys.path initialisation process. The status quo is that I'm currently wary of adding more complexity to an already fragile initialisation sequence for things that can be implemented by way of running a custom launch script that uses the runpy module to replicate the CPython script invocation options.

In the specific case of this patch, while the idea is one we've considered before (and may end up implementing some day), this specific implementation adds new path initilisation logic directly in Py_Main, well away from getpath.c (which, in collaboration with site.py, is intended to fully handle the path initialisation logic), and without being separated out into a distinct API that embedding applications can easily take advantage of. A lot of Python code will also run *before* the command line arguments are processed, so you'll end up with a case where code run early may pick up a version of a module from later in sys.path, giving the appearance that the sys.path additions are being ignored, even though they appear in sys.path after the program is fully initialised.

While the technical concerns mean it isn't likely this particular approach would be pursued further, as a practical matter, additional things that would be needed for a patch to the interpreter argument handling to be accepted include:
- a signed contributor licensing agreement
- documentation upates in the "using" guide
- a new test (or tests) in Lib/test/test_cmdline.py and Lib/test/test_cmdline_script.py
msg233269 - (view) Author: (c2621566) Date: 2014-12-31 19:03
Thanks for the feedback. The technical arguments convinced me not to spend more time on this. I admit this patch is kind of an ugly hack. The effort needed for a technically satisfactory solution seems too much for this unimportant feature.
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67324
2014-12-31 19:03:46c2621566setstatus: open -> closed
resolution: rejected
messages: + msg233269
2014-12-31 18:11:14ncoghlansetmessages: + msg233262
2014-12-31 13:28:56pitrousetnosy: + ncoghlan
2014-12-31 10:04:22martin.pantersetnosy: + martin.panter
2014-12-30 20:20:44r.david.murraysetnosy: + r.david.murray
messages: + msg233218
2014-12-30 19:10:31c2621566create