Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47421

Listing modules from all installed packages

X-Received by 10.224.36.66 with SMTP id s2mr5195035qad.6.1370752248489; Sat, 08 Jun 2013 21:30:48 -0700 (PDT)
X-Received by 10.50.36.99 with SMTP id p3mr330422igj.4.1370752248343; Sat, 08 Jun 2013 21:30:48 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!p1no3792862qaj.0!news-out.google.com!y6ni1323qax.0!nntp.google.com!ch1no3248186qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sat, 8 Jun 2013 21:30:48 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=67.169.55.173; posting-account=lxV8QgoAAAAe2fBoBWwbYnOGFpspJi5o
NNTP-Posting-Host 67.169.55.173
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <210f2f63-13b0-46c2-b080-fd831cb3ca49@googlegroups.com> (permalink)
Subject Listing modules from all installed packages
From Julien Phalip <jphalip@gmail.com>
Injection-Date Sun, 09 Jun 2013 04:30:48 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:47421

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Listing modules from all installed packages Julien Phalip <jphalip@gmail.com> - 2013-06-08 21:30 -0700
  RE: Listing modules from all installed packages Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-09 08:23 +0300
    Re: Listing modules from all installed packages 88888 Dihedral <dihedral88888@gmail.com> - 2013-06-09 11:57 -0700
  RE: Listing modules from all installed packages Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-09 09:22 +0300
    Re: Listing modules from all installed packages cclauss@bluewin.ch - 2013-06-09 05:51 -0700
    Re: Listing modules from all installed packages Julien Phalip <jphalip@gmail.com> - 2013-06-14 12:18 -0700

csiph-web