I have built a .pyd module from some fortran code via f2py
, which we can call foo.pyd
. I want to import this into a python code, which we can call main.py
. When I import as:
import foo
everything works fine. However, because this python will eventually be 'frozen' into an executable, and I will have to package the foo.pyd
along with the main.exe
, I need to specify an explicit path to import from.
Thus, I tried using the imp
module, specifically imp.load_compiled()
. When I try:
import imp
foo = imp.load_compiled('foo','foo.pyd')
or even:
foo = imp.load_compiled('foo','./foo.pyd')
I get the following error:
ImportError: Bad magic number in foo.pyd
It seems that this error comes from trying to build in one Python dist, and import in another. However, I'm using the same Python for both, and it works with the standard import
command!
Has anyone out there experienced this, and/or possibly have some guidance? I greatly appreciate any help or advice you may have.
imp.load_compiled()
is for .pyc
files. You're looking for imp.load_dynamic()
. Alternatively, you can add the directory you want in sys.path
, after which regular imports will work.