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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; "'.'": 0.09; 'closest': 0.09; 'subject:modules': 0.09; 'dist': 0.16; 'modules,': 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; "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; 'email addr:gmail.com': 0.63; 'email name:python-list': 0.65; '2013': 0.98 X-TMN: [kDEdbPTdZXw/WyHWnQXajz5tdt3OAFFx] X-Originating-Email: [carlosnepomuceno@outlook.com] Content-Type: multipart/alternative; boundary="_39358e4d-a5e0-4c60-aff3-59660fa61b03_" From: Carlos Nepomuceno To: "python-list@python.org" Subject: RE: Listing modules from all installed packages Date: Sun, 9 Jun 2013 08:23:15 +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 05:23:15.0583 (UTC) FILETIME=[718558F0:01CE64D1] 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: 112 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370755403 news.xs4all.nl 15910 [2001:888:2000:d::a6]:50538 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47424 --_39358e4d-a5e0-4c60-aff3-59660fa61b03_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable print '\n'.join([re.findall("from '(.*)'"=2Cstr(v))[0] for k=2Cv in sys.mod= ules.items() if str(v).find('from')>-1]) > 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 = --_39358e4d-a5e0-4c60-aff3-59660fa61b03_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
print '\n'.join([re.findall("fro= m '(.*)'"=2Cstr(v))[0] for k=2Cv in sys.modules.items() if str(v).find('fro= m')>=3B-1])


>=3B Date: Sat=2C 8 Jun 2013 21:30:48 -0700=
>=3B Subject: Listing modules from all installed packages
>=3B F= rom: jphalip@gmail.com
>=3B To: python-list@python.org
>=3B
&= gt=3B Hi=2C
>=3B
>=3B I'm trying to write a function that progra= mmatically obtains and returns the exact location of all first-level module= s for all installed packages.
>=3B
>=3B For example=2C if the pa= ckages named 'django' and 'django-debug-toolbar' are installed=2C I'd like = this function to return something like:
>=3B >=3B>=3B>=3B instal= led_modules()
>=3B /Users/my_user/.virtualenvs/my_venv/lib/python2.6/s= ite-packages/django
>=3B /Users/my_user/.virtualenvs/my_venv/src/debug= _toolbar
>=3B
>=3B That is=2C this function needs to consider al= l installed packages=2C including those that have been installed in "edit" = mode (i.e. in the src/ folder). Note also that the main module for the 'dja= ngo-debug-toolbar' is in fact named 'debug_toolbar'.
>=3B
>=3B S= o far the closest I've been to retrieving the list of first-level modules i= s as follows:
>=3B
>=3B import os
>=3B import pkg_r= esources
>=3B import setuptools
>=3B
>=3B pkgs =3D = set()
>=3B
>=3B for dist in pkg_resources.working_set:
&g= t=3B if os.path.isdir(dist.location):
>=3B for pkg= in setuptools.find_packages(dist.location):
>=3B if '= .' not in pkg:
>=3B pkgs.add(pkg)
>=3B
&g= t=3B The idea is then to loop through that list of modules=2C import them a= nd 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 there must be a better way. If p= ossible I'd also like to avoid having to use setuptools.
>=3B
>= =3B Do you have any tips on how to achieve this?
>=3B
>=3B Many = thanks!
>=3B
>=3B Julien
>=3B --
>=3B http://mail.pyt= hon.org/mailman/listinfo/python-list
= --_39358e4d-a5e0-4c60-aff3-59660fa61b03_--