Module name does not match file name

Robert Kern robert.kern at gmail.com
Mon Jul 7 13:15:51 CEST 2014


On 2014-07-07 09:57, Steven D'Aprano wrote:
> Ah, I think I have a partial answer... but not a complete answer.
>
>
> On Mon, 07 Jul 2014 07:57:21 +0000, Steven D'Aprano wrote:
>
>> Can anyone explain how "import pg" can end up coming from pgmodule.so?
>>
>>
>> Sure enough:
>>
>>>>> import pg
>>>>> pg.__file__
>> '/usr/local/lib/python2.6/dist-packages/pgmodule.so'
>
>
>
> I've looked inside the pgmodule.c source code, and it includes this
> snippet:
>
>
> extern void
> initpg(void)
> {
>      char *p;
>      int i;
>
>      Py_InitModule("pg", pg_methods);
>
>
> which suggests that the pgmodule.so file creates a module called "pg".
> What I don't understand is how "import pg" gets turned into "run
> pgmodule.so"?

This has been standard Python behavior for extension modules since forever. It's 
a very old practice and not recommended for new code, though.

[~]
|1> import imp

[~]
|2> imp.get_suffixes()
[('.so', 'rb', 3), ('module.so', 'rb', 3), ('.py', 'U', 1), ('.pyc', 'rb', 2)]

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list