Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.04; 'attribute': 0.07; "'.'": 0.09; 'closest': 0.09; 'subject:modules': 0.09; 'dist': 0.16; 'modules,': 0.16; 'received:65.55.116.7': 0.16; 'retrieving': 0.16; 'set()': 0.16; 'setuptools': 0.16; 'setuptools.': 0.16; 'skip:/ 70': 0.16; 'sat,': 0.16; 'module': 0.19; 'trying': 0.19; 'packages.': 0.19; '>>>': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'this?': 0.23; '>>>': 0.24; 'either.': 0.24; 'received:65.55.116': 0.24; "i've": 0.25; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'installed': 0.27; 'idea': 0.28; 'function': 0.29; 'correct': 0.29; 'locations': 0.30; 'mode': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'asked': 0.31; "skip:' 10": 0.31; 'values.': 0.31; 'subject:all': 0.32; 'thanks!': 0.32; 'quite': 0.32; 'url:python': 0.33; '(i.e.': 0.33; 'date:': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'something': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'possible': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'email addr:python.org': 0.37; 'skip:o 20': 0.38; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'subject:': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'tips': 0.61; "you've": 0.63; 'email addr:gmail.com': 0.63; 'email name:python-list': 0.65; 'realized': 0.68; 'trick.': 0.84; '2013': 0.98 X-TMN: [heEVGz5SVj4WjiIfoLehNX2OVwdEt74e] X-Originating-Email: [carlosnepomuceno@outlook.com] Content-Type: multipart/alternative; boundary="_efcc3f0d-6d70-47e3-b4a3-4a07ebee6ed8_" From: Carlos Nepomuceno To: "python-list@python.org" Subject: RE: Listing modules from all installed packages Date: Sun, 9 Jun 2013 09:22:16 +0300 Importance: Normal In-Reply-To: <210f2f63-13b0-46c2-b080-fd831cb3ca49@googlegroups.com> References: <210f2f63-13b0-46c2-b080-fd831cb3ca49@googlegroups.com> MIME-Version: 1.0 X-OriginalArrivalTime: 09 Jun 2013 06:22:16.0274 (UTC) FILETIME=[AFEFEB20:01CE64D9] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 119 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370758945 news.xs4all.nl 15970 [2001:888:2000:d::a6]:37365 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47426 --_efcc3f0d-6d70-47e3-b4a3-4a07ebee6ed8_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Just realized that you've asked for installed packages. Perhaps the followi= ng will do the trick. I don't know why the 'lib-tk' isn't included. Why not= ? toplevel_packages =3D ['%s\\%s'%(ml.path=2Cname)for ml=2Cname=2Cispkg in pk= gutil.iter_modules() if ispkg] print '\n'.join(toplevel_packages) > Date: Sat=2C 8 Jun 2013 21:30:48 -0700 > Subject: Listing modules from all installed packages > From: jphalip@gmail.com > To: python-list@python.org >=20 > Hi=2C >=20 > I'm trying to write a function that programmatically obtains and returns = the exact location of all first-level modules for all installed packages. >=20 > For example=2C if the packages named 'django' and 'django-debug-toolbar' = are installed=2C 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 >=20 > That is=2C this function needs to consider all installed packages=2C incl= uding those that have been installed in "edit" mode (i.e. in the src/ folde= r). Note also that the main module for the 'django-debug-toolbar' is in fac= t named 'debug_toolbar'. >=20 > So far the closest I've been to retrieving the list of first-level module= s is as follows: >=20 > import os > import pkg_resources > import setuptools >=20 > pkgs =3D set() >=20 > 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) >=20 > The idea is then to loop through that list of modules=2C import them and = get their exact locations by fetching their __file__ attribute values. >=20 > However=2C 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 l= ike to avoid having to use setuptools. >=20 > Do you have any tips on how to achieve this? >=20 > Many thanks! >=20 > Julien > --=20 > http://mail.python.org/mailman/listinfo/python-list = --_efcc3f0d-6d70-47e3-b4a3-4a07ebee6ed8_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Just realized that you've asked = for installed packages. Perhaps the following will do the trick. I don't kn= ow why the 'lib-tk' isn't included. Why not?

toplevel_packages =3D [= '%s\\%s'%(ml.path=2Cname)for ml=2Cname=2Cispkg in pkgutil.iter_modules() if= ispkg]
print '\n'.join(toplevel_packages)

>=3B Date: Sat= =2C 8 Jun 2013 21:30:48 -0700
>=3B Subject: Listing modules from all i= nstalled packages
>=3B From: jphalip@gmail.com
>=3B To: python-li= st@python.org
>=3B
>=3B Hi=2C
>=3B
>=3B I'm trying to= write a function that programmatically obtains and returns the exact locat= ion of all first-level modules for all installed packages.
>=3B
&g= t=3B For example=2C if the packages named 'django' and 'django-debug-toolba= r' are installed=2C I'd like this function to return something like:
>= =3B >=3B>=3B>=3B installed_modules()
>=3B /Users/my_user/.virtua= lenvs/my_venv/lib/python2.6/site-packages/django
>=3B /Users/my_user/.= virtualenvs/my_venv/src/debug_toolbar
>=3B
>=3B That is=2C this = function needs to consider all installed packages=2C including those that h= ave 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_too= lbar'.
>=3B
>=3B So far the closest I've been to retrieving the = list of first-level modules is as follows:
>=3B
>=3B import = os
>=3B import pkg_resources
>=3B import setuptools
&g= t=3B
>=3B pkgs =3D set()
>=3B
>=3B for dist in pkg= _resources.working_set:
>=3B if os.path.isdir(dist.location):<= br>>=3B for pkg in setuptools.find_packages(dist.location):>=3B if '.' not in pkg:
>=3B p= kgs.add(pkg)
>=3B
>=3B The idea is then to loop through that lis= t of modules=2C import them and get their exact locations by fetching their= __file__ attribute values.
>=3B
>=3B However=2C this feels very= hackish and I don't think it's actually quite correct either. I'm sure the= re must be a better way. If possible I'd also like to avoid having to use s= etuptools.
>=3B
>=3B Do you have any tips on how to achieve this= ?
>=3B
>=3B Many thanks!
>=3B
>=3B Julien
>=3B -= -
>=3B http://mail.python.org/mailman/listinfo/python-list
=
= --_efcc3f0d-6d70-47e3-b4a3-4a07ebee6ed8_--