How do I systematically identify the dependencies Python has across its accessible package/module tree?

StackOverflow https://stackoverflow.com/questions/15309685

Question

Question: How can I systematically probe into files that are involved at any time by the interpreter (like in debug mode).

When everything fails I get error message. What I ask for is the opposite: Everything works, but I don't know how much redundant rubbish I have in comparison to its usage, even though I can imagine that something like pynotify probably could trace it.

Context:

I've spent all morning exercising trial & error to get a package to work. I'm sure I have copied the relevant python package into at least 3 directories and messed up my windows setx -m path badly with junk. Now I'm wondering how to clean it all up without breaking any dependencies, and actually learn from the process.

I can't possibly be the only one wondering about this. Some talented test-developer must have written a script/package that:

import everything from everywhere
check for all dependencies
E = list(errorMessages)
L = list_of_stuff_that_was_used
print L
print E

so if I have something stored which is not in L, I can delete it. But of course the probing has to be thorough to exhaust all accessible files (or at least actively used directories).

What the question is NOT about: I'm not interested in what is on the sys.path. This is trivial.

More Context:

I know from The Hitchhikers Guide to Packaging that the future of this problem is being adressed, however it does not probe into the past. So with the transition from Python 2xx to 3xx this problem must become more and more relevant?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top