inspect.getmodulename giving unexpected results

Thomas Jollans thomas at jollybox.de
Fri Sep 24 17:29:43 CEST 2010


On Friday 24 September 2010, it occurred to Geoff Bache to exclaim:
> > > Unfortunately, it doesn't seem to do that in some cases. Consider the
> > 
> > > following code:
> > It does behave as documented: it does not find package names, or
> > investigate sys.modules
> 
> Possibly, although for me "logging" is exposed as a module, and ought
> to behave as one when requesting the module name. The fact that it's a
> package internally doesn't seem relevant to me in this context. It's
> weird to me that when I do
> 
> import os, logging
> 
> treating these things as normal modules I can get the right name for
> "os" but get "__init__" when requesting "logging". Externally, these
> things appear to be the same. I can accept from the docs that if I did
> "getmodulename" on "logging.config" I would get "config" back.
> 
> > > ## file 'inspect_test'
> > 
> > rename this file to "inspect_test.py", and the filename will make sense
> > to inspect.getmodulename, which should *then* return "inspect_test"
> > instead of None.
> 
> I realise that, but of course I can't do that in the real code. It's a
> long-established convention to drop the ".py" from executable programs
> on UNIX.

Then again, it's a classic trick to put your Python code in ../site-packages/ 
(or the like) and just put a tiny stub script in bin/ for projects of the same 
kind of dimensions where anybody would care about the executable having a file 
extension or not ;-)



More information about the Python-list mailing list