If you are intent on not using sys.path.append
to import the file, I was able to do it using the following snippet in file1.py
:
import imp
import os
file = os.sep+os.path.join(*os.path.realpath(__file__).split(os.sep)[:-1]+['..','libs','package.so'])
package = imp.load_source('root.libs.package', file)
print package.a()
Which prints <root.libs.package.a instance at 0x101845518>
. Note that my package.so file is just a python file that defines a dummy class a
so that I could test importing it. From what I have read, I believe that replacing imp.load_source
with imp.load_dynamic
may be what you want.
Breaking it down:
os.path.realpath(__file__).split(os.sep)[:-1]
gets you the path to the directory the currently-running script is in, as a list of strings.
+['..','libs','package.so']
concatenates a list containing the parent directory (..
), the libs
directory, and your filename to that list, so that os.path.join will build the full path to the package.so
file.
os.path.join(*[that list])
unpacks the list elements into arguments to os.path.join
, and joins the strings with os.sep
. I also add a leading os.sep
since it is an absolute path.
imp.load_source
returns a module with the name root.libs.package
loaded from the file path.
This source was useful for writing this answer, and here are the docs for the imp
module, which you might also find useful.