470,643 Members | 1,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

recursive import list

Hi,

I have a fairly large project going on and would like to figure out
automatically from the source which files are being imported.

ex: find_out mymain.py
Is there an easy way to achieve that ?

Regards,

Philippe

Jul 19 '05 #1
3 1647
If you use your own import function, like below, you could create a
list of all imported modules.

#!/usr/bin/env python

mod_list = []

def my_import(name, globals = None, locals = None, fromlist = None):
mod_list.append(name)
mod = __import__(name, globals, locals, fromlist)
return mod

os = my_import('os')
print os.name

print mod_list

sys = my_import('sys')
print sys.version

print mod_list

Jul 19 '05 #2
"wi******@hotmail.com" <ma**********@gmail.com> writes:
I have a fairly large project going on and would like to figure out
automatically from the source which files are being imported.

If you use your own import function, like below, you could create a
list of all imported modules.


Why not use sys.modules? To answer the question actually asked, check
for a __file__ attributes on each module in sys.modules, and print
that if it exists.

That won't list builtin modules that are imported - but they don't
have files to be imported, so I assume that the OP doesn't want them
listed.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #3
Mike Meyer wrote:
"wi******@hotmail.com" <ma**********@gmail.com> writes:
I have a fairly large project going on and would like to figure out
automatically from the source which files are being imported.

If you use your own import function, like below, you could create a
list of all imported modules.


Why not use sys.modules? To answer the question actually asked, check
for a __file__ attributes on each module in sys.modules, and print
that if it exists.

That won't list builtin modules that are imported - but they don't
have files to be imported, so I assume that the OP doesn't want them
listed.

<mike

Thanks to both, I'll try this.

My goal would have been to just launch the "recipe" on the main module, and
get the list for all modules.

Regards,

Philippe

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stian Søiland | last post: by
3 posts views Thread by Michael Foord | last post: by
10 posts views Thread by Steve Goldman | last post: by
7 posts views Thread by bearophileHUGS | last post: by
7 posts views Thread by aurora | last post: by
10 posts views Thread by AsheeG87 | last post: by
reply views Thread by oj | last post: by
reply views Thread by Tim Golden | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.