Finding which modules a Python program uses

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Sep 10 11:13:03 EDT 2001


"Andre M. Descombes" <amdescombes at testandgo.com> writes:

> I want to distribute my Python application, but I want to deploy only the
> modules it uses. The problem is I don't know what they are, for example if I
> use the string module, which modules does it need?
> Is there a way of automatically finding a list of all needed modules for a
> program?

Depends on the meaning of "all" and "needed" :-) A simple approach is
to run "python -v"; that way, you find all the modules that a
particular run of your application needs - that may not be all modules
that it could potentially access.

The other option is to look for all import statements. Tools/freeze
uses that approach, and provides the modulefinder object for that
purpose. With that approach, you may include modules which might never
be used, since they only act as fallbacks for cases that never
occur. Also, if your applications executes code that is created only
at runtime, or invokes __import__ directly, modulefinder will not find
these modules.

Hope this helps,
Martin



More information about the Python-list mailing list