Include File?

Discussion in 'Python' started by Whofer, Oct 3, 2003.

  1. Whofer

    Whofer Guest

    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
     
    Whofer, Oct 3, 2003
    #1
    1. Advertising

  2. Whofer

    Peter Otten Guest

    Whofer wrote:

    > 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
     
    Peter Otten, Oct 3, 2003
    #2
    1. Advertising

  3. Whofer

    Peter Hansen Guest

    Whofer wrote:
    >
    > 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
     
    Peter Hansen, Oct 3, 2003
    #3
  4. Whofer

    Cam Guest

    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
    >
    >
     
    Cam, Oct 6, 2003
    #4
  5. Whofer

    Harry George Guest

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

    --

    6-6M31 Knowledge Management
    Phone: (425) 342-5601
     
    Harry George, Oct 6, 2003
    #5
  6. On Mon, 6 Oct 2003 06:52:12 GMT, Harry George <> wrote:

    >Cam <> 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
     
    Bengt Richter, Oct 7, 2003
    #6
  7. Whofer

    Anand Pillai Guest

    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


    (Bengt Richter) wrote in message news:<bltf05$ndh$0@216.39.172.122>...
    > On Mon, 6 Oct 2003 06:52:12 GMT, Harry George <> wrote:
    >
    > >Cam <> 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
     
    Anand Pillai, Oct 7, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    #include "file" -vs- #include <file>

    Victor Bazarov, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    539
    Exits Funnel
    Mar 6, 2005
  2. PTM
    Replies:
    1
    Views:
    333
    Andy Dingley
    Nov 12, 2007
  3. naveeddil
    Replies:
    0
    Views:
    582
    naveeddil
    Jan 4, 2008
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    932
    Andreas Bogenberger
    Feb 22, 2008
  5. Eric
    Replies:
    10
    Views:
    475
Loading...

Share This Page