[Python-Dev] how to find the file path to an extension module at init time?
Stefan Behnel
stefan_ml at behnel.de
Tue Nov 15 09:01:58 CET 2011
"Martin v. Löwis", 15.11.2011 01:33:
>>> 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...
>
> If there was an actual __init__.py that just had
>
> import __cinit__
or rather
from .__cinit__ import *
(relative import only for CPythons that support it)
> then __cinit__ could copy __path__ from the already-loaded __init__, no?
Hmm, right - that should work. __init__ would be in sys.modules with a
properly set __file__ and __path__, and __cinit__ (knowing its own package
name anyway) could look it up there to find out its file system path.
I don't think there's much code out there that actually uses __file__ to
find out the name of the module rather than just its package directory. So
it would (more or less) fix both problems for __init__ files, and it should
work with Py2.4+.
>>> 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?
>
> If somebody contributes a patch: sure.
Ok, cool.
>> What about 2.7?
>
> Certainly not. It would be a new feature, and there can't be new
> features in 2.7.
>
>> Could an exception be made there regarding new "features"?
>> It's not likely to break anything, but it would help Cython.
>
> "Not likely to break anything" really means it practice
> "it probably will break somebody's code". Policies are
> there to be followed, and this one I personally feel strongly
> about. If it means that users can get certain Cython features
> only with Python 3.x, the better.
Understandable.
Stefan
More information about the Python-Dev
mailing list