Finding scripts provided by a package...

3 views
Skip to first unread message

B Maqueira

unread,
Apr 10, 2012, 5:53:31 AM4/10/12
to cam...@googlegroups.com
Dear all,

Hopefully a simple & obvious question:

I have a namespace package 'a' with a code structure like:
a/
 b/
    b1.py
    b2.py...
  c/
     c1.py

And so on. This gets packaged as a.b.msi, a.c.msi etc, with each package providing also at least 1 script.

Given the name of the namespace package 'a', what is the correct way of finding all the installed scripts? I am trying to avoid doing any direct directory list read, etc. Is there a distribute/pkg_utils/pkg_resources/xx way of doing this?

Thanks in advance,

Brau

---------------------------
Dr B Maqueira
Senior Software Engineer
CSR plc
braudel.maqu...@csr.com bra...@ferrarihaines.com
http://codelab.ferrarihaines.com

David Guaraglia

unread,
Apr 12, 2012, 3:40:49 PM4/12/12
to cam...@googlegroups.com
The simplest way I can think of is using the 'inspect' module (comes in the standard Python library since version 2.1, here's the doc: http://docs.python.org/library/inspect.html).

For example, to list all modules in the Django namespace you could use code like this:

import inspect
import django

inspect.getmembers(django, inspect.ismodule)

This will return this list of tuples:

[('conf',
  <module 'django.conf' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/conf/__init__.pyc'>),
 ('contrib',
  <module 'django.contrib' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/contrib/__init__.pyc'>),
 ('core',
  <module 'django.core' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/core/__init__.pyc'>),
 ('db',
  <module 'django.db' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/db/__init__.pyc'>),
 ('dispatch',
  <module 'django.dispatch' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/dispatch/__init__.pyc'>),
 ('forms',
  <module 'django.forms' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/forms/__init__.pyc'>),
 ('http',
  <module 'django.http' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/http/__init__.pyc'>),
 ('middleware',
  <module 'django.middleware' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/middleware/__init__.pyc'>),
 ('shortcuts',
  <module 'django.shortcuts' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/shortcuts/__init__.pyc'>),
 ('template',
  <module 'django.template' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/template/__init__.pyc'>),
 ('test',
  <module 'django.test' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/test/__init__.pyc'>),
 ('utils',
  <module 'django.utils' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/utils/__init__.pyc'>),
 ('views',
  <module 'django.views' from '/Users/david/.virtualenvs/backend/lib/python2.7/site-packages/django/views/__init__.pyc'>)]

Please notices these are tuples where the first element is the name of the module and the second element is the module itself. Simply by recursing on the second element of the tuple you can generate a full list of modules for the namespace (left as an exercise to the reader :)).

Cheers,
David



--
You received this message because you are subscribed to the Google Groups "Cambridge and East Anglian Python Users Group" group.
To post to this group, send email to cam...@googlegroups.com.
To unsubscribe from this group, send email to campug+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/campug?hl=en.

Reply all
Reply to author
Forward
0 new messages