[Python-Dev] how to find the file path to an extension module at init time?
stefan_ml at behnel.de
Mon Nov 14 09:18:33 CET 2011
"Martin v. Löwis", 13.11.2011 21:46:
>> I'm asking specifically because I'd like to properly implement __file__
>> in Cython modules at module init time.
> Why do you need to implement __file__? Python will set it eventually to
> its correct value, no?
Well, yes, eventually. However, almost all real world usages are at module
init time, not afterwards. So having CPython set it after running through
the module global code doesn't help much.
>> Another problem is that package local imports from __init__.py no longer
>> work when it's compiled
> Does it actually work to have __init__ be an extension module?
I'm just starting to try it, and the problems I found so far were __file__
(in general), __path__ and relative imports (specifically).
>> Any ideas how this could currently be achieved?
> Currently, for Cython? I don't think that can work.
Hmm, it might work to put an empty module next to the 'real' extension and
to import it to figure out the common directory of both. As long as it's
still there after installation and the right one gets imported, that is. A
relative import should help on versions that support it. Although that
won't help in the __init__ case because a relative import will likely
depend on __path__ being available first. Chicken and egg...
Support in CPython would definitely help.
>> Or could this become a new feature in the future?
> Certainly. An approach similar to _Py_PackageContext should be possible.
Yes, and a "_Py_ModuleImportContext" would be rather trivial to do. Could
that go into 3.3? What about 2.7? Could an exception be made there
regarding new "features"? It's not likely to break anything, but it would
More information about the Python-Dev