Listing modules from all installed packages
88888 Dihedral
dihedral88888 at gmail.com
Sun Jun 9 14:57:41 EDT 2013
Carlos Nepomuceno於 2013年6月9日星期日UTC+8下午1時23分15秒寫道:
> print '\n'.join([re.findall("from '(.*)'",str(v))[0] for k,v in sys.modules.items() if str(v).find('from')>-1])
>
>
>
> > Date: Sat, 8 Jun 2013 21:30:48 -0700
> > Subject: Listing modules from all installed packages
> > From: jph... at gmail.com
> > To: pytho... at python.org
> >
> > Hi,
> >
> > I'm trying to write a function that programmatically obtains and returns the exact location of all first-level modules for all installed packages.
> >
> > For example, if the packages named 'django' and 'django-debug-toolbar' are installed, I'd like this function to return something like:
> > >>> installed_modules()
> > /Users/my_user/.virtualenvs/my_venv/lib/python2.6/site-packages/django
> > /Users/my_user/.virtualenvs/my_venv/src/debug_toolbar
> >
> > That is, this function needs to consider all installed packages, including those that have been installed in "edit" mode (i.e. in the src/ folder). Note also that the main module for the 'django-debug-toolbar' is in fact named 'debug_toolbar'.
> >
> > So far the closest I've been to retrieving the list of first-level modules is as follows:
> >
> > import os
> > import pkg_resources
> > import setuptools
> >
> > pkgs = set()
> >
> > for dist in pkg_resources.working_set:
> > if os.path.isdir(dist.location):
> > for pkg in setuptools.find_packages(dist.location):
> > if '.' not in pkg:
> > pkgs.add(pkg)
> >
> > The idea is then to loop through that list of modules, import them and get their exact locations by fetching their __file__ attribute values.
> >
> > However, this feels very hackish and I don't think it's actually quite correct either. I'm sure there must be a better way. If possible I'd also like to avoid having to use setuptools.
> >
> > Do you have any tips on how to achieve this?
> >
> > Many thanks!
> >
> > Julien
> > --
> > http://mail.python.org/mailman/listinfo/python-list
Please use a dictionary to store a tree first.
Then it is trivial to walk through all nodes of the tree.
More information about the Python-list
mailing list