newbie question about import

C

Craig

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
 
C

Craig

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

Craig

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
 
S

Scott David Daniels

Craig said:
... 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.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top