Include File?

Bengt Richter bokr at oz.net
Tue Oct 7 00:21:25 EDT 2003


On Mon, 6 Oct 2003 06:52:12 GMT, Harry George <harry.g.george at boeing.com> wrote:

>Cam <no-spam_please__cpbarton at pacific.net.au> writes:
>
>> I am not so new and I still dont know if its possible.
>> I 2nd that question.
>> 
>> Whofer wrote:
>> > Hi
>> > i am a totally python newcomer. Can i use
>> > include files within python?
>> > (like for example:  include <xxx.py>)
>> > Thanks for help in advance
>> > Werner Hofer
>> >
>> 
>
>There is no raw-file include (as in C's cpp) in the Python language.
>(Of course, you could use #include and run cpp on the file
>before passing it to Python.)
>
>The Pythonic way to do this is "import".  Unlike cpp, the imported
>file must be a valid python module in its own right, though it can be
>pretty simplistic.  E.g., you could just define a list.
>
I think maybe execfile comes closest to the effect of an include, in the sense
of interpreting the text of the file as if it were substituted for the including line.
At least at global scope and for simple "include" files. There are gotchas in some
contexts, but its more of an include than import. That said, the OP is probably
better off solving his problem with import than with execfile.

Regards,
Bengt Richter




More information about the Python-list mailing list