26.29 无some.py时"import some.pyc" https://scz.617.cn/python/201309251607.txt Q: 现有is_prime_pub.cpython-39.opt-2.pyc,没有is_prime_pub.py,此种情况可以直 接执行pyc python is_prime_pub.cpython-39.opt-2.pyc 59 想进一步,在other.py中"import is_prime_pub",并调用is_prime_pub中函数。 A: falsetru@stackoverflow 2013-09-25 参看 Import arbitrary python source file. (Python 3.3+) https://stackoverflow.com/questions/19009932/import-arbitrary-python-source-file-python-3-3 有多种实现,用Python 3.9.0测试无误 -------------------------------------------------------------------------- import importlib.machinery import importlib.util # # Load the compiled module from the .pyc file # loader = importlib.machinery.SourcelessFileLoader( 'is_prime_pub', 'is_prime_pub.cpython-39.opt-2.pyc' ) spec = importlib.util.spec_from_loader( loader.name, loader ) is_prime_pub = importlib.util.module_from_spec( spec ) loader.exec_module( is_prime_pub ) # # Call a method in the module # low_prime = [2,3] low_prime += [x for x in range(5,2047,2) if is_prime_pub.rabin_miller_test(x,b=2,rounds=1)] print( is_prime_pub.is_prime( 59, low_prime, 5 ) ) -------------------------------------------------------------------------- import importlib.machinery import types # # Load the compiled module from the .pyc file # loader = importlib.machinery.SourcelessFileLoader( 'is_prime_pub', 'is_prime_pub.cpython-39.opt-2.pyc' ) is_prime_pub = types.ModuleType( loader.name ) loader.exec_module( is_prime_pub ) # # Call a method in the module # low_prime = [2,3] low_prime += [x for x in range(5,2047,2) if is_prime_pub.rabin_miller_test(x,b=2,rounds=1)] print( is_prime_pub.is_prime( 59, low_prime, 5 ) ) -------------------------------------------------------------------------- 用的是SourcelessFileLoader,不是SourceFileLoader,用后者时报错 ValueError: source code string cannot contain null bytes 无some.py直接加载some.pyc时,解释器会检查pyc首部"magic number",必须匹配当 前解释器版本,否则报错,比如 ImportError: bad magic number in 'is_prime_pub': b'a\r\r\n' -------------------------------------------------------------------------- import importlib.machinery # # Load the compiled module from the .pyc file # loader = importlib.machinery.SourcelessFileLoader( 'is_prime_pub', 'is_prime_pub.cpython-39.opt-2.pyc' ) is_prime_pub = loader.load_module() # # Call a method in the module # low_prime = [2,3] low_prime += [x for x in range(5,2047,2) if is_prime_pub.rabin_miller_test(x,b=2,rounds=1)] print( is_prime_pub.is_prime( 59, low_prime, 5 ) ) -------------------------------------------------------------------------- load_module()已废弃,不建议使用,会有警告,但在Python 3.9.0中仍可用 DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead -------------------------------------------------------------------------- import warnings warnings.filterwarnings( "ignore", category=DeprecationWarning ) import sys, marshal, imp def ManualLoadModule ( modulename, pycfile ) : header_size_list = \ [ (16, (3, 7)), (12, (3, 3)), (8, (0, 9, 2)) ] header_size = next( size for size, ver in header_size_list if sys.version_info >= ver ) with open( pycfile, "rb" ) as f : f.read( header_size ) co = marshal.load( f ) # # end of with # m = imp.new_module( modulename ) exec( co, m.__dict__ ) # print( dir( m ) ) sys.modules[modulename] \ = m # # end of ManualLoadModule # ManualLoadModule( 'is_prime_pub', 'is_prime_pub.cpython-39.opt-2.pyc' ) import is_prime_pub # # Call a method in the module # low_prime = [2,3] low_prime += [x for x in range(5,2047,2) if is_prime_pub.rabin_miller_test(x,b=2,rounds=1)] print( is_prime_pub.is_prime( 59, low_prime, 5 ) ) -------------------------------------------------------------------------- warnings.filterwarnings是为了抑制如下警告 DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses