Reputation: 479
How does one get (finds the location of) the dynamically imported modules from a python script ?
so, python from my understanding can dynamically (at run time) load modules. Be it using _import_(module_name), or using the exec "from x import y", either using imp.find_module("module_name") and then imp.load_module(param1, param2, param3, param4) .
Knowing that I want to get all the dependencies for a python file. This would include getting (or at least I tried to) the dynamically loaded modules, those loaded either by using hard coded string objects or those returned by a function/method.
For normal import module_name and from x import y you can do either a manual scanning of the code or use module_finder.
So if I want to copy one python script and all its dependencies (including the custom dynamically loaded modules) how should I do that ?
Upvotes: 2
Views: 670
Reputation: 7644
Just an idea and I'm not sure that it will work:
You could write a module that contains a wrapper for __builtin__.__import__
. This wrapper would save a reference to the old __import__
and then assign a function to __builtin__.__import__
that does the following:
globals
parameter to __import__
is enough.__import__
After you have done this you can call your application with python -m magic_module yourapp.py
. The magic module must store the information somewhere where you can retrieve it later.
Upvotes: 1
Reputation: 18041
That's quite of a question.
Static analysis is about predicting all possible run-time execution paths and making sure the program halts for specific input at all.
Which is equivalent to Halting Problem and unfortunately there is no generic solution.
The only way to resolve dynamic dependencies is to run the code.
Upvotes: 0
Reputation: 185
From a theoretical perspective, you can never know exactly what/where modules are being imported. From a practical perspective, if you simply want to know where the modules are, check the module.__file__
attribute or run the script under python -v
to find files when modules are loaded. This won't give you every module that could possibly be loaded, but will get most modules with mostly sane code.
See also: How do I find the location of Python module sources?
Upvotes: 2
Reputation: 1312
You can't; the very nature of programming (in any language) means that you cannot predict what code will be executed without actually executing it. So you have no way of telling which modules could be included.
This is further confused by user-input, consider: __import__(sys.argv[1])
.
There's a lot of theoretical information about the first problem, which is normally described as the Halting problem, the second just obviously can't be done.
Upvotes: 3
Reputation: 71400
This is not possible to do 100% accurately. I answered a similar question here: Dependency Testing with Python
Upvotes: 1