Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'filename:fname piece:py': 0.07; 'modified': 0.07; 'subject:Getting': 0.07; 'imported': 0.09; 'subject:modules': 0.09; 'trailing': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '"/"': 0.16; "'__doc__',": 0.16; "'__file__',": 0.16; "'__path__',": 0.16; 'brackets.': 0.16; 'builtins': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'imported.': 0.16; 'programmatic': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; "hasn't": 0.19; 'packages.': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; "skip:' 10": 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'stuff': 0.32; 'subject:all': 0.32; 'skip:d 20': 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'shows': 0.36; 'charset:us-ascii': 0.36; 'searching': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'skip:[ 10': 0.38; 'skip:& 20': 0.39; 'enclosed': 0.60; 'skip:z 20': 0.60; 'identify': 0.61; 'skip:o 30': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; '30,': 0.65; 'within': 0.65; 'jul': 0.74; 'square': 0.74; '2014,': 0.84; 'contains.': 0.84; 'distinguish': 0.84; 'ideas.': 0.84; 'stuff:': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=M1q22BswpLx54jsm+DowWLlwDfWkZGg79dRyyJlyJnM=; b=N6pDjxryx1RR+i/M2+ff8JDMJILmnFFFO1X5DQ5Nv4elUyXP3cdx4HvdtNgCwHt+MU xzqeKztgncn6klGsnfkp3ZwgYisnRmLBMWrlRjXUTDyDHFwIMs8sz4NeGE3JBBpMWwos ELKpYGvh8q1ocsQHTJ/5f/SpufCyXjQ0Dac7RwE6apr2evKmnQ6MW9O4HPj7gv1cjK+N uonHnHc75JjeUsqS9QVVX5PGb5VrBMPXs94a4Q6u9MpsQvNh/0ZnbMtpHGThA1c9iM+C yJpZmo3fYi0WiX2w2VES9uGfD1gB4oeFPMlABQr3poFs0PM5YTL8/UF4nJRtl7vt8ybN gLiQ== MIME-Version: 1.0 X-Received: by 10.50.164.202 with SMTP id ys10mr8363689igb.6.1406731531300; Wed, 30 Jul 2014 07:45:31 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: <53d8a20e$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <53d8a20e$0$29977$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 30 Jul 2014 09:45:30 -0500 X-Google-Sender-Auth: o3NYbE3QTlYqv6MoaXL5Ti2DWVo Subject: Re: Getting a list of all modules From: Skip Montanaro To: "Steven D'Aprano" Content-Type: multipart/mixed; boundary=089e0149490637cfb204ff6a3501 Cc: Python 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: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406731534 news.xs4all.nl 2948 [2001:888:2000:d::a6]:59521 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75378 --089e0149490637cfb204ff6a3501 Content-Type: multipart/alternative; boundary=089e0149490637cfad04ff6a35ff --089e0149490637cfad04ff6a35ff Content-Type: text/plain; charset=UTF-8 On Wed, Jul 30, 2014 at 2:43 AM, Steven D'Aprano wrote: > I'm looking for a programmatic way to get a list of all Python modules > and packages. Not just those already imported, but all those which > *could* be imported. > I wrote a modified dir(), which I inject into builtins in interactive sessions. When getting a directory of an object, it attempts to identify not-yet-imported modules it contains. Such modules are enclosed in square brackets. Modules that smell like packages also get a trailing '/'. For example: % python Python 2.7.5+ (2.7:2921f6c2009e, Apr 30 2014, 14:00:13) [GCC 4.4.6 [TWW]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import dateutil >>> dir(dateutil) ['[easter]', '[parser]', '[relativedelta]', '[rrule]', '[tz]', '[tzwin]', '[zoneinfo/]', '__author__', '__builtins__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__version__'] It's not perfect, but works for my needs. Perhaps it will give you some ideas. Skip --089e0149490637cfad04ff6a35ff Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On Wed, Jul 30, 2014 at 2:43 AM, Steven D'Aprano <= steve@pearwood.inf= o> wrote:
I= 9;m looking for a programmatic way to get a list of all Python modules
and packages. Not just those already imported, but all those which
*could* be imported.

I wrote a modified dir(), = which I inject into builtins in interactive sessions. When getting a direct= ory of an object, it attempts to identify not-yet-imported modules it conta= ins. Such modules are enclosed in square brackets. Modules that smell like = packages also get a trailing '/'. For example:

% python
Python 2.7.5+ (2.= 7:2921f6c2009e, Apr 30 2014, 14:00:13)=C2=A0
[GCC 4.4.6 [TWW]] on linux2
Type "help", "copyright", &q= uot;credits" or "license" for more information.
>>> import dateutil=
>>> dir(dateutil)
['[easter]', '[parser]', '[relativedelta]= 9;, '[rrule]', '[tz]', '[tzwin]', '[zoneinfo/]&= #39;, '__author__', '__builtins__', '__doc__', '= ;__file__', '__license__', '__name__', '__package__= ', '__path__', '__version__']


It's not perfect, but works for my needs. Perhaps it will giv= e you some ideas.

Skip

--089e0149490637cfad04ff6a35ff-- --089e0149490637cfb204ff6a3501 Content-Type: text/x-python; charset=US-ASCII; name="dir.py" Content-Disposition: attachment; filename="dir.py" Content-Transfer-Encoding: base64 X-Attachment-Id: f_hy8rl4940 aW1wb3J0IG9zCl9kaXIgPSBkaXIKCmRlZiBkaXIobz1nbG9iYWxzKCksIGhpZGRlbj1GYWxzZSk6 CiAgICBpZiBub3QgaGlkZGVuIGFuZCBoYXNhdHRyKG8sICJfX2FsbF9fIik6CiAgICAgICAgY29u dGVudHMgPSBvLl9fYWxsX18KICAgIGVsc2U6CiAgICAgICAgY29udGVudHMgPSBfZGlyKG8pCiAg ICBpZiBoYXNhdHRyKG8sICJfX2ZpbGVfXyIpOgogICAgICAgIGRuYW1lID0gb3MucGF0aC5kaXJu YW1lKG8uX19maWxlX18pCiAgICAgICAgIyBsb29rIGZvciBub3QteWV0LWltcG9ydGVkIG1vZHVs ZXMgd2l0aGluIHBhY2thZ2VzCiAgICAgICAgaWYgIi9fX2luaXRfXy5weSIgaW4gby5fX2ZpbGVf XzoKICAgICAgICAgICAgdHJ5OgogICAgICAgICAgICAgICAgc3R1ZmYgPSBvcy5saXN0ZGlyKGRu YW1lKQogICAgICAgICAgICBleGNlcHQgT1NFcnJvcjoKICAgICAgICAgICAgICAgICMgU2VhcmNo aW5nIGVnZ3MgbGFuZHMgaGVyZS4gIEludHJvc3BlY3QuCiAgICAgICAgICAgICAgICBpbXBvcnQg emlwZmlsZQogICAgICAgICAgICAgICAgZCA9IG9zLnBhdGguZGlybmFtZShkbmFtZSkKICAgICAg ICAgICAgICAgIGlmIG5vdCB6aXBmaWxlLmlzX3ppcGZpbGUoZCk6CiAgICAgICAgICAgICAgICAg ICAgcmV0dXJuIHNvcnRlZChjb250ZW50cykKICAgICAgICAgICAgICAgIGJhc2UgPSBvcy5wYXRo LmJhc2VuYW1lKGRuYW1lKQogICAgICAgICAgICAgICAgc3R1ZmYgPSBbZltsZW4oYmFzZSkrMTpd CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yIGYgaW4gemlwZmlsZS5aaXBGaWxlKGQp Lm5hbWVsaXN0KCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgZi5zdGFydHN3 aXRoKGJhc2UpXQogICAgICAgICAgICBmb3IgcCBpbiBzdHVmZjoKICAgICAgICAgICAgICAgIG0g PSBvcy5wYXRoLnNwbGl0ZXh0KHApWzBdCiAgICAgICAgICAgICAgICBpZiAoCiAgICAgICAgICAg ICAgICAgICAgIyBub3QgYWxyZWFkeSBrbm93bgogICAgICAgICAgICAgICAgICAgIG0gbm90IGlu IGNvbnRlbnRzIGFuZAogICAgICAgICAgICAgICAgICAgICMgaXNuJ3QgYSBwYWNrYWdlIGZpbGUK ICAgICAgICAgICAgICAgICAgICBwICE9ICJfX2luaXRfXy5weSIgYW5kCiAgICAgICAgICAgICAg ICAgICAgIyBpcyBhIHB5dGhvbiBvciAuLi4KICAgICAgICAgICAgICAgICAgICAocC5lbmRzd2l0 aCgiLnB5Iikgb3IKICAgICAgICAgICAgICAgICAgICAgIyBjIG1vZHVsZSBvciAuLi4KICAgICAg ICAgICAgICAgICAgICAgcC5lbmRzd2l0aCgiLnNvIikgb3IKICAgICAgICAgICAgICAgICAgICAg IyBhIHN1YnBhY2thZ2UKICAgICAgICAgICAgICAgICAgICAgKG9zLnBhdGguaXNkaXIob3MucGF0 aC5qb2luKGRuYW1lLCBwKSkgYW5kCiAgICAgICAgICAgICAgICAgICAgICBvcy5wYXRoLmV4aXN0 cyhvcy5wYXRoLmpvaW4oZG5hbWUsIHAsICJfX2luaXRfXy5weSIpKSkpKToKICAgICAgICAgICAg ICAgICAgICBpZiBvcy5wYXRoLmlzZGlyKG9zLnBhdGguam9pbihkbmFtZSwgcCkpOgogICAgICAg ICAgICAgICAgICAgICAgICAjIHRhY2sgb24gdHJhaWxpbmcgLyB0byBkaXN0aW5ndWlzaCBwYWNr YWdlcyBmcm9tCiAgICAgICAgICAgICAgICAgICAgICAgICMgbW9kdWxlcwogICAgICAgICAgICAg ICAgICAgICAgICBtICs9ICIvIgogICAgICAgICAgICAgICAgICAgIGlmIG5vdCBtLnN0YXJ0c3dp dGgoIl8iKSBvciBoaWRkZW46CiAgICAgICAgICAgICAgICAgICAgICAgICMgWy4uLl0gc2hvd3Mg aXQgaGFzbid0IGJlZW4gaW1wb3J0ZWQgeWV0CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRl bnRzLmFwcGVuZCgiWyVzXSIgJSBtKQogICAgcmV0dXJuIHNvcnRlZChjb250ZW50cykK --089e0149490637cfb204ff6a3501--