Note that it was necessary to switch the PYD module to be read by 32bitOS and 64bitOS. If the module you want to load is xxx.yyy.zzz and your environment is Win64, Make an import hook to read xxx.yyy. ** win64 **. zzz.
from xxx.yyy import zzz
#If this import statement is a win64 environment
# from xxx.yyy.win64 import zzz
#To be the same process as
5. Import System — Python 3.4.3 Document # import-hooks
import sys
def get_env():
platform = ''
if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
platform = 'win'
elif sys.platform.startswith('darwin'):
platform = 'mac'
elif sys.platform.startswith('linux'):
platform = 'lnx'
return platform + ('32' if sys.maxsize < 2**31 else '64')
The OS is determined by ** sys.platform **. You can tell whether it is 32bit or 64bit with ** sys.maxsize **. Return'win64'for Win64 and'mac64' for Mac.
import os.path
from importlib import import_module
from importlib.abc import MetaPathFinder
from importlib.machinery import ModuleSpec, ExtensionFileLoader
#Get the execution environment
env = get_env()
class PydMetaPathFinder(MetaPathFinder):
def find_spec(fullname, path, target=None):
if path is None:
return None
#Scan the entire list of paths
for p in path:
#Module existence confirmation
pac_mod = fullname.rsplit('.', maxsplit=1)
pac,mod = pac_mod[0],pac_mod[-1]
origin = os.path.join(p, env, mod+'.pyd')
if os.path.exists(origin):
#The parent package must be imported
import_module('.'.join([pac,env]))
name = '.'.join([pac,env,mod])
return ModuleSpec(
name=name,
loader=ExtensionFileLoader(name, origin),
origin=origin,
)
This time, we will create a Finder to be registered in ** sys.meta_path **, so make it a subclass of ** MetaPathFinder **. The MetaPathFinder abstract base class wants to override the find_spec abstract method, so we'll implement it.
The return value will be an instance of ** ModuleSpec **. If the module cannot be loaded, return None and the process will be delegated to another Finder.
--The parent package of the module to be loaded is not automatically imported. ** importlib.import_module ** will automatically import the parent package, so import the parent package before loading the target module. --This time, I used ExtensionFileLoader because it is an import of PYD file, but I think that if it is a normal PY file, it will be read by SourceFileLoader.
31.5. importlib – The implementation of import — Python 3.5.0 documentation #import_module 31.5. importlib – The implementation of import — Python 3.5.0 documentation #MetaPathFinder 31.5. importlib – The implementation of import — Python 3.5.0 documentation #SourceFileLoader 31.5. importlib – The implementation of import — Python 3.5.0 documentation #ExtensionFileLoader 31.5. importlib – The implementation of import — Python 3.5.0 documentation #ModuleSpec
import sys
sys.meta_path.append(PydMetaPathFinder)
** sys.meta_path ** is a list of Finder, so you can append a subclass of Finder. After append, when the import statement is executed, the registered class will be inquired.
If you can use cython's pyximport, we recommend that.
Recommended Posts