<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Just realized that you've asked for installed packages. Perhaps the following will do the trick. I don't know why the 'lib-tk' isn't included. Why not?<br><br>toplevel_packages = ['%s\\%s'%(ml.path,name)for ml,name,ispkg in pkgutil.iter_modules() if ispkg]<br>print '\n'.join(toplevel_packages)<br><br><div>> Date: Sat, 8 Jun 2013 21:30:48 -0700<br>> Subject: Listing modules from all installed packages<br>> From: jphalip@gmail.com<br>> To: python-list@python.org<br>> <br>> Hi,<br>> <br>> I'm trying to write a function that programmatically obtains and returns the exact location of all first-level modules for all installed packages.<br>> <br>> For example, if the packages named 'django' and 'django-debug-toolbar' are installed, I'd like this function to return something like:<br>> >>> installed_modules()<br>> /Users/my_user/.virtualenvs/my_venv/lib/python2.6/site-packages/django<br>> /Users/my_user/.virtualenvs/my_venv/src/debug_toolbar<br>> <br>> 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'.<br>> <br>> So far the closest I've been to retrieving the list of first-level modules is as follows:<br>> <br>>     import os<br>>     import pkg_resources<br>>     import setuptools<br>> <br>>     pkgs = set()<br>> <br>>     for dist in pkg_resources.working_set:<br>>         if os.path.isdir(dist.location):<br>>             for pkg in setuptools.find_packages(dist.location):<br>>                 if '.' not in pkg:<br>>                     pkgs.add(pkg)<br>> <br>> The idea is then to loop through that list of modules, import them and get their exact locations by fetching their __file__ attribute values.<br>> <br>> 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.<br>> <br>> Do you have any tips on how to achieve this?<br>> <br>> Many thanks!<br>> <br>> Julien<br>> -- <br>> http://mail.python.org/mailman/listinfo/python-list<br></div>                                         </div></body>
</html>