include statement

Discussion in 'Python' started by joakim.hove@gmail.com, Sep 20, 2006.

  1. Guest

    Hello,

    is it possible in python to include another python source file into the
    current namespace, i.e.completely analogous to the #include statement
    in C.

    Regards Joakim
    , Sep 20, 2006
    #1
    1. Advertising

  2. Ravi Teja Guest

    > is it possible in python to include another python source file into the
    > current namespace, i.e.completely analogous to the #include statement
    > in C.


    By using a pre-processor, like C does.
    http://www.freenet.org.nz/python/pyp/

    If you are new to Python, keep in mind that this is for special cases
    only. Since Python has a module system, there isn't a need for #include
    as in C.
    Ravi Teja, Sep 20, 2006
    #2
    1. Advertising

  3. Gary Herron Guest

    wrote:
    > Hello,
    >
    > is it possible in python to include another python source file into the
    > current namespace, i.e.completely analogous to the #include statement
    > in C.
    >
    > Regards Joakim
    >
    >

    No (thank heavens)! We left #include and other such primitive
    functionality back with the dinosaurs (C and its relatives) when we
    moved into the world of tje modern sophisticated programming language
    Python. Tell us why you are contemplating such a thing, and someone
    here will help you implement it the "Pythonic" way.

    Gary Herron
    Gary Herron, Sep 20, 2006
    #3
  4. MonkeeSage Guest

    MonkeeSage, Sep 20, 2006
    #4
  5. Guest

    Thanks to all who took time to answer!


    > > is it possible in python to include another python source file into the
    > > current namespace, i.e.completely analogous to the #include statement
    > > in C.


    [...]

    > Tell us why you are contemplating such a thing, and someone
    > here will help you implement it the "Pythonic" way.


    My application has a configuration file where lots of variables are
    set. (The configuration file is python source, so there is no
    home-brewed parsing involved.) The configuration file is starting to
    get quite large and unwieldy, so for this reason I would like to split
    it in several files.

    I know I could do:
    from configA import *
    from configB import *

    But I felt that the import statemant was 'more' than I wanted. Maybe I
    am just pedantic.

    Regards

    Joakim
    , Sep 20, 2006
    #5
  6. Peter Otten Guest

    wrote:

    > Thanks to all who took time to answer!
    >
    >
    >> > is it possible in python to include another python source file into the
    >> > current namespace, i.e.completely analogous to the #include statement
    >> > in C.

    >
    > [...]
    >
    >> Tell us why you are contemplating such a thing, and someone
    >> here will help you implement it the "Pythonic" way.

    >
    > My application has a configuration file where lots of variables are
    > set. (The configuration file is python source, so there is no
    > home-brewed parsing involved.) The configuration file is starting to
    > get quite large and unwieldy, so for this reason I would like to split
    > it in several files.
    >
    > I know I could do:
    > from configA import *
    > from configB import *
    >
    > But I felt that the import statemant was 'more' than I wanted. Maybe I
    > am just pedantic.


    Maybe you like execfile() which would allow you to collect the configuration
    files without the need for them to be reachable through the import
    mechanism.

    pattern = os.path.expanduser("~/.herron/config*.py")

    for fn in glob.glob(pattern):
    execfile(fn)

    # objects in the config*.py files now
    # share one namespace.

    Peter
    Peter Otten, Sep 20, 2006
    #6
  7. wrote:
    > Thanks to all who took time to answer!
    >
    >
    >>> is it possible in python to include another python source file into the
    >>> current namespace, i.e.completely analogous to the #include statement
    >>> in C.

    >
    > [...]
    >
    >> Tell us why you are contemplating such a thing, and someone
    >> here will help you implement it the "Pythonic" way.

    >
    > My application has a configuration file where lots of variables are
    > set. (The configuration file is python source, so there is no
    > home-brewed parsing involved.) The configuration file is starting to
    > get quite large and unwieldy, so for this reason I would like to split
    > it in several files.
    >
    > I know I could do:
    > from configA import *
    > from configB import *
    >
    > But I felt that the import statemant was 'more' than I wanted. Maybe I
    > am just pedantic.


    Maybe... "import" is the very appropriate statement for what you want
    here IMVHO. I'd just put the config<X>.py files into a 'config'
    directory, add an __init__.py, and put the 'from configX import *'
    there. Then in the app code, a simple 'import config', which allow acces
    to config vars via 'config.varname' (clean namespaces really improve
    maintainability).

    My 2 cents

    > Regards
    >
    > Joakim
    >



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    Bruno Desthuilliers, Sep 20, 2006
    #7
  8. Larry Bates Guest

    wrote:
    > Thanks to all who took time to answer!
    >
    >
    >>> is it possible in python to include another python source file into the
    >>> current namespace, i.e.completely analogous to the #include statement
    >>> in C.

    >
    > [...]
    >
    >> Tell us why you are contemplating such a thing, and someone
    >> here will help you implement it the "Pythonic" way.

    >
    > My application has a configuration file where lots of variables are
    > set. (The configuration file is python source, so there is no
    > home-brewed parsing involved.) The configuration file is starting to
    > get quite large and unwieldy, so for this reason I would like to split
    > it in several files.
    >
    > I know I could do:
    > from configA import *
    > from configB import *
    >
    > But I felt that the import statemant was 'more' than I wanted. Maybe I
    > am just pedantic.
    >
    > Regards
    >
    > Joakim
    >

    I might also suggest that you consider using ConfigParser module to store
    your "variables". If you are worried about performance, I can tell you that
    I use it to feed thousands of lines of configuration info into one of my
    applications (this app has 5 of these files) and ConfigParser handles them
    so quickly that performance is NOT a problem. The format of the file is
    easily understandable by nearly every user which means I don't have to
    worry with someone not understanding Python's syntax. If someone gets
    something wrong in a file that gets included, it is harder to take a sane
    default or handle the error than with ConfigParser. Just a suggestion that
    I hope helps.

    -Larry Bates

    or
    Larry Bates, Sep 20, 2006
    #8
  9. At Wednesday 20/9/2006 03:50, wrote:

    >My application has a configuration file where lots of variables are
    >set. (The configuration file is python source, so there is no
    >home-brewed parsing involved.) The configuration file is starting to
    >get quite large and unwieldy, so for this reason I would like to split
    >it in several files.
    >
    >I know I could do:
    >from configA import *
    >from configB import *
    >
    >But I felt that the import statemant was 'more' than I wanted. Maybe I
    >am just pedantic.


    You can limit the range of "from...import *" declaring a __all__
    variable in the imported module; this way you import exactly what is
    needed (and the rest is kept as "private")



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
    Gabriel Genellina, Sep 20, 2006
    #9
    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. Replies:
    3
    Views:
    783
  2. Jay McGavren
    Replies:
    11
    Views:
    1,099
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,051
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    444
    Duncan Booth
    Jul 22, 2004
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    861
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page