Import a module without executing it?

Andy Gross andy at andygross.org
Tue Dec 7 21:57:16 CET 2004


You'll want to use the "compiler" package.  compiler.parseFile will 
return an AST that you can inspect (which is not really 'reflection', 
btw).

/arg


On Dec 7, 2004, at 10:56 PM, Caleb Hattingh wrote:

> 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
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list