I have a directory structure like this...
dir/
build.py
dir2
dir3/
packages.py
Now the build.py
needs packages.py
-- and note that dir2
is not a package.
So what's the best way to get packages.py
loaded into build.py
(the directory structure can't be changed)
EDIT
The sys.path.append
solution seems good -- but there is one thing -- I need to use the packages.py
file rarely -- and keeping a sys.path
that includes a directory that is used rarely, but is at the front -- is that the best thing?
EDIT II
I think the imp
solution is best.
import imp
packages = imp.load_source('packages', '/path/to/packages.py')
EDIT III
for Python 3.x
Note that imp.load_source
and some other function have been deprecated. So you should use the imp.load_module
today.
fp, pathname, description = imp.find_module('packages', '/path/to/packages.py')
try:
mod = imp.load_module('packages', fp, pathname, description)
finally:
# since we may exit via an exception, close fp explicitly
if fp:
fp.close()