newbie question about import

Discussion in 'Python' started by Craig, Jun 3, 2004.

  1. Craig

    Craig Guest

    hi all,

    is there a python equivalent to the C preprocessor statement #include
    relative-or-absolute-path?
    i want to import code in a specific file into my script.

    thanks,
    craig
     
    Craig, Jun 3, 2004
    #1
    1. Advertising

  2. Craig

    Craig Guest

    anwering my own query...i see that this a common question amongst python
    newbies. i shall look around the web a bit and see if i can work it out.

    "Craig" <> wrote in message
    news:...
    > hi all,
    >
    > is there a python equivalent to the C preprocessor statement #include
    > relative-or-absolute-path?
    > i want to import code in a specific file into my script.
    >
    > thanks,
    > craig
    >
    >
     
    Craig, Jun 3, 2004
    #2
    1. Advertising

  3. Craig

    Craig Guest

    ok, so i understand that there are important differences between import and
    #include, but the thing i don't get is why you can't do something like
    import "d:\dev\build\config.py"?

    is it good style to do this to get the above behaviour:

    import sys
    sys.path.append(/my/include/path)
    import my-include-file


    "Craig" <> wrote in message
    news:...
    > anwering my own query...i see that this a common question amongst python
    > newbies. i shall look around the web a bit and see if i can work it out.
    >
    > "Craig" <> wrote in message
    > news:...
    > > hi all,
    > >
    > > is there a python equivalent to the C preprocessor statement #include
    > > relative-or-absolute-path?
    > > i want to import code in a specific file into my script.
    > >
    > > thanks,
    > > craig
    > >
    > >

    >
    >
     
    Craig, Jun 3, 2004
    #3
  4. Craig wrote:
    >... is it good style to do this to get the above behaviour:
    >
    > import sys
    > sys.path.append(/my/include/path)
    > import my-include-file

    Assuming you quote properly, and your file is nicely named:
    sys.path.append('/my/include/path')
    import my-include-file

    Typically you will only have one or two directories you need
    to add, and you _can_ make a file "my.pth" which consists
    of the directory names you care about, and throw "my.pth"
    in <pythontop>/lib/site-packages

    The idea is to keep the source as portable as possible. Each OS
    has a different syntax for directory names; you could easily
    become non-portable that way. Note the "append to sys.path"
    solution does not address this. Also note, code like:

    import sys
    if myextdir not in sys.path:
    sys.path.append(myextdir)

    may work a little better for you.

    --
    -Scott David Daniels
     
    Scott David Daniels, Jun 3, 2004
    #4
    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. Paul Opal
    Replies:
    12
    Views:
    1,002
    Paul Opal
    Oct 11, 2004
  2. JPractitioner
    Replies:
    13
    Views:
    20,235
    Roedy Green
    Feb 24, 2006
  3. Vitali Gontsharuk
    Replies:
    2
    Views:
    605
    Vitali Gontsharuk
    Aug 25, 2005
  4. Artur M. Piwko

    import/from import question

    Artur M. Piwko, Jun 29, 2003, in forum: Python
    Replies:
    1
    Views:
    896
    Peter Hansen
    Jul 2, 2003
  5. Dennis
    Replies:
    2
    Views:
    532
    Dennis
    Aug 18, 2003
Loading...

Share This Page