The dynamic nature of python makes this a next to impossible task.
Functions can import too, for example. Are you going to run all code in all modules?
And then there are backward-compatibility tests; import pysqlite2
if sqlite3
is not present, use a backport module if collections.Counter
isn't present in the current version of Python, etc. There are platform-specific modules (os.path
is posixpath
, ntpath
(same code but renamed) or riscospath
depending on the current platform), and whole-sale imports into the main module (posix
, nt
, os2
, ce
and riscos
all can be used by the os
module depending on the platform to supply functions).
Packages that use setuptools
declare their dependencies and are discoverable through the pkg_resources
library. That's the limit of what you can reasonably discover.