Determining which things in 'from package import *' are actually used

Robert Kern robert.kern at gmail.com
Tue Jun 10 21:03:37 CEST 2008


Patrick Bouffard wrote:
> I have a fairly large library of Python code, where 'from package import *' is
> used rather liberally, and it's not uncommon for more than one of these to
> appear in any given module. What I'd like to be able to do is to clean my code
> up a bit and turn each of the 'from package import *' statements into 'from
> package import thing_1, thing_2, ..., thing_n', where only thing_i's that are
> actually _used_ in the module are imported. In this way I hope to make my code a
> bit more understandable by future civilizations. :) (it needs all the help it
> can get!)
> 
> Does anyone know of a package/recipe/whatever that does something like this
> automatically, even in limited cases? Ideally it should be accomplished only by
> looking at the source, or at most, importing the module.

I don't know of any automatic tools. I usually comment out those import 
statements and use pyflakes to show me the undefined names.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list