Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question about import

Reply
Thread Tools

newbie question about import

 
 
Craig
Guest
Posts: n/a
 
      06-03-2004
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


 
Reply With Quote
 
 
 
 
Craig
Guest
Posts: n/a
 
      06-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Craig
Guest
Posts: n/a
 
      06-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      06-03-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to use import java.lang.* or import java.lang.Math or none at all? JPractitioner Java 13 02-24-2006 08:48 PM
XML Schema question - does "import" import elements? Vitali Gontsharuk XML 2 08-25-2005 07:33 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
GTK import doesn't import first time Dennis Python 2 08-18-2003 10:00 PM
import/from import question Artur M. Piwko Python 1 07-02-2003 07:04 PM



Advertisments