Is there a tool to check what names I have used from a "wildly" imported module?
<p>I've been using python to do computations for my research. In an effort to clean up my terrible code, I've been reading <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html" rel="nofollow">Code Like a Pythonista: Idiomatic Python</a> by David Goodger. </p>

<p>In this article, Goodger advises against "wild-card" imports of the form</p>

<pre><code>from module import *
</code></pre>

<p>My code uses a lot of these. I'd like to clean my code up, but I'm not sure how. I'm curious if there is a way to check what names from <code>module</code> I have used in my code. This way I could either explicitly import these names or replace the names with <code>module.name</code>. Is there a tool designed to accomplish such a task?</p>

<p>Use a tool like <code>pyflakes</code> (which you should use anyway) to note which names in your code become undefined after you replace <code>from module import *</code> with <code>import module</code>. Once you've positively identified every instance of a name imported from <code>module</code>, you can assess whether to</p>

<ol>
<li>Always use <code>import module</code> and <code>module.x</code> for <code>x</code> imported from <code>module</code>.</li>
<li>Use <code>import module as m</code> and <code>m.x</code> if the module name is long.</li>
<li>Selectively import some names from <code>module</code> into the module namespace with <code>from module import x, y, z</code></li>
</ol>

<p>The above three are not mutually exclusive; as an extreme example, you can use all three in the same module:</p>

<pre><code>import really.long.module.name
import really.long.module.name as rlmn
from really.long.module.name import obvious_name

really.long.module.name.foo()  # full module name
rlmn.bar()                     # module alias
obvious_name()                 # imported name
</code></pre>

<p>all in the same code. (I don't <em>recommend</em> using all three in the same module. Stick with either the full module name <em>or</em> the alias throughout a single module, but there is no harm importing common, obvious names directly and using the fully qualified name for more obscure module attributes.)</p>

python