Include File?

W

Whofer

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
 
P

Peter Otten

Whofer said:
i am a totally python newcomer. Can i use
include files within python?

(like for example: include <xxx.py>)


The python equivalent is

import xxx

#use it, assuming the doSomething() function is defined in module xxx:
xxx.doSomething()

If you already know a programming language, the tutorial that comes with the
python distribution is a good starting point to learn the language.
Good luck!

Peter
 
P

Peter Hansen

Whofer said:
i am a totally python newcomer. Can i use
include files within python?

(like for example: include <xxx.py>)

Have you gone through the several tutorials and web pages for newcomers?

Questions like this should be adequately covered in them, so it would
be surprising if you've read them yet still don't know about "import".

-Peter
 
H

Harry George

Cam said:
I am not so new and I still dont know if its possible.
I 2nd that question.

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.
 
B

Bengt Richter

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
 
A

Anand Pillai

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top