Discover all non-standard library modules imported by a script

Chris Angelico rosuav at gmail.com
Fri Sep 16 08:13:05 EDT 2016


On Fri, Sep 16, 2016 at 9:29 PM, Malcolm Greene <python at bdurham.com> wrote:
> Looking for suggestions on how, given a main script, discover all the
> non-standard library modules imported across all modules, eg. the
> modules that other modules import, etc. I'm not looking to discover
> dynamic imports or other edge cases, just the list modules loaded via
> "import <module>" and "from <module> import ...". I know I could write a
> script to do this, but certainly there must be such a capability in the
> standard library?
>
> Use case: Discovering list of modules to use for building a Python 3.5
> zipapp distributable.
>

Possibly not what you're thinking of, but try creating a clean virtual
environment (python3 -m venv env) and installing no packages, and then
watch for ImportError. As you find them, record stuff into
requirements.txt and "pip install -r requirements.txt", until you have
a working program - at which point requirements.txt should have every
non-standard thing you're using.

ChrisA



More information about the Python-list mailing list