Import a module without executing it?

Caleb Hattingh caleb1 at telkomsa.net
Tue Dec 7 22:56:09 EST 2004


Hi

You could just parse the model file.  Off the top of my head

***
f = open('ModuleYouWantToExamine.py','r')
for i in f:
     if i.find('def ') > -1:
         print 'Found a function!: '+i.replace('def ','')

f.close()
***

You would have to build this up for a more complete examination.  Of  
course, one of the guru's around here should be able to give you guidance  
regarding actually parsing the file with the interpreter (not executing)  
and building a dict or something with all the different types of  
constructs.  That's not me :)

>
> I want to be able to import this module so I can see "ah ha, this module  
> defines a function called 'test'", but I don't want  the code at the  
> bottom executed during the import.
>
> Thanks
>
> Take care,
> Jay




More information about the Python-list mailing list