Repository navigation
Closed as not planned
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
It would be useful to expose the runtime library path, if any, as a sys module attribute.
This is already implemented for getpath, we can easily make this information also available in sys.
Lines 805 to 829 in 10ee2d9
| /* Add the runtime library's path to the dict */ | |
| static int | |
| library_to_dict(PyObject *dict, const char *key) | |
| { | |
| /* macOS framework builds do not link against a libpython dynamic library, but | |
| instead link against a macOS Framework. */ | |
| #if defined(Py_ENABLE_SHARED) || defined(WITH_NEXT_FRAMEWORK) | |
| #ifdef MS_WINDOWS | |
| extern HMODULE PyWin_DLLhModule; | |
| if (PyWin_DLLhModule) { | |
| return winmodule_to_dict(dict, key, PyWin_DLLhModule); | |
| } | |
| #endif | |
| #if HAVE_DLADDR | |
| Dl_info libpython_info; | |
| if (dladdr(&Py_Initialize, &libpython_info) && libpython_info.dli_fname) { | |
| return decode_to_dict(dict, key, libpython_info.dli_fname); | |
| } | |
| #endif | |
| #endif | |
| return PyDict_SetItemString(dict, key, Py_None) == 0; | |
| } |
I'm proposing sys, as I think that's what makes the most logical sense, but people are not too keen on it, we could add it to sysconfig instead.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Reacted by Kirill Podoprigora, Stan Ulbrych, Dawid Konrad Kohnke and zwhfly
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Activity
vstinner commentedon Jan 31, 2025
What is the runtime library path?
FFY00 commentedon Jan 31, 2025
It's
libpython. If it's dynamically linked,sys.runtime_librarywill contain its path, if it's statically linked, it will beNone.ncoghlan commentedon Jan 31, 2025
When Python is built as a shared library, the path to that library.
It's the value provided for the
libraryvariable when running https://github.com/python/cpython/blob/main/Modules/getpath.pyIt's very much a CPython implementation detail though, since even CPython doesn't define it when statically linked. (The same is also true of
sys.dllhandle, but that ship has already sailed).Checking what
setuptoolsdoes, the current incantation to get that info would be:zooba commentedon Jan 31, 2025
What is it useful for? And why is calculating it from
sysconfig(mixed withsys.prefixif you want relocatability) not sufficient?In getpath, we use it because Windows historically would resolve the stdlib against the DLL, and later we resolve
._pthfiles from it,1 but that's very much internal implementation.sys.dllhandleprobably shouldn't have been added, asGetModuleHandle(dll_name)is a perfectly fine way to get the same information. It's done now, so we can't take it away, but we don't have to continue making the same mistake.Footnotes
For security reasons. It allows to control attackers that manage to LoadLibrary the DLL, or to simplify controlling your host app when you have multiple executables. ↩
gpshead commentedon Jan 31, 2025
What are the use cases for exposing this more widely?
FFY00 commentedon Dec 10, 2025
I think for now we can get away with just exposing it, or the whole getpath dictionary, as a private sysconfig attribute. We can revisit exposing it publicly if a strong enough need presents itself.
zooba commentedon Dec 10, 2025
This is the particular line we're asking you to expand on. Yes, it's very simple to do it, and there are ways to get away with it, but the only rationale you've given is "it would be useful" - we're asking why it would be useful.
FFY00 commentedon Dec 10, 2025
To be completely honest, I don't recall what exactly triggered me to open this issue, but I have on a number of occasions wanted access to this value. So, I won't fight on this. I think exposing it as a private sysconfig attribute, at least, is worthwhile. We could revisit the original proposal later, if we find a more concrete demonstrable need.
FFY00 commentedon Mar 3, 2026
Closing as not planned for now.