I have a directory structure similar to this ...
dir/ build.py dir2 dir3/ packages.py
Now build.py
requires packages.py
- and note that dir2
not a package.
So, what is the best way to get packages.py
loaded in build.py
(the directory structure cannot be changed)
EDIT
The solution sys.path.append
seems good - but there is one thing: I need to rarely use the packages.py
file and store sys.path
, which includes a directory that is rarely used, but located at the front - is this the best?
EDIT II
I think the best solution is imp
.
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 are deprecated . Therefore, you should use 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()
source share