Re: Discover all non-standard library modules imported by a script

2016-09-16 Thread Malcolm Greene
Thanks for your suggestions Chris and Terry.

The answer I was looking for is the modulefinder module which is part of
the standard lib. Works like a charm!

Quote: This module provides a ModuleFinder class that can be used to
determine the set of modules imported by a script. modulefinder.py can
also be run as a script, giving the filename of a Python script as its
argument, after which a report of the imported modules will be printed.

https://docs.python.org/3.5/library/modulefinder.html 

Note there's a similar module for Python 2.7.

--
Malcolm
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Discover all non-standard library modules imported by a script

2016-09-16 Thread Terry Reedy

On 9/16/2016 7:29 AM, Malcolm Greene 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 " and "from  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.


You could check the .__file__ attribute of each module in sys.modules 
for 'site-packages', (or more generally, for non-stdlib locations).


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Discover all non-standard library modules imported by a script

2016-09-16 Thread Chris Angelico
On Fri, Sep 16, 2016 at 9:29 PM, Malcolm Greene  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 " and "from  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
-- 
https://mail.python.org/mailman/listinfo/python-list