Include File?

Anand Pillai pythonguy at Hotpop.com
Tue Oct 7 13:30:32 EDT 2003


If you feel like doing an <include xxx.py> think Java,
and your brain will put an automatic callback and convert
it to <import xxx>...

Python is similar to Java here. I cannot claim that this
syntaxt is borrowed from Java, but at least it is very similar.

Just as Java uses CLASSPATH to manage its namespaces in the
<import> directive python uses PYTHONPATH and sys.path in its
<import> directive.

The namespace rules are also quite similar.

So when you start to code your python program, 
think Java, not C or C++.

It helps...At least with those imports!

This is a begineer's thread, so I dont want to dissect this
issue further.

-Anand


bokr at oz.net (Bengt Richter) wrote in message news:<bltf05$ndh$0 at 216.39.172.122>...
> 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