Take the 2-minute tour ×

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 ?

share|improve this question
add comment

5 Answers

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:

  1. whenever called, get the current stacktrace and work out the calling function. Maybe the information in the globals parameter to __import__ is enough.
  2. get the module of that calling functions and store the name of this module and what will get imported
  3. redirect the call the real __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.

share|improve this answer
Something like this should work. It gets around the "you can't know what the code will do without doing something equivalent to running it" by actually running it. But it still only gets you the modules that were imported on one particular one, not everything it could need to import. That's probably enough for a Python module with reasonably static imports, but if it has reasonably static imports just grepping for "import" is probably enough. –  Ben Sep 17 '11 at 2:36
add comment

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.

share|improve this answer
add comment

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?

share|improve this answer
add comment

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.

share|improve this answer
add comment

This is not possible to do 100% accurately. I answered a similar question here: Dependency Testing with Python

share|improve this answer
add comment

Your Answer


