[Tutor] import glob.glob('*.py')

Kent Johnson kent37 at tds.net
Mon Jan 8 14:38:42 CET 2007


János Juhász wrote:
> I plan to make a python script, that collect all the projectfiles from 
> that folder and render them as a report summary.
> I planned to import these files as modules like 
> 
> for filename in glob.glob('*.py'):
>     if '_' in filename: continue
>     import filename
>     render(filename)


This won't work, the import statement does not take a variable as an 
argument. You need something like
module = __import__(filename)
render(module)

You will want to take care that your modules don't have any side effects 
on import.

> 
> Probably you have better ideas to do that.

You might want to look at the existing Python document generation tools. 
There is a summary here (be sure to read the comments and the original 
post just below this one):
http://www.voidspace.org.uk/python/weblog/arch_d7_2006_12_30.shtml#e599

In particular PythonDoc supports structured comments that are similar to 
what you outlined and has pluggable output generators that could be used 
to drive ReportLab.

Kent



More information about the Tutor mailing list