recursive import list

P

Philippe C. Martin

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
 
W

wittempj

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
 
M

Mike Meyer

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
 
P

Philippe C. Martin

Mike said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,241
Messages
2,571,223
Members
47,856
Latest member
mmorais

Latest Threads

Top