擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/library/pkgutil.html" rel="noreferrer">python2.3 and above</a>,还可以使用<code>pkgutil</code>模块:</p>
<pre><code>>>> import pkgutil
>>> [name for _, name, _ in pkgutil.iter_modules(['testpkg'])]
['modulea', 'moduleb']
</code></pre>
<p><strong>编辑:</strong>请注意,参数不是模块列表,而是路径列表,因此您可能需要执行以下操作:</p>
<pre><code>>>> import os.path, pkgutil
>>> import testpkg
>>> pkgpath = os.path.dirname(testpkg.__file__)
>>> print [name for _, name, _ in pkgutil.iter_modules([pkgpath])]
</code></pre>