Minor gripe about module names

Tim Chase python.list at tim.thechases.com
Sat May 12 07:45:15 EDT 2012


On 05/12/12 05:51, Chris Angelico wrote:
> On Sat, May 12, 2012 at 8:41 PM, John O'Hagan
> <research at johnohagan.com> wrote:
>> Not sure if this is only package-manager specific, but
>> occasionally I come across a module that sounds interesting,
>> install it (in my case by apt-get), and then can't find it,
>> because the actual module has a different name from what it
>> says on the package - unlike the majority, which if they are
>> called "python-whatever", are imported using "import
>> whatever".
> 
> $ dpkg -L python-ecasound
> 
> This works only after the package has been installed
> successfully. Not a solution to your problem, but possibly a
> viable workaround.

I use the dpkg method on my Debian-based systems, but (1) I never
remember the syntax and only occasionally need it, so I have to look
it up EVERY time and (2) it can return a lot of chaff if the module
includes auxiliary files.

If only we had some way to develop a simple program that knew about
where Python modules were stored... ;-)

=====================================
import os
import sys

if len(sys.argv) < 2:
  print("Usage:")
  print("%s module_text [or_module_text ...]" % argv[0])
  sys.exit(1)

mods_to_find = [s.lower() for s in sys.argv[1:]]

for d in sys.path:
  if os.path.isdir(d):
    for modname in os.listdir(d):
      for piece in mods_to_find:
        if piece in modname.lower():
          print(os.path.join(d, modname))
  else:
    sys.stderr.write("Unable to check %r\n" % d)
======================================

should about do the trick and return less chaff.  Could be tweaked
to refine even further.  Or to deduplicate results if the same file
is found due to search criteria.

-tkc






More information about the Python-list mailing list