{"id":167690,"date":"2023-08-03T21:33:27","date_gmt":"2023-08-03T13:33:27","guid":{"rendered":"http:\/\/www.idc.net\/help\/167690\/"},"modified":"2023-08-03T21:33:27","modified_gmt":"2023-08-03T13:33:27","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9aimportlib-import-%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/167690\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1aimportlib \u2014- import \u7684\u5b9e\u73b0"},"content":{"rendered":"<h1><code>importlib<\/code> \u2014- <code>import<\/code> \u7684\u5b9e\u73b0<\/h1>\n<p>3.1 \u65b0\u7248\u529f\u80fd.<\/p>\n<p><strong>\u6e90\u4ee3\u7801<\/strong> Lib\/importlib\/__init__.py<\/p>\n<hr>\n<h2>\u6982\u8ff0<\/h2>\n<p>The purpose of the importlib package is three-fold.<\/p>\n<p>One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of <code>import<\/code> which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u76ee\u7684\u662f\u5b9e\u73b0 import \u7684\u90e8\u5206\u88ab\u516c\u5f00\u5728\u8fd9\u4e2a\u5305\u4e2d\uff0c\u4f7f\u5f97\u7528\u6237\u66f4\u5bb9\u6613\u521b\u5efa\u4ed6\u4eec\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61 (\u901a\u5e38\u88ab\u79f0\u4e3a importer) \u6765\u53c2\u4e0e\u5230\u5bfc\u5165\u8fc7\u7a0b\u4e2d\u3002<\/p>\n<p>Three, the package contains modules exposing additional functionality for managing aspects of Python packages:<\/p>\n<ul>\n<li>\n<p>importlib.metadata presents access to metadata from third-party distributions.<\/p>\n<\/li>\n<li>\n<p>importlib.resources provides routines for accessing non-code \u201cresources\u201d from Python packages.<\/p>\n<\/li>\n<\/ul>\n<p>\u53c2\u89c1<\/p>\n<p>import \u8bed\u53e5<\/p>\n<p>import \u8bed\u53e5\u7684\u8bed\u8a00\u53c2\u8003<\/p>\n<p>\u5305\u89c4\u683c\u8bf4\u660e<\/p>\n<p>\u5305\u7684\u521d\u59cb\u89c4\u8303\u3002\u81ea\u4ece\u7f16\u5199\u8fd9\u4e2a\u6587\u6863\u5f00\u59cb\uff0c\u4e00\u4e9b\u8bed\u4e49\u5df2\u7ecf\u53d1\u751f\u6539\u53d8\u4e86\uff08\u6bd4\u5982\u57fa\u4e8e sys.modules \u4e2d <code>None<\/code> \u7684\u91cd\u5b9a\u5411\uff09\u3002<\/p>\n<p>__import__() \u51fd\u6570<\/p>\n<p>import \u8bed\u53e5\u662f\u8fd9\u4e2a\u51fd\u6570\u7684\u8bed\u6cd5\u7cd6\u3002<\/p>\n<p>The initialization of the sys.path module search path<\/p>\n<p>The initialization of sys.path.<\/p>\n<p>PEP 235<\/p>\n<p>\u5728\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5e73\u53f0\u4e0a\u8fdb\u884c\u5bfc\u5165<\/p>\n<p>PEP 263<\/p>\n<p>\u5b9a\u4e49 Python \u6e90\u4ee3\u7801\u7f16\u7801<\/p>\n<p>PEP 302<\/p>\n<p>\u65b0\u5bfc\u5165\u94a9\u5b50<\/p>\n<p>PEP 328<\/p>\n<p>\u5bfc\u5165\uff1a\u591a\u884c\u548c\u7edd\u5bf9\/\u76f8\u5bf9<\/p>\n<p>PEP 366<\/p>\n<p>\u4e3b\u6a21\u5757\u663e\u5f0f\u76f8\u5bf9\u5bfc\u5165<\/p>\n<p>PEP 420<\/p>\n<p>\u9690\u5f0f\u547d\u540d\u7a7a\u95f4\u5305<\/p>\n<p>PEP 451<\/p>\n<p>\u5bfc\u5165\u7cfb\u7edf\u7684\u4e00\u4e2a\u6a21\u5757\u89c4\u8303\u7c7b\u578b<\/p>\n<p>PEP 488<\/p>\n<p>\u6d88\u9664PYO\u6587\u4ef6<\/p>\n<p>PEP 489<\/p>\n<p>\u591a\u9636\u6bb5\u6269\u5c55\u6a21\u5757\u521d\u59cb\u5316<\/p>\n<p>PEP 552<\/p>\n<p>\u786e\u5b9a\u6027\u7684 pyc \u6587\u4ef6<\/p>\n<p>PEP 3120<\/p>\n<p>\u4f7f\u7528 UTF-8 \u4f5c\u4e3a\u9ed8\u8ba4\u7684\u6e90\u7f16\u7801<\/p>\n<p>PEP 3147<\/p>\n<p>PYC \u4ed3\u5e93\u76ee\u5f55<\/p>\n<h2>\u51fd\u6570<\/h2>\n<p>importlib.__import__(<em>name<\/em>, <em>globals=None<\/em>, <em>locals=None<\/em>, <em>fromlist=()<\/em>, <em>level=0<\/em>)<\/p>\n<p>\u5185\u7f6e __import__() \u51fd\u6570\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5907\u6ce8<\/p>\n<p>\u7a0b\u5e8f\u5f0f\u5730\u5bfc\u5165\u6a21\u5757\u5e94\u8be5\u4f7f\u7528 import_module() \u800c\u4e0d\u662f\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<p>importlib.import_module(<em>name<\/em>, <em>package=None<\/em>)<\/p>\n<p>\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u3002 \u53c2\u6570 <em>name<\/em> \u6307\u5b9a\u4e86\u4ee5\u7edd\u5bf9\u6216\u76f8\u5bf9\u5bfc\u5165\u65b9\u5f0f\u5bfc\u5165\u4ec0\u4e48\u6a21\u5757 (\u6bd4\u5982\u8981\u4e48\u50cf\u8fd9\u6837 <code>pkg.mod<\/code> \u6216\u8005\u8fd9\u6837 <code>..mod<\/code>)\u3002 \u5982\u679c\u53c2\u6570 name \u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u7684\u65b9\u5f0f\u6765\u6307\u5b9a\uff0c\u90a3\u4e48 <em>package<\/em> \u53c2\u6570\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u90a3\u4e2a\u5305\u540d\uff0c\u8fd9\u4e2a\u5305\u540d\u4f5c\u4e3a\u89e3\u6790\u8fd9\u4e2a\u5305\u540d\u7684\u951a\u70b9 (\u6bd4\u5982 <code>import_module('..mod', 'pkg.subpkg')<\/code> \u5c06\u4f1a\u5bfc\u5165 <code>pkg.mod<\/code>)\u3002<\/p>\n<p>import_module() \u51fd\u6570\u662f\u4e00\u4e2a\u5bf9 importlib.__import__() \u8fdb\u884c\u7b80\u5316\u7684\u5305\u88c5\u5668\u3002 \u8fd9\u610f\u5473\u7740\u8be5\u51fd\u6570\u7684\u6240\u6709\u8bed\u4e49\u90fd\u6765\u81ea\u4e8e importlib.__import__()\u3002 \u8fd9\u4e24\u4e2a\u51fd\u6570\u4e4b\u95f4\u6700\u91cd\u8981\u7684\u4e0d\u540c\u70b9\u5728\u4e8e import_module() \u8fd4\u56de\u6307\u5b9a\u7684\u5305\u6216\u6a21\u5757 (\u4f8b\u5982 <code>pkg.mod<\/code>)\uff0c\u800c __import__() \u8fd4\u56de\u6700\u9ad8\u5c42\u7ea7\u7684\u5305\u6216\u6a21\u5757 (\u4f8b\u5982 <code>pkg<\/code>)\u3002<\/p>\n<p>\u5982\u679c\u52a8\u6001\u5bfc\u5165\u4e00\u4e2a\u81ea\u89e3\u91ca\u5668\u5f00\u59cb\u6267\u884c\u4ee5\u6765\u88ab\u521b\u5efa\u7684\u6a21\u5757\uff08\u5373\u521b\u5efa\u4e86\u4e00\u4e2a Python \u6e90\u4ee3\u7801\u6587\u4ef6\uff09\uff0c\u4e3a\u4e86\u8ba9\u5bfc\u5165\u7cfb\u7edf\u77e5\u9053\u8fd9\u4e2a\u65b0\u6a21\u5757\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u7528 invalidate_caches()\u3002<\/p>\n<p>\u5728 3.3 \u7248\u66f4\u6539: \u7236\u5305\u4f1a\u88ab\u81ea\u52a8\u5bfc\u5165\u3002<\/p>\n<p>importlib.find_loader(<em>name<\/em>, <em>path=None<\/em>)<\/p>\n<p>\u67e5\u627e\u4e00\u4e2a\u6a21\u5757\u7684\u52a0\u8f7d\u5668\uff0c\u53ef\u9009\u62e9\u5730\u5728\u6307\u5b9a\u7684 <em>path<\/em> \u91cc\u9762\u3002\u5982\u679c\u8fd9\u4e2a\u6a21\u5757\u662f\u5728 sys.modules\uff0c\u90a3\u4e48\u8fd4\u56de <code>sys.modules[name].__loader__<\/code> (\u9664\u975e\u8fd9\u4e2a\u52a0\u8f7d\u5668\u662f <code>None<\/code> \u6216\u8005\u662f\u6ca1\u6709\u88ab\u8bbe\u7f6e\uff0c \u5728\u8fd9\u6837\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u5f15\u8d77 ValueError \u5f02\u5e38\uff09\u3002 \u5426\u5219\u4f7f\u7528 sys.meta_path \u7684\u4e00\u6b21\u641c\u7d22\u5c31\u7ed3\u675f\u3002\u5982\u679c\u672a\u53d1\u73b0\u52a0\u8f7d\u5668\uff0c\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<p>\u70b9\u72b6\u7684\u540d\u79f0\u6ca1\u6709\u4f7f\u5f97\u5b83\u7236\u5305\u6216\u6a21\u5757\u9690\u5f0f\u5730\u5bfc\u5165\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u52a0\u8f7d\u5b83\u4eec\u5e76\u4e14\u53ef\u80fd\u4e0d\u9700\u8981\u3002\u4e3a\u4e86\u9002\u5f53\u5730\u5bfc\u5165\u4e00\u4e2a\u5b50\u6a21\u5757\uff0c\u9700\u8981\u5bfc\u5165\u5b50\u6a21\u5757\u7684\u6240\u6709\u7236\u5305\u5e76\u4e14\u4f7f\u7528\u6b63\u786e\u7684\u53c2\u6570\u63d0\u4f9b\u7ed9 <em>path<\/em>\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5982\u679c\u6ca1\u6709\u8bbe\u7f6e <code>__loader__<\/code>\uff0c\u4f1a\u5f15\u8d77 ValueError \u5f02\u5e38\uff0c\u5c31\u50cf\u5c5e\u6027\u8bbe\u7f6e\u4e3a <code>None<\/code> \u7684\u65f6\u5019\u4e00\u6837\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 importlib.util.find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<p>importlib.invalidate_caches()<\/p>\n<p>\u4f7f\u67e5\u627e\u5668\u5b58\u50a8\u5728 sys.meta_path \u4e2d\u7684\u5185\u90e8\u7f13\u5b58\u65e0\u6548\u3002\u5982\u679c\u4e00\u4e2a\u67e5\u627e\u5668\u5b9e\u73b0\u4e86 <code>invalidate_caches()<\/code>\uff0c\u90a3\u4e48\u5b83\u4f1a\u88ab\u8c03\u7528\u6765\u6267\u884c\u90a3\u4e2a\u65e0\u6548\u8fc7\u7a0b\u3002 \u5982\u679c\u521b\u5efa\/\u5b89\u88c5\u4efb\u4f55\u6a21\u5757\uff0c\u540c\u65f6\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u662f\u4e3a\u4e86\u4fdd\u8bc1\u6240\u6709\u7684\u67e5\u627e\u5668\u77e5\u9053\u65b0\u6a21\u5757\u7684\u5b58\u5728\uff0c\u90a3\u4e48\u5e94\u8be5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.10 \u7248\u66f4\u6539: Namespace packages created\/installed in a different sys.path location after the same namespace was already imported are noticed.<\/p>\n<p>importlib.reload(<em>module<\/em>)<\/p>\n<p>\u91cd\u65b0\u52a0\u8f7d\u4e4b\u524d\u5bfc\u5165\u7684 <em>module<\/em>\u3002 \u90a3\u4e2a\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u6a21\u5757\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4e4b\u524d\u5fc5\u987b\u5df2\u7ecf\u6210\u529f\u5bfc\u5165\u4e86\u3002 \u8fd9\u5728\u4f60\u5df2\u7ecf\u4f7f\u7528\u5916\u90e8\u7f16\u8f91\u5668\u7f16\u8f91\u8fc7\u4e86\u90a3\u4e2a\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u5e76\u4e14\u60f3\u5728\u9000\u51fa Python \u89e3\u91ca\u5668\u4e4b\u524d\u8bd5\u9a8c\u8fd9\u4e2a\u65b0\u7248\u672c\u7684\u6a21\u5757\u7684\u65f6\u5019\u5c06\u5f88\u9002\u7528\u3002 \u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u90a3\u4e2a\u6a21\u5757\u5bf9\u8c61\uff08\u5982\u679c\u91cd\u65b0\u5bfc\u5165\u5bfc\u81f4\u4e00\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\u653e\u7f6e\u5728 sys.modules \u4e2d\uff0c\u90a3\u4e48\u90a3\u4e2a\u6a21\u5757\u5bf9\u8c61\u662f\u6709\u53ef\u80fd\u4f1a\u4e0d\u540c\uff09\u3002<\/p>\n<p>\u5f53\u6267\u884c reload() \u7684\u65f6\u5019\uff1a<\/p>\n<ul>\n<li>\n<p>Python \u6a21\u5757\u7684\u4ee3\u7801\u4f1a\u88ab\u91cd\u65b0\u7f16\u8bd1\u5e76\u4e14\u90a3\u4e2a\u6a21\u5757\u7ea7\u7684\u4ee3\u7801\u88ab\u91cd\u65b0\u6267\u884c\uff0c\u901a\u8fc7\u91cd\u65b0\u4f7f\u7528\u4e00\u5f00\u59cb\u52a0\u8f7d\u90a3\u4e2a\u6a21\u5757\u7684 loader\uff0c\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u7ed1\u5b9a\u5728\u90a3\u4e2a\u6a21\u5757\u5b57\u5178\u4e2d\u7684\u540d\u79f0\u7684\u5bf9\u8c61\u96c6\u5408\u3002\u6269\u5c55\u6a21\u5757\u7684``init``\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\u7b2c\u4e8c\u6b21\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0ePython\u4e2d\u7684\u6240\u6709\u7684\u5176\u5b83\u5bf9\u8c61\u4e00\u6837\uff0c\u65e7\u7684\u5bf9\u8c61\u53ea\u6709\u5728\u5b83\u4eec\u7684\u5f15\u7528\u8ba1\u6570\u4e3a0\u4e4b\u540e\u624d\u4f1a\u88ab\u56de\u6536\u3002<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5757\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u540d\u79f0\u91cd\u65b0\u6307\u5411\u4efb\u4f55\u65b0\u7684\u6216\u66f4\u6539\u540e\u7684\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u65e7\u5bf9\u8c61\u7684\u5f15\u7528\uff08\u4f8b\u5982\u90a3\u4e2a\u6a21\u5757\u7684\u5916\u90e8\u540d\u79f0\uff09\u4e0d\u4f1a\u88ab\u91cd\u65b0\u7ed1\u5b9a\u5230\u5f15\u7528\u7684\u65b0\u5bf9\u8c61\u7684\uff0c\u5e76\u4e14\u5982\u679c\u6709\u9700\u8981\uff0c\u5fc5\u987b\u5728\u51fa\u73b0\u7684\u6bcf\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6709\u4e00\u4e9b\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<p>\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u91cd\u65b0\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u5b83\u7684\u5b57\u5178\uff08\u5305\u542b\u4e86\u90a3\u4e2a\u6a21\u5757\u7684\u5168\u533a\u53d8\u91cf\uff09\u4f1a\u88ab\u4fdd\u7559\u3002\u540d\u79f0\u7684\u91cd\u65b0\u5b9a\u4e49\u4f1a\u8986\u76d6\u65e7\u7684\u5b9a\u4e49\uff0c\u6240\u4ee5\u901a\u5e38\u6765\u8bf4\u8fd9\u4e0d\u662f\u95ee\u9898\u3002\u5982\u679c\u4e00\u4e2a\u65b0\u6a21\u5757\u6ca1\u6709\u5b9a\u4e49\u5728\u65e7\u7248\u672c\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\uff0c\u5219\u5c06\u4fdd\u7559\u65e7\u7248\u672c\u4e2d\u7684\u5b9a\u4e49\u3002\u8fd9\u4e00\u7279\u6027\u53ef\u7528\u4e8e\u4f5c\u4e3a\u90a3\u4e2a\u6a21\u5757\u7684\u4f18\u70b9\uff0c\u5982\u679c\u5b83\u7ef4\u62a4\u4e00\u4e2a\u5168\u5c40\u8868\u6216\u8005\u5bf9\u8c61\u7684\u7f13\u5b58 \u2014\u2014 \u4f7f\u7528 try \u8bed\u53e5\uff0c\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u8868\u7684\u5b58\u5728\u5e76\u4e14\u8df3\u8fc7\u5b83\u7684\u521d\u59cb\u5316\uff0c\u5982\u679c\u6709\u9700\u8981\u7684\u8bdd:<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>try<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> cache<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>except<\/span><span> <\/span><span>NameError<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> cache <\/span><span>=<\/span><span> <\/span><span>{}<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<p>\u91cd\u65b0\u52a0\u8f7d\u5185\u7f6e\u7684\u6216\u8005\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\uff0c\u901a\u5e38\u6765\u8bf4\u4e0d\u662f\u5f88\u6709\u7528\u5904\u3002\u4e0d\u63a8\u8350\u91cd\u65b0\u52a0\u8f7d\u201dsys\uff0c__main__\uff0cbuiltins \u548c\u5176\u5b83\u5173\u952e\u6a21\u5757\u3002\u5728\u5f88\u591a\u4f8b\u5b50\u4e2d\uff0c\u6269\u5c55\u6a21\u5757\u5e76\u4e0d\u662f\u8bbe\u8ba1\u4e3a\u4e0d\u6b62\u4e00\u6b21\u7684\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u5f53\u91cd\u65b0\u52a0\u8f7d\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ee5\u4efb\u610f\u65b9\u5f0f\u5931\u8d25\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u4f7f\u7528 from \u2026 import \u2026 \u5bfc\u5165\u7684\u5bf9\u8c61\u6765\u81ea\u53e6\u5916\u4e00\u4e2a\u6a21\u5757\uff0c\u7ed9\u5176\u5b83\u6a21\u5757\u8c03\u7528 reload() \u4e0d\u4f1a\u91cd\u65b0\u5b9a\u4e49\u6765\u81ea\u8fd9\u4e2a\u6a21\u5757\u7684\u5bf9\u8c61 \u2014\u2014 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u5f0f\u662f\u91cd\u65b0\u6267\u884c <code>from<\/code> \u8bed\u53e5\uff0c\u53e6\u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 <code>import<\/code> \u548c\u9650\u5b9a\u540d\u79f0(<em>module.name<\/em>)\u6765\u4ee3\u66ff\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u91cd\u65b0\u52a0\u8f7d\u5b9a\u4e49\u90a3\u4e2a\u7c7b\u7684\u6a21\u5757\u4e0d\u5f71\u54cd\u90a3\u4e9b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u5b9a\u4e49\u2014\u2014\u2014\u5b83\u4eec\u7ee7\u7eed\u4f7f\u7528\u65e7\u7c7b\u4e2d\u7684\u5b9a\u4e49\u3002\u5bf9\u4e8e\u5b50\u7c7b\u6765\u8bf4\u540c\u6837\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.7 \u7248\u66f4\u6539: \u5982\u679c\u91cd\u65b0\u52a0\u8f7d\u7684\u6a21\u5757\u7f3a\u5c11 ModuleSpec \uff0c\u5219\u4f1a\u89e6\u53d1 ModuleNotFoundError \u3002<\/p>\n<h2>importlib.abc \u2014\u2014 \u5173\u4e8e\u5bfc\u5165\u7684\u62bd\u8c61\u57fa\u7c7b<\/h2>\n<p><strong>\u6e90\u4ee3\u7801\uff1a<\/strong> Lib\/importlib\/abc.py<\/p>\n<hr>\n<p>The importlib.abc module contains all of the core abstract base classes used by import. Some subclasses of the core abstract base classes are also provided to help in implementing the core ABCs.<\/p>\n<p>ABC \u7c7b\u7684\u5c42\u6b21\u7ed3\u6784\uff1a<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>object<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>Finder<\/span><span> <\/span><span>(<\/span><span>deprecated<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>MetaPathFinder<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>PathEntryFinder<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>Loader<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>ResourceLoader<\/span><span> <\/span><span>--------+<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>InspectLoader<\/span><span> <\/span><span>|<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>ExecutionLoader<\/span><span> <\/span><span>--+<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>FileLoader<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>+--<\/span><span> <\/span><span>SourceLoader<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<p><em>class<\/em> importlib.abc.Finder<\/p>\n<p>\u4ee3\u8868 finder \u7684\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b<\/p>\n<p>3.3 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 MetaPathFinder \u6216 PathEntryFinder \u6765\u4ee3\u66ff\u3002<\/p>\n<ul>\n<li>\n<p><em>abstractmethod<\/em> find_module(<em>fullname<\/em>, <em>path=None<\/em>)<\/p>\n<p>\u4e3a\u6307\u5b9a\u7684\u6a21\u5757\u67e5\u627e loader \u5b9a\u4e49\u7684\u62bd\u8c61\u65b9\u6cd5\u3002\u672c\u6765\u662f\u5728 PEP 302 \u6307\u5b9a\u7684\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728 sys.meta_path \u548c\u57fa\u4e8e\u8def\u5f84\u7684\u5bfc\u5165\u5b50\u7cfb\u7edf\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f53\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u8fd4\u56de <code>None<\/code> \u800c\u4e0d\u662f\u5f15\u53d1 NotImplementedError\u3002<\/p>\n<p>3.10 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u4e3a\u5b9e\u73b0 MetaPathFinder.find_spec() \u6216 PathEntryFinder.find_spec() \u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.MetaPathFinder<\/p>\n<p>\u4e00\u4e2a\u4ee3\u8868 meta path finder \u7684\u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.10 \u7248\u66f4\u6539: \u4e0d\u518d\u662f Finder \u7684\u5b50\u7c7b\u3002<\/p>\n<ul>\n<li>\n<p>find_spec(<em>fullname<\/em>, <em>path<\/em>, <em>target=None<\/em>)<\/p>\n<p>\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u6307\u5b9a\u6a21\u5757\u7684 spec \u3002\u82e5\u662f\u9876\u5c42\u5bfc\u5165\uff0c<em>path<\/em> \u5c06\u4e3a <code>None<\/code>\u3002 \u5426\u5219\u5c31\u662f\u67e5\u627e\u5b50\u5305\u6216\u6a21\u5757\uff0c<em>path<\/em> \u5c06\u662f\u7236\u7ea7\u5305\u7684 __path__ \u503c\u3002\u627e\u4e0d\u5230\u5219\u4f1a\u8fd4\u56de <code>None<\/code>\u3002\u4f20\u5165\u7684 <code>target<\/code> \u662f\u4e00\u4e2a\u6a21\u5757\u5bf9\u8c61\uff0c\u67e5\u627e\u5668\u53ef\u4ee5\u7528\u6765\u5bf9\u8fd4\u56de\u7684\u89c4\u683c\u8fdb\u884c\u66f4\u6709\u4f9d\u636e\u7684\u731c\u6d4b\u3002\u5728\u5b9e\u73b0\u5177\u4f53\u7684 <code>MetaPathFinders<\/code> \u4ee3\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u7528\u5230 importlib.util.spec_from_loader() \u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>find_module(<em>fullname<\/em>, <em>path<\/em>)<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u67e5\u627e\u6307\u5b9a\u7684\u6a21\u5757\u4e2d loader \u7684\u9057\u7559\u65b9\u6cd5\u3002\u5982\u679c\u8fd9\u662f\u6700\u9ad8\u5c42\u7ea7\u7684\u5bfc\u5165\uff0c<em>path<\/em> \u7684\u503c\u5c06\u4f1a\u662f <code>None<\/code>\u3002\u5426\u5219\uff0c\u8fd9\u662f\u4e00\u4e2a\u67e5\u627e\u5b50\u5305\u6216\u8005\u6a21\u5757\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14 <em>path<\/em> \u7684\u503c\u5c06\u4f1a\u662f\u6765\u81ea\u7236\u5305\u7684 __path__ \u7684\u503c\u3002\u5982\u679c\u672a\u53d1\u73b0\u52a0\u8f7d\u5668\uff0c\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<p>\u5982\u679c\u5b9a\u4e49\u4e86 find_spec() \u65b9\u6cd5\uff0c\u5219\u63d0\u4f9b\u4e86\u5411\u540e\u517c\u5bb9\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f53\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\u8fd4\u56de <code>None<\/code> \u800c\u4e0d\u662f\u5f15\u53d1 NotImplementedError\u3002 \u53ef\u4ee5\u4f7f\u7528 find_spec() \u6765\u63d0\u4f9b\u529f\u80fd\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p>invalidate_caches()<\/p>\n<p>\u5f53\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u53ef\u9009\u7684\u65b9\u6cd5\u5e94\u8be5\u5c06\u67e5\u627e\u5668\u4f7f\u7528\u7684\u4efb\u4f55\u5185\u90e8\u7f13\u5b58\u8fdb\u884c\u65e0\u6548\u3002\u5c06\u5728 sys.meta_path \u4e0a\u7684\u6240\u6709\u67e5\u627e\u5668\u7684\u7f13\u5b58\u8fdb\u884c\u65e0\u6548\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u51fd\u6570\u88ab importlib.invalidate_caches() \u6240\u4f7f\u7528\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f53\u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u65b9\u6cd5\u8fd4\u56de\u662f <code>None<\/code> \u800c\u4e0d\u662f <code>NotImplemented<\/code> \u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.PathEntryFinder<\/p>\n<p>\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u4ee3\u8868 path entry finder\u3002\u867d\u7136\u4e0e MetaPathFinder \u6709\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46 PathEntryFinder \u4ec5\u7528\u4e8e importlib.machinery.PathFinder \u63d0\u4f9b\u7684\u57fa\u4e8e\u8def\u5f84\u7684\u5bfc\u5165\u5b50\u7cfb\u7edf\u4e2d\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.10 \u7248\u66f4\u6539: \u4e0d\u518d\u662f Finder \u7684\u5b50\u7c7b\u3002<\/p>\n<ul>\n<li>\n<p>find_spec(<em>fullname<\/em>, <em>target=None<\/em>)<\/p>\n<p>\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u6307\u5b9a\u6a21\u5757\u7684 spec\u3002\u641c\u7d22\u5668\u5c06\u53ea\u5728\u6307\u5b9a\u7684 path entry \u5185\u641c\u7d22\u8be5\u6a21\u5757\u3002\u627e\u4e0d\u5230\u5219\u4f1a\u8fd4\u56de <code>None<\/code>\u3002\u5728\u5b9e\u73b0\u5177\u4f53\u7684 <code>PathEntryFinders<\/code> \u4ee3\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u7528\u5230 importlib.util.spec_from_loader() \u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>find_loader(<em>fullname<\/em>)<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u5728\u6a21\u5757\u4e2d\u67e5\u627e\u4e00\u4e2a loader \u7684\u9057\u7559\u65b9\u6cd5\u3002 \u8fd4\u56de\u4e00\u4e2a <code>(loader, portion)<\/code> \u76842\u5143\u7ec4\uff0c<code>portion<\/code> \u662f\u4e00\u4e2a\u8d21\u732e\u7ed9\u547d\u540d\u7a7a\u95f4\u5305\u90e8\u5206\u7684\u6587\u4ef6\u7cfb\u7edf\u4f4d\u7f6e\u7684\u5e8f\u5217\u3002 \u52a0\u8f7d\u5668\u53ef\u80fd\u662f <code>None<\/code>\uff0c\u540c\u65f6\u6b63\u5728\u6307\u5b9a\u7684 <code>portion<\/code> \u8868\u793a\u7684\u662f\u8d21\u732e\u7ed9\u547d\u540d\u7a7a\u95f4\u5305\u7684\u6587\u4ef6\u7cfb\u7edf\u4f4d\u7f6e\u3002<code>portion<\/code> \u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u8868\u793a\u52a0\u8f7d\u5668\u4e0d\u662f\u547d\u540d\u7a7a\u95f4\u5305\u7684\u4e00\u90e8\u5206\u3002 \u5982\u679c <code>loader<\/code> \u662f <code>None<\/code> \u5e76\u4e14 <code>portion<\/code> \u662f\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u90a3\u4e48\u547d\u540d\u7a7a\u95f4\u5305\u4e2d\u65e0\u52a0\u8f7d\u5668\u6216\u8005\u6587\u4ef6\u7cfb\u7edf\u4f4d\u7f6e\u53ef\u67e5\u627e\u5230\uff08\u5373\u5728\u90a3\u4e2a\u6a21\u5757\u4e2d\u672a\u80fd\u627e\u5230\u4efb\u4f55\u4e1c\u897f\uff09\u3002<\/p>\n<p>\u5982\u679c\u5b9a\u4e49\u4e86 find_spec() \uff0c\u5219\u63d0\u4f9b\u4e86\u5411\u540e\u517c\u5bb9\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u8fd4\u56de <code>(None, [])<\/code> \u800c\u4e0d\u662f\u5f15\u53d1 NotImplementedError\u3002 \u5f53\u53ef\u4e8e\u63d0\u4f9b\u76f8\u5e94\u7684\u529f\u80fd\u7684\u65f6\u5019\uff0c\u4f7f\u7528 find_spec()\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p>find_module(<em>fullname<\/em>)<\/p>\n<p><code>Finder.find_module`\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u8be5\u65b9\u6cd5\u7b49\u4ef7\u4e8e``self.find_loader(fullname)[0]`()<\/code>\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p>invalidate_caches()<\/p>\n<p>\u53ef\u9009\u65b9\u6cd5\uff0c\u8c03\u7528\u540e\u5e94\u8ba9\u67e5\u627e\u5668\u7528\u5230\u7684\u6240\u6709\u5185\u90e8\u7f13\u5b58\u5931\u6548\u3002\u8981\u8ba9\u6240\u6709\u7f13\u5b58\u7684\u67e5\u627e\u5668\u7684\u7f13\u5b58\u65e0\u6548\u65f6\uff0c\u53ef\u4f9b importlib.machinery.PathFinder.invalidate_caches() \u8c03\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.Loader<\/p>\n<p>loader \u7684\u62bd\u8c61\u57fa\u7c7b\u3002 \u5173\u4e8e\u4e00\u4e2a\u52a0\u8f7d\u5668\u7684\u5b9e\u9645\u5b9a\u4e49\u8bf7\u67e5\u770b PEP 302\u3002<\/p>\n<p>Loaders that wish to support resource reading should implement a <code>get_resource_reader()<\/code> method as specified by importlib.resources.abc.ResourceReader.<\/p>\n<p>\u5728 3.7 \u7248\u66f4\u6539: Introduced the optional <code>get_resource_reader()<\/code> method.<\/p>\n<ul>\n<li>\n<p>create_module(<em>spec<\/em>)<\/p>\n<p>\u5f53\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u8fd4\u56de\u5c06\u8981\u4f7f\u7528\u7684\u90a3\u4e2a\u6a21\u5757\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u80fd\u8fd4\u56de <code>None<\/code> \uff0c\u8fd9\u6697\u793a\u7740\u5e94\u8be5\u53d1\u751f\u9ed8\u8ba4\u7684\u6a21\u5757\u521b\u5efa\u8bed\u4e49\u3002\u201d<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: This method is no longer optional when exec_module() is defined.<\/p>\n<\/li>\n<li>\n<p>exec_module(<em>module<\/em>)<\/p>\n<p>An abstract method that executes the module in its own namespace when a module is imported or reloaded. The module should already be initialized when exec_module() is called. When this method exists, create_module() must be defined.<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: create_module() must also be defined.<\/p>\n<\/li>\n<li>\n<p>load_module(<em>fullname<\/em>)<\/p>\n<p>A legacy method for loading a module. If the module cannot be loaded, ImportError is raised, otherwise the loaded module is returned.<\/p>\n<p>If the requested module already exists in sys.modules, that module should be used and reloaded. Otherwise the loader should create a new module and insert it into sys.modules before any loading begins, to prevent recursion from the import. If the loader inserted a module and the load fails, it must be removed by the loader from sys.modules; modules already in sys.modules before the loader began execution should be left alone (see importlib.util.module_for_loader()).<\/p>\n<p>The loader should set several attributes on the module (note that some of these attributes can change when a module is reloaded):<\/p>\n<ul>\n<li>\n<p>__name__<\/p>\n<p>The module\u2019s fully qualified name. It is <code>'__main__'<\/code> for an executed module.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>-<\/span><span> <\/span><span>[<\/span><span>\\_\\_file\\_\\_<\/span><span>](<\/span><span>$4cd7c0b31904cd2f<\/span><span>.<\/span><span>md<\/span><span>#file__ \"__file__\")<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>The<\/span><span> location the <\/span><span>[<\/span><span>loader<\/span><span>](<\/span><span>$8f6095f929cdb646<\/span><span>.<\/span><span>md<\/span><span>#term-loader) used to load the module. For example, for modules loaded from a .py file this is the filename. It is not set on all modules (e.g. built-in modules).<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>-<\/span><span> <\/span><span>[<\/span><span>\\_\\_cached\\_\\_<\/span><span>](<\/span><span>$4cd7c0b31904cd2f<\/span><span>.<\/span><span>md<\/span><span>#cached__ \"__cached__\")<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>The<\/span><span> filename of a compiled version of the <\/span><span>module<\/span><span>'s code. It is not set on all modules (e.g. built-in modules).<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>- [\\_\\_path\\_\\_]($4cd7c0b31904cd2f.md#path__ \"__path__\")<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> The list of locations where the package'<\/span><span>s submodules will be found<\/span><span>.<\/span><span> <\/span><span>Most<\/span><span> of the time <\/span><span>this<\/span><span> <\/span><span>is<\/span><span> a single directory<\/span><span>.<\/span><span> <\/span><span>The<\/span><span> <\/span><span>import<\/span><span> system passes <\/span><span>this<\/span><span> attribute to <\/span><span>`__import__()`<\/span><span> <\/span><span>and<\/span><span> to finders <\/span><span>in<\/span><span> the same way <\/span><span>as<\/span><span> <\/span><span>[<\/span><span>sys<\/span><span>.<\/span><span>path<\/span><span>](<\/span><span>$54eedd4d1535df46<\/span><span>.<\/span><span>md<\/span><span>#sys.path \"sys.path\") but just for the package. It is not set on non-package modules so it can be used as an indicator that the module is a package.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>-<\/span><span> <\/span><span>[<\/span><span>\\_\\_<\/span><span>package<\/span><span>\\_\\_<\/span><span>](<\/span><span>$4cd7c0b31904cd2f<\/span><span>.<\/span><span>md<\/span><span>#package__ \"__package__\")<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>The<\/span><span> fully qualified name of the <\/span><span>package<\/span><span> the <\/span><span>module<\/span><span> <\/span><span>is<\/span><span> <\/span><span>in<\/span><span> <\/span><span>(<\/span><span>or<\/span><span> the empty <\/span><span>string<\/span><span> <\/span><span>for<\/span><span> a top<\/span><span>-<\/span><span>level <\/span><span>module<\/span><span>).<\/span><span> <\/span><span>If<\/span><span> the <\/span><span>module<\/span><span> <\/span><span>is<\/span><span> a <\/span><span>package<\/span><span> <\/span><span>then<\/span><span> <\/span><span>this<\/span><span> <\/span><span>is<\/span><span> the same <\/span><span>as<\/span><span> <\/span><span>[<\/span><span>\\_\\_name\\_\\_<\/span><span>](<\/span><span>$4cd7c0b31904cd2f<\/span><span>.<\/span><span>md<\/span><span>#name__ \"__name__\").<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>-<\/span><span> <\/span><span>[<\/span><span>\\_\\_loader\\_\\_<\/span><span>](<\/span><span>$4cd7c0b31904cd2f<\/span><span>.<\/span><span>md<\/span><span>#loader__ \"__loader__\")<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>The<\/span><span> <\/span><span>[<\/span><span>loader<\/span><span>](<\/span><span>$8f6095f929cdb646<\/span><span>.<\/span><span>md<\/span><span>#term-loader) used to load the module.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>\u5f53<\/span><span> <\/span><span>[<\/span><span>exec<\/span><span>\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>exec_module <\/span><span>\"importlib.abc.Loader.exec_module\"<\/span><span>)<\/span><span> <\/span><span>\u53ef\u7528\u7684\u65f6\u5019\uff0c\u90a3\u4e48\u5219\u63d0\u4f9b\u4e86\u5411\u540e\u517c\u5bb9\u7684\u529f\u80fd\u3002<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>\u5728<\/span><span> <\/span><span>3.4<\/span><span> <\/span><span>\u7248\u66f4\u6539:<\/span><span> <\/span><span>Raise<\/span><span> <\/span><span>[<\/span><span>ImportError<\/span><span>](<\/span><span>$e69e6dad35dc3610<\/span><span>.<\/span><span>md<\/span><span>#ImportError \"ImportError\") when called instead of [NotImplementedError]($e69e6dad35dc3610.md#NotImplementedError \"NotImplementedError\"). Functionality provided when [exec\\_module()](#importlib.abc.Loader.exec_module \"importlib.abc.Loader.exec_module\") is available.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>3.4<\/span><span> <\/span><span>\u7248\u540e\u5df2\u79fb\u9664:<\/span><span> <\/span><span>The<\/span><span> recommended API <\/span><span>for<\/span><span> loading a <\/span><span>module<\/span><span> <\/span><span>is<\/span><span> <\/span><span>[<\/span><span>exec<\/span><span>\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>exec_module <\/span><span>\"importlib.abc.Loader.exec_module\"<\/span><span>)<\/span><span> <\/span><span>(<\/span><span>and<\/span><span> <\/span><span>[<\/span><span>create\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>create_module <\/span><span>\"importlib.abc.Loader.create_module\"<\/span><span>)).<\/span><span> <\/span><span>Loaders<\/span><span> should implement it instead of <\/span><span>[<\/span><span>load\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>load_module <\/span><span>\"importlib.abc.Loader.load_module\"<\/span><span>).<\/span><span> <\/span><span>The<\/span><span> <\/span><span>import<\/span><span> machinery takes care of all the other responsibilities of <\/span><span>[<\/span><span>load\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>load_module <\/span><span>\"importlib.abc.Loader.load_module\"<\/span><span>)<\/span><span> <\/span><span>when<\/span><span> <\/span><span>[<\/span><span>exec<\/span><span>\\_<\/span><span>module<\/span><span>()](#<\/span><span>importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>.<\/span><span>exec_module <\/span><span>\"importlib.abc.Loader.exec_module\"<\/span><span>)<\/span><span> <\/span><span>is<\/span><span> implemented<\/span><span>.<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<ul>\n<li>\n<p>module_repr(<em>module<\/em>)<\/p>\n<p>A legacy method which when implemented calculates and returns the given module\u2019s representation, as a string. The module type\u2019s default <code>__repr__()<\/code> will use the result of this method as appropriate.<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u662f\u53ef\u9009\u7684\u65b9\u6cd5\u800c\u4e0d\u662f\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u73b0\u5728\u5bfc\u5165\u673a\u5236\u4f1a\u81ea\u52a8\u5730\u5173\u6ce8\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.ResourceLoader<\/p>\n<p>\u4e00\u4e2a loader \u7684\u62bd\u8c61\u57fa\u7c7b\uff0c\u5b83\u5b9e\u73b0\u4e86\u53ef\u9009\u7684 PEP 302 \u534f\u8bae\u7528\u4e8e\u4ece\u5b58\u50a8\u540e\u7aef\u52a0\u8f7d\u4efb\u610f\u8d44\u6e90\u3002<\/p>\n<p>3.7 \u7248\u540e\u5df2\u79fb\u9664: This ABC is deprecated in favour of supporting resource loading through importlib.resources.abc.ResourceReader.<\/p>\n<ul>\n<li>\n<p><em>abstractmethod<\/em> get_data(<em>path<\/em>)<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u8fd4\u56de\u4f4d\u4e8e <em>path<\/em> \u7684\u5b57\u8282\u6570\u636e\u7684\u62bd\u8c61\u65b9\u6cd5\u3002\u6709\u4e00\u4e2a\u5141\u8bb8\u5b58\u50a8\u4efb\u610f\u6570\u636e\u7684\u7c7b\u6587\u4ef6\u5b58\u50a8\u540e\u7aef\u7684\u52a0\u8f7d\u5668\u80fd\u591f\u5b9e\u73b0\u8fd9\u4e2a\u62bd\u8c61\u65b9\u6cd5\u6765\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u88ab\u5b58\u50a8\u7684\u6570\u636e\u3002\u5982\u679c\u4e0d\u80fd\u591f\u627e\u5230 <em>path<\/em>\uff0c\u5219\u4f1a\u5f15\u53d1 OSError \u5f02\u5e38\u3002<em>path<\/em> \u88ab\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u6a21\u5757\u7684 <code>__file<\/code> \u5c5e\u6027\u6216\u6765\u81ea\u4e00\u4e2a\u5305\u7684 __path__ \u6765\u6784\u5efa\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f15\u53d1 OSError \u5f02\u5e38\u800c\u4e0d\u662f NotImplementedError \u5f02\u5e38\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.InspectLoader<\/p>\n<p>\u4e00\u4e2a\u5b9e\u73b0\u52a0\u8f7d\u5668\u68c0\u67e5\u6a21\u5757\u53ef\u9009\u7684 PEP 302 \u534f\u8bae\u7684 loader \u7684\u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<ul>\n<li>\n<p>get_code(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a\u6a21\u5757\u7684\u4ee3\u7801\u5bf9\u8c61\uff0c\u6216\u5982\u679c\u6a21\u5757\u6ca1\u6709\u4e00\u4e2a\u4ee3\u7801\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5185\u7f6e\u7684\u6a21\u5757\u6765\u8bf4\uff0c\u8fd9\u4f1a\u662f\u8fd9\u79cd\u60c5\u51b5\uff09\uff0c\u5219\u4e3a <code>None<\/code>\u3002 \u5982\u679c\u52a0\u8f7d\u5668\u4e0d\u80fd\u627e\u5230\u8bf7\u6c42\u7684\u6a21\u5757\uff0c\u5219\u5f15\u53d1 ImportError \u5f02\u5e38\u3002<\/p>\n<p>\u5907\u6ce8<\/p>\n<p>\u5f53\u8fd9\u4e2a\u65b9\u6cd5\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u51fa\u4e8e\u6027\u80fd\u65b9\u9762\u7684\u8003\u8651\uff0c\u5982\u679c\u6709\u53ef\u80fd\u7684\u8bdd\uff0c\u5efa\u8bae\u8986\u76d6\u5b83\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u4e0d\u518d\u62bd\u8c61\u5e76\u4e14\u63d0\u4f9b\u4e00\u4e2a\u5177\u4f53\u7684\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p><em>abstractmethod<\/em> get_source(<em>fullname<\/em>)<\/p>\n<p>\u4e00\u4e2a\u8fd4\u56de\u6a21\u5757\u6e90\u7684\u62bd\u8c61\u65b9\u6cd5\u3002\u4f7f\u7528 universal newlines \u4f5c\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\u88ab\u8fd4\u56de\uff0c\u5c06\u6240\u6709\u53ef\u8bc6\u522b\u884c\u5206\u5272\u7b26\u7ffb\u8bd1\u6210 <code>'\\n'<\/code> \u5b57\u7b26\u3002 \u5982\u679c\u6ca1\u6709\u53ef\u7528\u7684\u6e90\uff08\u4f8b\u5982\uff0c\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff09\uff0c\u5219\u8fd4\u56de <code>None<\/code>\u3002 \u5982\u679c\u52a0\u8f7d\u5668\u4e0d\u80fd\u627e\u5230\u6307\u5b9a\u7684\u6a21\u5757\uff0c\u5219\u5f15\u53d1 ImportError \u5f02\u5e38\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f15\u53d1 ImportError \u800c\u4e0d\u662f NotImplementedError\u3002<\/p>\n<\/li>\n<li>\n<p>is_package(<em>fullname<\/em>)<\/p>\n<p>\u53ef\u9009\u65b9\u6cd5\uff0c\u5982\u679c\u6a21\u5757\u4e3a\u5305\uff0c\u5219\u8fd4\u56de True\uff0c\u5426\u5219\u8fd4\u56de False\u3002 \u5982\u679c loader \u627e\u4e0d\u5230\u6a21\u5757\uff0c\u5219\u4f1a\u89e6\u53d1 ImportError\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f15\u53d1 ImportError \u800c\u4e0d\u662f NotImplementedError\u3002<\/p>\n<\/li>\n<li>\n<p><em>static<\/em> source_to_code(<em>data<\/em>, <em>path=\u2019&lt;string&gt;\u2019<\/em>)<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u6765\u81eaPython\u6e90\u7801\u7684\u4ee3\u7801\u5bf9\u8c61\u3002<\/p>\n<p>\u53c2\u6570 <em>data<\/em> \u53ef\u4ee5\u662f\u4efb\u610f compile() \u51fd\u6570\u652f\u6301\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u5b57\u7b26\u4e32\u6216\u5b57\u8282\u4e32\uff09\u3002 \u53c2\u6570 <em>path<\/em> \u5e94\u8be5\u662f\u6e90\u4ee3\u7801\u6765\u6e90\u7684\u8def\u5f84\uff0c\u8fd9\u53ef\u80fd\u662f\u4e00\u4e2a\u62bd\u8c61\u6982\u5ff5\uff08\u4f8b\u5982\u4f4d\u4e8e\u4e00\u4e2a zip \u6587\u4ef6\u4e2d\uff09\u3002<\/p>\n<p>\u5728\u6709\u540e\u7eed\u4ee3\u7801\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u901a\u8fc7\u8fd0\u884c``exec(code, module.__dict__)``\u6765\u6267\u884c\u5b83\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.5 \u7248\u66f4\u6539: \u4f7f\u5f97\u8fd9\u4e2a\u65b9\u6cd5\u53d8\u6210\u9759\u6001\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>exec_module(<em>module<\/em>)<\/p>\n<p>Loader.exec_module() \u7684\u5b9e\u73b0\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>load_module(<em>fullname<\/em>)<\/p>\n<p>Loader.load_module() \u7684\u5b9e\u73b0\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 exec_module() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.ExecutionLoader<\/p>\n<p>\u4e00\u4e2a\u7ee7\u627f\u81ea InspectLoader \u7684\u62bd\u8c61\u57fa\u7c7b\uff0c\u5f53\u88ab\u5b9e\u73b0\u65f6\uff0c\u5e2e\u52a9\u4e00\u4e2a\u6a21\u5757\u4f5c\u4e3a\u811a\u672c\u6765\u6267\u884c\u3002 \u8fd9\u4e2a\u62bd\u8c61\u57fa\u7c7b\u8868\u793a\u53ef\u9009\u7684 PEP 302 \u534f\u8bae\u3002<\/p>\n<ul>\n<li>\n<p><em>abstractmethod<\/em> get_filename(<em>fullname<\/em>)<\/p>\n<p>\u4e00\u4e2a\u7528\u6765\u4e3a\u6307\u5b9a\u6a21\u5757\u8fd4\u56de __file__ \u7684\u503c\u7684\u62bd\u8c61\u65b9\u6cd5\u3002\u5982\u679c\u65e0\u8def\u5f84\u53ef\u7528\uff0c\u5219\u5f15\u53d1 ImportError\u3002<\/p>\n<p>\u5982\u679c\u6e90\u4ee3\u7801\u53ef\u7528\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u6e90\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u4e0d\u7ba1\u662f\u5426\u662f\u7528\u6765\u52a0\u8f7d\u6a21\u5757\u7684\u5b57\u8282\u7801\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f15\u53d1 ImportError \u800c\u4e0d\u662f NotImplementedError\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.FileLoader(<em>fullname<\/em>, <em>path<\/em>)<\/p>\n<p>\u4e00\u4e2a\u7ee7\u627f\u81ea ResourceLoader \u548c ExecutionLoader\uff0c\u63d0\u4f9b ResourceLoader.get_data() \u548c ExecutionLoader.get_filename() \u5177\u4f53\u5b9e\u73b0\u7684\u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<p>\u53c2\u6570 <em>fullname<\/em> \u662f\u52a0\u8f7d\u5668\u8981\u5904\u7406\u7684\u6a21\u5757\u7684\u5b8c\u5168\u89e3\u6790\u7684\u540d\u5b57\u3002\u53c2\u6570 <em>path<\/em> \u662f\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>\n<p>name<\/p>\n<p>\u52a0\u8f7d\u5668\u53ef\u4ee5\u5904\u7406\u7684\u6a21\u5757\u7684\u540d\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p>path<\/p>\n<p>\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<\/li>\n<li>\n<p>load_module(<em>fullname<\/em>)<\/p>\n<p>\u8c03\u7528super\u7684``load_module()``\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 Loader.exec_module() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p><em>abstractmethod<\/em> get_filename(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de path\u3002<\/p>\n<\/li>\n<li>\n<p><em>abstractmethod<\/em> get_data(<em>path<\/em>)<\/p>\n<p>\u8bfb\u53d6 <em>path<\/em> \u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6\u5e76\u4e14\u8fd4\u56de\u6765\u81ea\u5b83\u7684\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.abc.SourceLoader<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0\u6e90\u6587\u4ef6\uff08\u548c\u53ef\u9009\u5730\u5b57\u8282\u7801\uff09\u52a0\u8f7d\u7684\u62bd\u8c61\u57fa\u7c7b\u3002\u8fd9\u4e2a\u7c7b\u7ee7\u627f\u81ea ResourceLoader \u548c ExecutionLoader\uff0c\u9700\u8981\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li>ResourceLoader.get_data()<\/li>\n<\/ul>\n<ul>\n<li>\n<p>ExecutionLoader.get_filename()<\/p>\n<p>\u5e94\u8be5\u662f\u53ea\u8fd4\u56de\u6e90\u6587\u4ef6\u7684\u8def\u5f84\uff1b\u4e0d\u652f\u6301\u65e0\u6e90\u52a0\u8f7d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7531\u8fd9\u4e2a\u7c7b\u5b9a\u4e49\u7684\u62bd\u8c61\u65b9\u6cd5\u7528\u6765\u6dfb\u52a0\u53ef\u9009\u7684\u5b57\u8282\u7801\u6587\u4ef6\u652f\u6301\u3002\u4e0d\u5b9e\u73b0\u8fd9\u4e9b\u53ef\u9009\u7684\u65b9\u6cd5\uff08\u6216\u5bfc\u81f4\u5b83\u4eec\u5f15\u53d1 NotImplementedError \u5f02\u5e38\uff09\u5bfc\u81f4\u8fd9\u4e2a\u52a0\u8f7d\u5668\u53ea\u80fd\u4e0e\u6e90\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\u3002 \u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u52a0\u8f7d\u5668\u80fd\u4e0e\u6e90 <em>\u548c<\/em> \u5b57\u8282\u7801\u6587\u4ef6\u4e00\u8d77\u5de5\u4f5c\u3002\u4e0d\u5141\u8bb8\u53ea\u63d0\u4f9b\u5b57\u8282\u7801\u7684 <em>\u65e0\u6e90\u5f0f<\/em> \u52a0\u8f7d\u3002\u5b57\u8282\u7801\u6587\u4ef6\u662f\u901a\u8fc7\u79fb\u9664 Python \u7f16\u8bd1\u5668\u7684\u89e3\u6790\u6b65\u9aa4\u6765\u52a0\u901f\u52a0\u8f7d\u7684\u4f18\u5316\uff0c\u5e76\u4e14\u56e0\u6b64\u6ca1\u6709\u5f00\u653e\u51fa\u5b57\u8282\u7801\u4e13\u7528\u7684 API\u3002<\/p>\n<ul>\n<li>\n<p>path_stats(<em>path<\/em>)<\/p>\n<p>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5173\u4e8e\u6307\u5b9a\u8def\u5f84\u7684\u5143\u6570\u636e\u7684 dict \u7684\u53ef\u9009\u7684\u62bd\u8c61\u65b9\u6cd5\u3002 \u652f\u6301\u7684\u5b57\u5178\u952e\u6709\uff1a<\/p>\n<ul>\n<li>\n<p><code>'mtime'<\/code> (\u5fc5\u9009\u9879): \u4e00\u4e2a\u8868\u793a\u6e90\u7801\u4fee\u6539\u65f6\u95f4\u7684\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff1b<\/p>\n<\/li>\n<li>\n<p><code>'size'<\/code> (\u53ef\u9009\u9879)\uff1a\u6e90\u7801\u7684\u5b57\u8282\u5927\u5c0f\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>\u5b57\u5178\u4e2d\u4efb\u4f55\u5176\u4ed6\u952e\u4f1a\u88ab\u5ffd\u7565\uff0c\u4ee5\u5141\u8bb8\u5c06\u6765\u7684\u6269\u5c55\u3002<\/span><span> <\/span><span>\u5982\u679c\u4e0d\u80fd\u5904\u7406\u8be5\u8def\u5f84\uff0c\u5219\u4f1a\u5f15\u53d1<\/span><span> <\/span><span>[<\/span><span>OSError<\/span><span>](<\/span><span>$e69e6dad35dc3610<\/span><span>.<\/span><span>md<\/span><span>#OSError \"OSError\")\u3002<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>3.3<\/span><span> <\/span><span>\u65b0\u7248\u529f\u80fd.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>\u5728<\/span><span> <\/span><span>3.4<\/span><span> <\/span><span>\u7248\u66f4\u6539:<\/span><span> <\/span><span>\u5f15\u53d1<\/span><span> <\/span><span>[<\/span><span>OSError<\/span><span>](<\/span><span>$e69e6dad35dc3610<\/span><span>.<\/span><span>md<\/span><span>#OSError \"OSError\") \u800c\u4e0d\u662f [NotImplemented]($7b977d4506b0e99c.md#NotImplemented \"NotImplemented\")\u3002<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<ul>\n<li>\n<p>path_mtime(<em>path<\/em>)<\/p>\n<p>\u8fd4\u56de\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u4fee\u6539\u65f6\u95f4\u7684\u53ef\u9009\u7684\u62bd\u8c61\u65b9\u6cd5\u3002<\/p>\n<p>3.3 \u7248\u540e\u5df2\u79fb\u9664: \u5728\u6709\u4e86 path_stats() \u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u5f03\u7528\u4e86\u3002 \u6ca1\u5fc5\u8981\u53bb\u5b9e\u73b0\u5b83\u4e86\uff0c\u4f46\u662f\u4e3a\u4e86\u517c\u5bb9\u6027\uff0c\u5b83\u4f9d\u7136\u5904\u4e8e\u53ef\u7528\u72b6\u6001\u3002 \u5982\u679c\u6587\u4ef6\u8def\u5f84\u4e0d\u80fd\u88ab\u5904\u7406\uff0c\u5219\u5f15\u53d1 OSError \u5f02\u5e38\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f15\u53d1 OSError \u800c\u4e0d\u662f NotImplemented\u3002<\/p>\n<\/li>\n<li>\n<p>set_data(<em>path<\/em>, <em>data<\/em>)<\/p>\n<p>\u5f80\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5199\u5165\u6307\u5b9a\u5b57\u8282\u7684\u7684\u53ef\u9009\u7684\u62bd\u8c61\u65b9\u6cd5\u3002\u4efb\u4f55\u4e2d\u95f4\u4e0d\u5b58\u5728\u7684\u76ee\u5f55\u4e0d\u4f1a\u88ab\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<p>\u7531\u4e8e\u8def\u5f84\u662f\u53ea\u8bfb\u7684\uff0c\u5f53\u5199\u5165\u7684\u8def\u5f84\u4ea7\u751f\u9519\u8bef\u65f6\uff08errno.EACCES\/PermissionError\uff09\uff0c\u4e0d\u4f1a\u4f20\u64ad\u5f02\u5e38\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5f53\u88ab\u8c03\u7528\u65f6\uff0c\u4e0d\u518d\u5f15\u8d77 NotImplementedError \u5f02\u5e38\u3002<\/p>\n<\/li>\n<li>\n<p>get_code(<em>fullname<\/em>)<\/p>\n<p>InspectLoader.get_code() \u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>exec_module(<em>module<\/em>)<\/p>\n<p>Loader.exec_module() \u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>load_module(<em>fullname<\/em>)<\/p>\n<p>Concrete implementation of Loader.load_module().<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 exec_module() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p>get_source(<em>fullname<\/em>)<\/p>\n<p>InspectLoader.get_source() \u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>is_package(<em>fullname<\/em>)<\/p>\n<p>InspectLoader.is_package() \u7684\u5177\u4f53\u5b9e\u73b0\u3002\u4e00\u4e2a\u6a21\u5757\u88ab\u786e\u5b9a\u4e3a\u4e00\u4e2a\u5305\u7684\u6761\u4ef6\u662f\uff1a\u5b83\u7684\u6587\u4ef6\u8def\u5f84\uff08\u7531 ExecutionLoader.get_filename() \u63d0\u4f9b\uff09\u5f53\u6587\u4ef6\u6269\u5c55\u540d\u88ab\u79fb\u9664\u65f6\u662f\u4e00\u4e2a\u547d\u540d\u4e3a <code>__init__<\/code> \u7684\u6587\u4ef6\uff0c<strong>\u5e76\u4e14<\/strong> \u8fd9\u4e2a\u6a21\u5757\u540d\u5b57\u672c\u8eab\u4e0d\u662f\u4ee5``__init__``\u7ed3\u675f\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>importlib.machinery \u2014\u2014 \u5bfc\u5165\u5668\u548c\u8def\u5f84\u94a9\u5b50\u51fd\u6570\u3002<\/h2>\n<p><strong>\u6e90\u4ee3\u7801\uff1a<\/strong> Lib\/importlib\/machinery.py<\/p>\n<hr>\n<p>\u672c\u6a21\u5757\u5305\u542b\u591a\u4e2a\u5bf9\u8c61\uff0c\u4ee5\u5e2e\u52a9 import \u67e5\u627e\u5e76\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<p>importlib.machinery.SOURCE_SUFFIXES<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u8868\u793a\u6e90\u6a21\u5757\u7684\u53ef\u8bc6\u522b\u7684\u6587\u4ef6\u540e\u7f00\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>importlib.machinery.DEBUG_BYTECODE_SUFFIXES<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u8868\u793a\u672a\u7ecf\u4f18\u5316\u5b57\u8282\u7801\u6a21\u5757\u7684\u6587\u4ef6\u540e\u7f00\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>3.5 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u7528 BYTECODE_SUFFIXES \u3002<\/p>\n<p>importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u8868\u793a\u5df2\u4f18\u5316\u5b57\u8282\u7801\u6a21\u5757\u7684\u6587\u4ef6\u540e\u7f00\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>3.5 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u7528 BYTECODE_SUFFIXES \u3002<\/p>\n<p>importlib.machinery.BYTECODE_SUFFIXES<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u8868\u793a\u5b57\u8282\u7801\u6a21\u5757\u7684\u53ef\u8bc6\u522b\u7684\u6587\u4ef6\u540e\u7f00\uff08\u5305\u542b\u524d\u5bfc\u7684\u53e5\u70b9\u7b26\u53f7\uff09\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.5 \u7248\u66f4\u6539: \u8be5\u503c\u4e0d\u518d\u4f9d\u8d56\u4e8e <code>__debug__<\/code> \u3002<\/p>\n<p>importlib.machinery.EXTENSION_SUFFIXES<\/p>\n<p>\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u8868\u793a\u6269\u5c55\u6a21\u5757\u7684\u53ef\u8bc6\u522b\u7684\u6587\u4ef6\u540e\u7f00\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>importlib.machinery.all_suffixes()<\/p>\n<p>\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u7ec4\u5408\u5217\u8868\uff0c\u4ee3\u8868\u6807\u51c6\u5bfc\u5165\u673a\u5236\u53ef\u8bc6\u522b\u6a21\u5757\u7684\u6240\u6709\u6587\u4ef6\u540e\u7f00\u3002\u8fd9\u662f\u4e2a\u52a9\u624b\u51fd\u6570\uff0c\u53ea\u9700\u77e5\u9053\u67d0\u4e2a\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u662f\u5426\u4f1a\u6307\u5411\u6a21\u5757\uff0c\u800c\u4e0d\u9700\u8981\u4efb\u4f55\u5173\u4e8e\u6a21\u5757\u79cd\u7c7b\u7684\u7ec6\u8282\uff08\u4f8b\u5982 inspect.getmodulename()\uff09\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p><em>class<\/em> importlib.machinery.BuiltinImporter<\/p>\n<p>\u7528\u4e8e\u5bfc\u5165\u5185\u7f6e\u6a21\u5757\u7684 importer\u3002 \u6240\u6709\u5df2\u77e5\u7684\u5185\u7f6e\u6a21\u5757\u90fd\u5df2\u5217\u5165 sys.builtin_module_names\u3002 \u6b64\u7c7b\u5b9e\u73b0\u4e86 importlib.abc.MetaPathFinder \u548c importlib.abc.InspectLoader \u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<p>\u6b64\u7c7b\u53ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u8f7b\u5b9e\u4f8b\u5316\u7684\u5f00\u9500\u3002<\/p>\n<p>\u5728 3.5 \u7248\u66f4\u6539: \u4f5c\u4e3a PEP 489 \u7684\u4e00\u90e8\u5206\uff0c\u73b0\u5728\u5185\u7f6e\u6a21\u5757\u5bfc\u5165\u5668\u5b9e\u73b0\u4e86 <code>Loader.create_module()<\/code> \u548c <code>Loader.exec_module()<\/code>\u3002<\/p>\n<p><em>class<\/em> importlib.machinery.FrozenImporter<\/p>\n<p>\u7528\u4e8e\u5df2\u51bb\u7ed3\u6a21\u5757\u7684 importer\u3002 \u6b64\u7c7b\u5b9e\u73b0\u4e86 importlib.abc.MetaPathFinder \u548c importlib.abc.InspectLoader \u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<p>\u6b64\u7c7b\u53ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u8f7b\u5b9e\u4f8b\u5316\u7684\u5f00\u9500\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u6709\u4e86 <code>create_module()<\/code> \u548c <code>exec_module()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><em>class<\/em> importlib.machinery.WindowsRegistryFinder<\/p>\n<p>Finder \u7528\u4e8e\u67e5\u627e\u5728 Windows \u6ce8\u518c\u8868\u4e2d\u58f0\u660e\u7684\u6a21\u5757\u3002\u8be5\u7c7b\u5b9e\u73b0\u4e86\u57fa\u7840\u7684 importlib.abc.MetaPathFinder \u3002<\/p>\n<p>\u6b64\u7c7b\u53ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u8f7b\u5b9e\u4f8b\u5316\u7684\u5f00\u9500\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>3.6 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u7528 site \u914d\u7f6e\u3002\u672a\u6765\u7248\u672c\u7684 Python \u53ef\u80fd\u4e0d\u4f1a\u9ed8\u8ba4\u542f\u7528\u8be5\u67e5\u627e\u5668\u3002<\/p>\n<p><em>class<\/em> importlib.machinery.PathFinder<\/p>\n<p>\u7528\u4e8e sys.path \u548c\u5305\u7684 <code>__path__<\/code> \u5c5e\u6027\u7684 Finder \u3002\u8be5\u7c7b\u5b9e\u73b0\u4e86\u57fa\u7840\u7684 importlib.abc.MetaPathFinder\u3002<\/p>\n<p>\u6b64\u7c7b\u53ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u8f7b\u5b9e\u4f8b\u5316\u7684\u5f00\u9500\u3002<\/p>\n<ul>\n<li>\n<p><em>classmethod<\/em> find_spec(<em>fullname<\/em>, <em>path=None<\/em>, <em>target=None<\/em>)<\/p>\n<p>\u7c7b\u65b9\u6cd5\u8bd5\u56fe\u5728 sys.path \u6216 <em>path<\/em> \u4e0a\u4e3a <em>fullname<\/em> \u6307\u5b9a\u7684\u6a21\u5757\u67e5\u627e spec\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u8def\u5f84\u6761\u76ee\uff0c\u90fd\u4f1a\u67e5\u770b sys.path_importer_cache \u3002\u5982\u679c\u627e\u5230\u975e False \u7684\u5bf9\u8c61\uff0c\u5219\u5c06\u5176\u7528\u4f5c path entry finder \u6765\u67e5\u627e\u8981\u641c\u7d22\u7684\u6a21\u5757\u3002\u5982\u679c\u5728 sys.path_importer_cache \u4e2d\u6ca1\u6709\u627e\u5230\u6761\u76ee\uff0c\u90a3\u4f1a\u5728 sys.path_hooks \u68c0\u7d22\u8be5\u8def\u5f84\u6761\u76ee\u7684\u67e5\u627e\u5668\uff0c\u627e\u5230\u4e86\u5219\u548c\u67e5\u5230\u7684\u6a21\u5757\u4fe1\u606f\u4e00\u8d77\u5b58\u5165 sys.path_importer_cache \u3002\u5982\u679c\u67e5\u627e\u5668\u6ca1\u6709\u627e\u5230\uff0c\u5219\u7f13\u5b58\u4e2d\u7684\u67e5\u627e\u5668\u548c\u6a21\u5757\u4fe1\u606f\u90fd\u5b58\u4e3a <code>None<\/code> \uff0c\u7136\u540e\u8fd4\u56de\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.5 \u7248\u66f4\u6539: \u5982\u679c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0d\u518d\u6709\u6548\uff08\u7528\u7a7a\u5b57\u7b26\u4e32\u8868\u793a\uff09\uff0c\u5219\u8fd4\u56de <code>None<\/code>\uff0c\u4f46\u5728 sys.path_importer_cache \u4e2d\u4e0d\u4f1a\u6709\u7f13\u5b58\u503c\u3002<\/p>\n<\/li>\n<li>\n<p><em>classmethod<\/em> find_module(<em>fullname<\/em>, <em>path=None<\/em>)<\/p>\n<p>\u4e00\u4e2a\u8fc7\u65f6\u7684 find_spec() \u5c01\u88c5\u5bf9\u8c61\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p><em>classmethod<\/em> invalidate_caches()<\/p>\n<p>\u4e3a\u6240\u6709\u5b58\u4e8e sys.path_importer_cache \u4e2d\u7684\u67e5\u627e\u5668\uff0c\u8c03\u7528\u5176 importlib.abc.PathEntryFinder.invalidate_caches() \u65b9\u6cd5\u3002 sys.path_importer_cache \u4e2d\u4e3a <code>None<\/code> \u7684\u6761\u76ee\u5c06\u88ab\u5220\u9664\u3002<\/p>\n<p>\u5728 3.7 \u7248\u66f4\u6539: sys.path_importer_cache \u4e2d\u4e3a <code>None<\/code> \u7684\u6761\u76ee\u5c06\u88ab\u5220\u9664\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u8c03\u7528 sys.path_hooks \u4e2d\u7684\u5bf9\u8c61\uff0c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e3a <code>''<\/code> (\u5373\u7a7a\u5b57\u7b26\u4e32)\u3002<\/p>\n<p><em>class<\/em> importlib.machinery.FileFinder(<em>path<\/em>, <em>\\<\/em>loader_details*)<\/p>\n<p>importlib.abc.PathEntryFinder \u7684\u4e00\u4e2a\u5177\u4f53\u5b9e\u73b0\uff0c\u5b83\u4f1a\u7f13\u5b58\u6765\u81ea\u6587\u4ef6\u7cfb\u7edf\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u53c2\u6570 <em>path<\/em> \u662f\u67e5\u627e\u5668\u8d1f\u8d23\u641c\u7d22\u7684\u76ee\u5f55\u3002<\/p>\n<p><em>loader_details<\/em> \u53c2\u6570\u662f\u6570\u91cf\u4e0d\u5b9a\u7684\u4e8c\u5143\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7ec4\u5305\u542b\u52a0\u8f7d\u5668\u53ca\u5176\u53ef\u8bc6\u522b\u7684\u6587\u4ef6\u540e\u7f00\u5217\u8868\u3002\u52a0\u8f7d\u5668\u5e94\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u53ef\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5373\u6a21\u5757\u7684\u540d\u79f0\u548c\u5df2\u627e\u5230\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<p>\u67e5\u627e\u5668\u5c06\u6309\u9700\u5bf9\u76ee\u5f55\u5185\u5bb9\u8fdb\u884c\u7f13\u5b58\uff0c\u901a\u8fc7\u5bf9\u6bcf\u4e2a\u6a21\u5757\u7684\u68c0\u7d22\u8fdb\u884c\u72b6\u6001\u7edf\u8ba1\uff0c\u9a8c\u8bc1\u7f13\u5b58\u662f\u5426\u8fc7\u671f\u3002\u56e0\u4e3a\u7f13\u5b58\u7684\u6ede\u540e\u6027\u4f9d\u8d56\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u6587\u4ef6\u7cfb\u7edf\u72b6\u6001\u4fe1\u606f\u7684\u7c92\u5ea6\uff0c\u6240\u4ee5\u641c\u7d22\u6a21\u5757\u3001\u65b0\u5efa\u6587\u4ef6\u3001\u7136\u540e\u641c\u7d22\u65b0\u6587\u4ef6\u4ee3\u8868\u7684\u6a21\u5757\uff0c\u8fd9\u4f1a\u5b58\u5728\u7ade\u4e89\u72b6\u6001\u3002\u5982\u679c\u8fd9\u4e9b\u64cd\u4f5c\u7684\u9891\u7387\u592a\u5feb\uff0c\u751a\u81f3\u5c0f\u4e8e\u72b6\u6001\u7edf\u8ba1\u7684\u7c92\u5ea6\uff0c\u90a3\u4e48\u6a21\u5757\u641c\u7d22\u5c06\u4f1a\u5931\u8d25\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u5728\u52a8\u6001\u521b\u5efa\u6a21\u5757\u65f6\uff0c\u8bf7\u786e\u4fdd\u8c03\u7528 importlib.invalidate_caches()\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>\n<p>path<\/p>\n<p>\u67e5\u627e\u5668\u5c06\u8981\u641c\u7d22\u7684\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>find_spec(<em>fullname<\/em>, <em>target=None<\/em>)<\/p>\n<p>\u5c1d\u8bd5\u5728 path \u4e2d\u627e\u5230\u5904\u7406 <em>fullname<\/em> \u7684\u89c4\u683c\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>find_loader(<em>fullname<\/em>)<\/p>\n<p>\u8bd5\u56fe\u5728 path \u5185\u627e\u5230\u5904\u7406 <em>fullname<\/em> \u7684\u52a0\u8f7d\u5668\u3002<\/p>\n<p>3.10 \u7248\u540e\u5df2\u79fb\u9664: \u4f7f\u7528 find_spec() \u6765\u4ee3\u66ff\u3002<\/p>\n<\/li>\n<li>\n<p>invalidate_caches()<\/p>\n<p>\u6e05\u7406\u5185\u90e8\u7f13\u5b58\u3002<\/p>\n<\/li>\n<li>\n<p><em>classmethod<\/em> path_hook(<em>\\<\/em>loader_details*)<\/p>\n<p>\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u8fd4\u56de\u4f9b sys.path_hooks \u4f7f\u7528\u7684\u95ed\u5305\u3002\u6839\u636e\u76f4\u63a5\u7ed9\u51fa\u7684\u8def\u5f84\u53c2\u6570\u548c\u95f4\u63a5\u7ed9\u51fa\u7684 <em>loader_details<\/em>\uff0c\u95ed\u5305\u4f1a\u8fd4\u56de\u4e00\u4e2a FileFinder \u7684\u5b9e\u4f8b\u3002<\/p>\n<p>\u5982\u679c\u7ed9\u95ed\u5305\u7684\u53c2\u6570\u4e0d\u662f\u5df2\u5b58\u5728\u7684\u76ee\u5f55\uff0c\u5c06\u4f1a\u89e6\u53d1 ImportError\u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.machinery.SourceFileLoader(<em>fullname<\/em>, <em>path<\/em>)<\/p>\n<p>importlib.abc.SourceLoader \u7684\u4e00\u4e2a\u5177\u4f53\u5b9e\u73b0\uff0c\u8be5\u5b9e\u73b0\u5b50\u7c7b\u5316\u4e86 importlib.abc.FileLoader \u5e76\u63d0\u4f9b\u4e86\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>\n<p>name<\/p>\n<p>\u8be5\u52a0\u8f7d\u5668\u5c06\u8981\u5904\u7406\u7684\u6a21\u5757\u540d\u79f0\u3002<\/p>\n<\/li>\n<li>\n<p>path<\/p>\n<p>\u6e90\u6587\u4ef6\u7684\u8def\u5f84<\/p>\n<\/li>\n<li>\n<p>is_package(<em>fullname<\/em>)<\/p>\n<p>\u5982\u679c path \u770b\u4f3c\u5305\u7684\u8def\u5f84\uff0c\u5219\u8fd4\u56de <code>True<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>path_stats(<em>path<\/em>)<\/p>\n<p>importlib.abc.SourceLoader.path_stats() \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>set_data(<em>path<\/em>, <em>data<\/em>)<\/p>\n<p>importlib.abc.SourceLoader.set_data() \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>load_module(<em>name=None<\/em>)<\/p>\n<p>importlib.abc.Loader.load_module() \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff0c\u8fd9\u91cc\u8981\u52a0\u8f7d\u7684\u6a21\u5757\u540d\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>3.6 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u7528 importlib.abc.Loader.exec_module() \u3002<\/p>\n<\/li>\n<\/ul>\n<p><em>class<\/em> importlib.machinery.SourcelessFileLoader(<em>fullname<\/em>, <em>path<\/em>)<\/p>\n<p>importlib.abc.FileLoader \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff0c\u53ef\u5bfc\u5165\u5b57\u8282\u7801\u6587\u4ef6\uff08\u4e5f\u5373\u6e90\u4ee3\u7801\u6587\u4ef6\u4e0d\u5b58\u5728\uff09\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u76f4\u63a5\u7528\u5b57\u8282\u7801\u6587\u4ef6\uff08\u800c\u4e0d\u662f\u6e90\u4ee3\u7801\u6587\u4ef6\uff09\uff0c\u4f1a\u8ba9\u6a21\u5757\u65e0\u6cd5\u5e94\u7528\u4e8e\u6240\u6709\u7684 Python \u7248\u672c\u6216\u5b57\u8282\u7801\u683c\u5f0f\u6709\u6240\u6539\u52a8\u7684\u65b0\u7248\u672c Python\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>\n<p>name<\/p>\n<p>\u52a0\u8f7d\u5668\u5c06\u8981\u5904\u7406\u7684\u6a21\u5757\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>path<\/p>\n<p>\u4e8c\u8fdb\u5236\u7801\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>is_package(<em>fullname<\/em>)<\/p>\n<p>\u6839\u636e path \u786e\u5b9a\u8be5\u6a21\u5757\u662f\u5426\u4e3a\u5305\u3002<\/p>\n<\/li>\n<li>\n<p>get_code(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de\u7531 path \u521b\u5efa\u7684 name \u7684\u4ee3\u7801\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>get_source(<em>fullname<\/em>)<\/p>\n<p>\u56e0\u4e3a\u7528\u6b64\u52a0\u8f7d\u5668\u65f6\u5b57\u8282\u7801\u6587\u4ef6\u6ca1\u6709\u6e90\u7801\u6587\u4ef6\uff0c\u6240\u4ee5\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>load_module(<em>name=None<\/em>)<\/p>\n<\/li>\n<\/ul>\n<p>importlib.abc.Loader.load_module() \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff0c\u8fd9\u91cc\u8981\u52a0\u8f7d\u7684\u6a21\u5757\u540d\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>3.6 \u7248\u540e\u5df2\u79fb\u9664: \u6539\u7528 importlib.abc.Loader.exec_module() \u3002<\/p>\n<p><em>class<\/em> importlib.machinery.ExtensionFileLoader(<em>fullname<\/em>, <em>path<\/em>)<\/p>\n<p>importlib.abc.ExecutionLoader \u7684\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff0c\u7528\u4e8e\u6269\u5c55\u6a21\u5757\u3002<\/p>\n<p>\u53c2\u6570 <em>fullname<\/em> \u6307\u5b9a\u4e86\u52a0\u8f7d\u5668\u8981\u652f\u6301\u7684\u6a21\u5757\u540d\u3002\u53c2\u6570 <em>path<\/em> \u662f\u6307\u5411\u6269\u5c55\u6a21\u5757\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>\n<p>name<\/p>\n<p>\u88c5\u8f7d\u5668\u652f\u6301\u7684\u6a21\u5757\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>path<\/p>\n<p>\u6269\u5c55\u6a21\u5757\u7684\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>create_module(<em>spec<\/em>)<\/p>\n<p>\u6839\u636e PEP 489 \uff0c\u7531\u7ed9\u5b9a\u89c4\u8303\u521b\u5efa\u6a21\u5757\u5bf9\u8c61\u3002<\/p>\n<p>3.5 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>exec_module(<em>module<\/em>)<\/p>\n<p>\u6839\u636e PEP 489\uff0c\u521d\u59cb\u5316\u7ed9\u5b9a\u7684\u6a21\u5757\u5bf9\u8c61\u3002<\/p>\n<p>3.5 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<li>\n<p>is_package(<em>fullname<\/em>)<\/p>\n<p>\u6839\u636e EXTENSION_SUFFIXES \uff0c\u5982\u679c\u6587\u4ef6\u8def\u5f84\u6307\u5411\u67d0\u4e2a\u5305\u7684 <code>__init__<\/code> \u6a21\u5757\uff0c\u5219\u8fd4\u56de <code>True<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>get_code(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de <code>None<\/code>\uff0c\u56e0\u4e3a\u6269\u5c55\u6a21\u5757\u7f3a\u5c11\u4ee3\u7801\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p>get_source(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de <code>None<\/code>\uff0c\u56e0\u4e3a\u6269\u5c55\u6a21\u5757\u6ca1\u6709\u6e90\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>get_filename(<em>fullname<\/em>)<\/p>\n<p>\u8fd4\u56de path\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<\/li>\n<\/ul>\n<p>NamespaceLoader(name, path, path_finder):<\/p>\n<p>A concrete implementation of importlib.abc.InspectLoader for namespace packages. This is an alias for a private class and is only made public for introspecting the <code>__loader__<\/code> attribute on namespace packages:<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>from<\/span><span> importlib<\/span><span>.<\/span><span>machinery <\/span><span>import<\/span><span> <\/span><span>NamespaceLoader<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>import<\/span><span> my_namespace<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> isinstance<\/span><span>(<\/span><span>my_namespace<\/span><span>.<\/span><span>__loader__<\/span><span>,<\/span><span> <\/span><span>NamespaceLoader<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>True<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>import<\/span><span> importlib<\/span><span>.<\/span><span>abc<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> isinstance<\/span><span>(<\/span><span>my_namespace<\/span><span>.<\/span><span>__loader__<\/span><span>,<\/span><span> importlib<\/span><span>.<\/span><span>abc<\/span><span>.<\/span><span>Loader<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>True<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<p>3.11 \u65b0\u7248\u529f\u80fd.<\/p>\n<p><em>class<\/em> importlib.machinery.ModuleSpec(<em>name<\/em>, <em>loader<\/em>, <em>**, <\/em>origin=None<em>, <\/em>loader_state=None<em>, <\/em>is_package=None*)<\/p>\n<p>A specification for a module\u2019s import-system-related state. This is typically exposed as the module\u2019s __spec__ attribute. In the descriptions below, the names in parentheses give the corresponding attribute available directly on the module object, e.g. <code>module.__spec__.origin == module.__file__<\/code>. Note, however, that while the <em>values<\/em> are usually equivalent, they can differ since there is no synchronization between the two objects. For example, it is possible to update the module\u2019s __file__ at runtime and this will not be automatically reflected in the module\u2019s <code>__spec__.origin<\/code>, and vice versa.<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<ul>\n<li>name<\/li>\n<\/ul>\n<p>(__name__)<\/p>\n<p>The module\u2019s fully qualified name. The finder should always set this attribute to a non-empty string.<\/p>\n<ul>\n<li>loader<\/li>\n<\/ul>\n<p>(__loader__)<\/p>\n<p>The loader used to load the module. The finder should always set this attribute.<\/p>\n<ul>\n<li>origin<\/li>\n<\/ul>\n<p>(__file__)<\/p>\n<p>The location the loader should use to load the module. For example, for modules loaded from a .py file this is the filename. The finder should always set this attribute to a meaningful value for the loader to use. In the uncommon case that there is not one (like for namespace packages), it should be set to <code>None<\/code>.<\/p>\n<ul>\n<li>submodule_search_locations<\/li>\n<\/ul>\n<p>(__path__)<\/p>\n<p>The list of locations where the package\u2019s submodules will be found. Most of the time this is a single directory. The finder should set this attribute to a list, even an empty one, to indicate to the import system that the module is a package. It should be set to <code>None<\/code> for non-package modules. It is set automatically later to a special object for namespace packages.<\/p>\n<ul>\n<li>loader_state<\/li>\n<\/ul>\n<p>The finder may set this attribute to an object containing additional, module-specific data to use when loading the module. Otherwise it should be set to <code>None<\/code>.<\/p>\n<ul>\n<li>cached<\/li>\n<\/ul>\n<p>(__cached__)<\/p>\n<p>The filename of a compiled version of the module\u2019s code. The finder should always set this attribute but it may be <code>None<\/code> for modules that do not need compiled code stored.<\/p>\n<ul>\n<li>parent<\/li>\n<\/ul>\n<p>(__package__)<\/p>\n<p>(Read-only) The fully qualified name of the package the module is in (or the empty string for a top-level module). If the module is a package then this is the same as name.<\/p>\n<ul>\n<li>\n<p>has_location<\/p>\n<\/li>\n<li>\n<p><code>True<\/code> if the spec\u2019s origin refers to a loadable location,<\/p>\n<p><code>False<\/code> otherwise. This value impacts how origin is interpreted and how the module\u2019s __file__ is populated.<\/p>\n<\/li>\n<\/ul>\n<h2>importlib.util \u2014\u2014 \u5bfc\u5165\u5668\u7684\u5de5\u5177\u7a0b\u5e8f\u4ee3\u7801<\/h2>\n<p><strong>\u6e90\u4ee3\u7801\uff1a<\/strong> Lib\/importlib\/util.py<\/p>\n<hr>\n<p>\u672c\u6a21\u5757\u5305\u542b\u4e86\u5e2e\u52a9\u6784\u5efa importer \u7684\u591a\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>importlib.util.MAGIC_NUMBER<\/p>\n<p>\u4ee3\u8868\u5b57\u8282\u7801\u7248\u672c\u53f7\u7684\u5b57\u8282\u4e32\u3002\u82e5\u8981\u6709\u52a9\u4e8e\u52a0\u8f7d\/\u5199\u5165\u5b57\u8282\u7801\uff0c\u53ef\u8003\u8651\u91c7\u7528 importlib.abc.SourceLoader\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>importlib.util.cache_from_source(<em>path<\/em>, <em>debug_override=None<\/em>, <em>**, <\/em>optimization=None*)<\/p>\n<p>\u8fd4\u56de PEP 3147\/PEP 488 \u5b9a\u4e49\u7684\uff0c\u4e0e\u6e90 <em>path<\/em> \u76f8\u5173\u8054\u7684\u5df2\u7f16\u8bd1\u5b57\u8282\u7801\u6587\u4ef6\u7684\u8def\u5f84\u3002 \u4f8b\u5982\uff0c\u5982\u679c <em>path<\/em> \u4e3a <code>\/foo\/bar\/baz.py<\/code> \u5219 Python 3.2 \u4e2d\u7684\u8fd4\u56de\u503c\u5c06\u662f <code>\/foo\/bar\/__pycache__\/baz.cpython-32.pyc<\/code>\u3002 \u5b57\u7b26\u4e32 <code>cpython-32<\/code> \u6765\u81ea\u4e8e\u5f53\u524d\u7684\u9b54\u6cd5\u6807\u7b7e (\u53c2\u89c1 <code>get_tag()<\/code>; \u5982\u679c <code>sys.implementation.cache_tag<\/code> \u672a\u5b9a\u4e49\u5219\u5c06\u4f1a\u5f15\u53d1 NotImplementedError)\u3002<\/p>\n<p>\u53c2\u6570 <em>optimization<\/em> \u7528\u4e8e\u6307\u5b9a\u5b57\u8282\u7801\u6587\u4ef6\u7684\u4f18\u5316\u7ea7\u522b\u3002\u7a7a\u5b57\u7b26\u4e32\u4ee3\u8868\u6ca1\u6709\u4f18\u5316\uff0c\u6240\u4ee5 <em>optimization<\/em> \u4e3a \u7684 <code>\/foo\/bar\/baz.py<\/code>\uff0c\u5c06\u4f1a\u5f97\u5230\u5b57\u8282\u7801\u8def\u5f84\u4e3a <code>\/foo\/bar\/__pycache__\/baz.cpython-32.pyc<\/code>\u3002<code>None<\/code> \u4f1a\u5bfc\u81f4\u91c7\u7528\u89e3\u91ca\u5668\u7684\u4f18\u5316\u3002\u4efb\u4f55\u5176\u4ed6\u5b57\u7b26\u4e32\u90fd\u4f1a\u88ab\u91c7\u7528\uff0c\u6240\u4ee5 <em>optimization<\/em> \u4e3a <code>''<\/code> \u7684 <code>\/foo\/bar\/baz.py<\/code> \u4f1a\u5bfc\u81f4\u5b57\u8282\u7801\u8def\u5f84\u4e3a <code>\/foo\/bar\/__pycache__\/baz.cpython-32.opt-2.pyc<\/code>\u3002<em>optimization<\/em> \u5b57\u7b26\u4e32\u53ea\u80fd\u662f\u5b57\u6bcd\u6570\u5b57\uff0c\u5426\u5219\u4f1a\u89e6\u53d1 ValueError\u3002<\/p>\n<p><em>debug_override<\/em> \u53c2\u6570\u5df2\u5e9f\u5f03\uff0c\u53ef\u7528\u4e8e\u8986\u76d6\u7cfb\u7edf\u7684 <code>__debug__<\/code> \u503c\u3002<code>True<\/code> \u503c\u76f8\u5f53\u4e8e\u5c06 <em>optimization<\/em> \u8bbe\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<code>False<\/code> \u5219\u76f8\u5f53\u4e8e*optimization* \u8bbe\u4e3a <code>1<\/code>\u3002\u5982\u679c <em>debug_override<\/em> \u548c <em>optimization<\/em> \u90fd\u4e0d\u4e3a <code>None<\/code>\uff0c\u5219\u4f1a\u89e6\u53d1 TypeError\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.5 \u7248\u66f4\u6539: \u589e\u52a0\u4e86 <em>optimization<\/em> \u53c2\u6570\uff0c\u5e9f\u5f03\u4e86 <em>debug_override<\/em> \u53c2\u6570\u3002<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: \u63a5\u53d7\u4e00\u4e2a path-like object\u3002<\/p>\n<p>importlib.util.source_from_cache(<em>path<\/em>)<\/p>\n<p>\u6839\u636e\u6307\u5411\u4e00\u4e2a PEP 3147 \u6587\u4ef6\u540d\u7684 <em>path<\/em>\uff0c\u8fd4\u56de\u76f8\u5173\u8054\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u8def\u5f84\u3002 \u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c <em>path<\/em> \u4e3a <code>\/foo\/bar\/__pycache__\/baz.cpython-32.pyc<\/code> \u5219\u8fd4\u56de\u7684\u8def\u5f84\u5c06\u662f <code>\/foo\/bar\/baz.py<\/code>\u3002 <em>path<\/em> \u4e0d\u9700\u8981\u5df2\u5b58\u5728\uff0c\u4f46\u5982\u679c\u5b83\u672a\u9075\u5faa PEP 3147 \u6216 PEP 488 \u7684\u683c\u5f0f\uff0c\u5219\u4f1a\u5f15\u53d1 ValueError\u3002 \u5982\u679c\u672a\u5b9a\u4e49 <code>sys.implementation.cache_tag<\/code>\uff0c\u5219\u4f1a\u5f15\u53d1 NotImplementedError\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: \u63a5\u53d7\u4e00\u4e2a path-like object\u3002<\/p>\n<p>importlib.util.decode_source(<em>source_bytes<\/em>)<\/p>\n<p>\u5bf9\u4ee3\u8868\u6e90\u4ee3\u7801\u7684\u5b57\u8282\u4e32\u8fdb\u884c\u89e3\u7801\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5e26\u6709\u901a\u7528\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u8fd4\u56de\uff08\u7b26\u5408 importlib.abc.InspectLoader.get_source() \u8981\u6c42\uff09\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>importlib.util.resolve_name(<em>name<\/em>, <em>package<\/em>)<\/p>\n<p>\u5c06\u6a21\u5757\u7684\u76f8\u5bf9\u540d\u79f0\u89e3\u6790\u4e3a\u7edd\u5bf9\u540d\u79f0\u3002<\/p>\n<p>\u5982\u679c <strong>name<\/strong> \u524d\u9762\u6ca1\u6709\u53e5\u70b9\uff0c\u90a3\u5c31\u7b80\u5355\u5730\u8fd4\u56de <strong>name<\/strong>\u3002\u8fd9\u6837\u5c31\u80fd\u91c7\u7528``importlib.util.resolve_name(\u2018sys\u2019, __spec__.parent)`` \u4e4b\u7c7b\u7684\u5199\u6cd5\uff0c\u800c\u65e0\u9700\u68c0\u67e5\u662f\u5426\u9700\u8981 <strong>package<\/strong> \u53c2\u6570\u3002<\/p>\n<p>ImportError is raised if <strong>name<\/strong> is a relative module name but <strong>package<\/strong> is a false value (e.g. <code>None<\/code> or the empty string). ImportError is also raised if a relative name would escape its containing package (e.g. requesting <code>..bacon<\/code> from within the <code>spam<\/code> package).<\/p>\n<p>3.3 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.9 \u7248\u66f4\u6539: \u4e3a\u4e86\u6539\u5584\u4e0e import \u8bed\u53e5\u7684\u4e00\u81f4\u6027\uff0c\u5bf9\u4e8e\u65e0\u6548\u7684\u76f8\u5bf9\u5bfc\u5165\u5c1d\u8bd5\u4f1a\u5f15\u53d1 ImportError \u800c\u4e0d\u662f ValueError\u3002<\/p>\n<p>importlib.util.find_spec(<em>name<\/em>, <em>package=None<\/em>)<\/p>\n<p>\u67e5\u627e\u6a21\u5757\u7684 spec\uff0c\u76f8\u5bf9\u6307\u5b9a\u7684 <strong>package<\/strong> \u540d\u4e3a\u53ef\u9009\u53c2\u6570\u3002\u5982\u679c\u8be5\u6a21\u5757\u4f4d\u4e8e sys.modules \u4e2d\uff0c\u5219\u4f1a\u8fd4\u56de <code>sys.modules[name].__spec__<\/code> \uff08\u9664\u975e spec\u4e3a <code>None<\/code> \u6216\u672a\u4f5c\u8bbe\u7f6e\uff0c\u8fd9\u65f6\u4f1a\u89e6\u53d1 ValueError)\u3002\u5426\u5219\u5c06\u7528 sys.meta_path \u8fdb\u884c\u641c\u7d22\u3002\u82e5\u627e\u4e0d\u5230\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<p>\u5982\u679c <strong>name<\/strong> \u4e3a\u4e00\u4e2a\u5b50\u6a21\u5757\uff08\u5e26\u6709\u4e00\u4e2a\u53e5\u70b9\uff09\uff0c\u5219\u4f1a\u81ea\u52a8\u5bfc\u5165\u7236\u7ea7\u6a21\u5757\u3002<\/p>\n<p><strong>name<\/strong> \u548c <strong>package<\/strong> \u7684\u7528\u6cd5\u4e0e <code>import_module()<\/code> \u76f8\u540c\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.7 \u7248\u66f4\u6539: \u5982\u679c <strong>package<\/strong> \u5b9e\u9645\u4e0a\u4e0d\u662f\u4e00\u4e2a\u5305\uff08\u5373\u7f3a\u5c11 __path__ \u5c5e\u6027\uff09\u5219\u4f1a\u5f15\u53d1 ModuleNotFoundError \u800c\u4e0d\u662f AttributeError\u3002<\/p>\n<p>importlib.util.module_from_spec(<em>spec<\/em>)<\/p>\n<p>\u57fa\u4e8e <strong>spec<\/strong> \u548c spec.loader.create_module \u521b\u5efa\u4e00\u4e2a\u65b0\u6a21\u5757\u3002<\/p>\n<p>\u5982\u679c spec.loader.create_module \u672a\u8fd4\u56de <code>None<\/code>\uff0c\u90a3\u4e48\u5148\u524d\u5df2\u5b58\u5728\u7684\u5c5e\u6027\u4e0d\u4f1a\u88ab\u91cd\u7f6e\u3002\u53e6\u5916\uff0c\u5982\u679c AttributeError \u662f\u5728\u8bbf\u95ee <strong>spec<\/strong> \u6216\u8bbe\u7f6e\u6a21\u5757\u5c5e\u6027\u65f6\u89e6\u53d1\u7684\uff0c\u5219\u4e0d\u4f1a\u89e6\u53d1 \u3002<\/p>\n<p>\u672c\u51fd\u6570\u6bd4 types.ModuleType \u521b\u5efa\u65b0\u6a21\u5757\u8981\u597d\uff0c\u56e0\u4e3a\u7528\u5230 <strong>spec<\/strong> \u6a21\u5757\u8bbe\u7f6e\u4e86\u5c3d\u53ef\u80fd\u591a\u7684\u5bfc\u5165\u63a7\u5236\u5c5e\u6027\u3002<\/p>\n<p>3.5 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>@importlib.util.module_for_loader<\/p>\n<p>importlib.abc.Loader.load_module() \u7684\u4e00\u4e2a decorator\uff0c\u7528\u6765\u9009\u53d6\u5408\u9002\u7684\u6a21\u5757\u5bf9\u8c61\u4ee5\u4f9b\u52a0\u8f7d\u3002\u88ab\u88c5\u9970\u65b9\u6cd5\u7684\u7b7e\u540d\u5e94\u5e26\u6709\u4e24\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff08\u5982\uff1a<code>load_module(self, module)<\/code>\uff09\uff0c\u5176\u4e2d\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c06\u662f\u52a0\u8f7d\u5668\u7528\u5230\u7684\u6a21\u5757 <strong>\u5bf9\u8c61<\/strong>\u3002\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u5047\u5b9a\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u88c5\u9970\u5668\u5bf9\u9759\u6001\u65b9\u6cd5\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p>\u88c5\u9970\u7684\u65b9\u6cd5\u5c06\u63a5\u53d7\u8981\u52a0\u8f7d\u7684\u6a21\u5757\u7684 <strong>name<\/strong>\uff0c\u6b63\u5982 loader \u4e00\u6837\u3002\u5982\u679c\u5728 sys.modules \u4e2d\u6ca1\u6709\u627e\u5230\u8be5\u6a21\u5757\uff0c\u90a3\u4e48\u5c06\u6784\u9020\u4e00\u4e2a\u65b0\u6a21\u5757\u3002\u4e0d\u7ba1\u6a21\u5757\u6765\u81ea\u54ea\u91cc\uff0c __loader__ \u8bbe\u7f6e\u4e3a <strong>self<\/strong> \uff0c\u5e76\u4e14 __package__ \u662f\u6839\u636e importlib.abc.InspectLoader.is_package() \u7684\u8fd4\u56de\u503c\u8bbe\u7f6e\u7684\u3002\u8fd9\u4e9b\u5c5e\u6027\u4f1a\u65e0\u6761\u4ef6\u8fdb\u884c\u8bbe\u7f6e\u4ee5\u4fbf\u652f\u6301\u518d\u6b21\u52a0\u8f7d\u3002<\/p>\n<p>\u5982\u679c\u88ab\u88c5\u9970\u7684\u65b9\u6cd5\u89e6\u53d1\u5f02\u5e38\uff0c\u5e76\u4e14\u5df2\u6709\u6a21\u5757\u52a0\u5165 sys.modules \u4e2d\uff0c\u90a3\u4e48\u8be5\u6a21\u5757\u5c06\u88ab\u79fb\u9664\uff0c\u4ee5\u9632 sys.modules \u4e2d\u6b8b\u7559\u4e00\u4e2a\u90e8\u5206\u521d\u59cb\u5316\u7684\u6a21\u5757\u3002\u5982\u679c\u8be5\u6a21\u5757\u539f\u5148\u5df2\u5728 sys.modules \u4e2d\uff0c\u5219\u4f1a\u4fdd\u7559\u4e0d\u53d8\u3002<\/p>\n<p>\u5728 3.3 \u7248\u66f4\u6539: \u6709\u53ef\u80fd\u65f6\u81ea\u52a8\u8bbe\u7f6e __loader__ \u548c __package__ \u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u65e0\u6761\u4ef6\u8bbe\u7f6e __name__ \u3001 __loader__ \u3001 __package__ \u4ee5\u652f\u6301\u518d\u6b21\u52a0\u8f7d\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u73b0\u5728\uff0c\u5bfc\u5165\u673a\u5236\u76f4\u63a5\u6267\u884c\u672c\u51fd\u6570\u63d0\u4f9b\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n<p>@importlib.util.set_loader<\/p>\n<p>\u4e00\u4e2a decorator\uff0c\u7528\u4e8e importlib.abc.Loader.load_module() \u5728\u8fd4\u56de\u7684\u6a21\u5757\u4e0a\u8bbe\u7f6e __loader__ \u5c5e\u6027\u3002\u5982\u679c\u8be5\u5c5e\u6027\u5df2\u88ab\u8bbe\u7f6e\uff0c\u88c5\u9970\u5668\u5c31\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002\u8fd9\u91cc\u5047\u5b9a\u88ab\u5c01\u88c5\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff08\u5373 <code>self<\/code>\uff09\u5c31\u662f __loader__ \u8981\u8bbe\u7f6e\u7684\u3002<\/p>\n<p>\u5728 3.4 \u7248\u66f4\u6539: \u5982\u679c\u8bbe\u4e3a <code>None<\/code> \uff0c\u5219\u4f1a\u53bb\u8bbe\u7f6e <code>__loader__<\/code> \uff0c\u5c31\u50cf\u8be5\u5c5e\u6027\u4e0d\u5b58\u5728\u4e00\u6837\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u73b0\u5728\u5bfc\u5165\u673a\u5236\u4f1a\u81ea\u52a8\u7528\u5230\u672c\u65b9\u6cd5\u3002<\/p>\n<p>@importlib.util.set_package<\/p>\n<p>\u4e00\u4e2a\u7528\u4e8e importlib.abc.Loader.load_module() \u7684 decorator \uff0c\u4ee5\u4fbf\u8bbe\u7f6e\u8fd4\u56de\u6a21\u5757\u7684 __package__ \u5c5e\u6027\u3002\u5982\u679c __package__ \u5df2\u8bbe\u7f6e\u4e14\u4e0d\u4e3a <code>None<\/code>\uff0c\u5219\u4e0d\u4f1a\u505a\u6539\u52a8\u3002<\/p>\n<p>3.4 \u7248\u540e\u5df2\u79fb\u9664: \u73b0\u5728\u5bfc\u5165\u673a\u5236\u4f1a\u81ea\u52a8\u7528\u5230\u672c\u65b9\u6cd5\u3002<\/p>\n<p>importlib.util.spec_from_loader(<em>name<\/em>, <em>loader<\/em>, <em>**, <\/em>origin=None<em>, <\/em>is_package=None*)<\/p>\n<p>\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u57fa\u4e8e\u52a0\u8f7d\u5668\u7684 ModuleSpec \u5b9e\u4f8b\u3002\u53c2\u6570\u7684\u542b\u4e49\u4e0e ModuleSpec \u7684\u76f8\u540c\u3002\u8be5\u51fd\u6570\u4f1a\u5229\u7528\u5f53\u524d\u53ef\u7528\u7684 loader API\uff0c\u6bd4\u5982 <code>InspectLoader.is_package()<\/code>\uff0c\u4ee5\u586b\u5145\u6240\u6709\u7f3a\u5931\u7684\u89c4\u683c\u4fe1\u606f\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>importlib.util.spec_from_file_location(<em>name<\/em>, <em>location<\/em>, <em>**, <\/em>loader=None<em>, <\/em>submodule_search_locations=None*)<\/p>\n<p>\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u6839\u636e\u6587\u4ef6\u8def\u5f84\u521b\u5efa ModuleSpec \u5b9e\u4f8b\u3002\u7f3a\u5931\u7684\u4fe1\u606f\u5c06\u6839\u636e spec \u8fdb\u884c\u586b\u8865\uff0c\u5229\u7528\u52a0\u8f7d\u5668 API \uff0c\u4ee5\u53ca\u6a21\u5757\u57fa\u4e8e\u6587\u4ef6\u7684\u9690\u542b\u6761\u4ef6\u3002<\/p>\n<p>3.4 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: \u63a5\u53d7\u4e00\u4e2a path-like object\u3002<\/p>\n<p>importlib.util.source_hash(<em>source_bytes<\/em>)<\/p>\n<p>\u4ee5\u5b57\u8282\u4e32\u7684\u5f62\u5f0f\u8fd4\u56de <em>source_bytes<\/em> \u7684\u54c8\u5e0c\u503c\u3002\u57fa\u4e8e\u54c8\u5e0c\u503c\u7684 <code>.pyc<\/code> \u6587\u4ef6\u5728\u5934\u90e8\u5d4c\u5165\u4e86\u5bf9\u5e94\u6e90\u6587\u4ef6\u5185\u5bb9\u7684 source_hash()\u3002<\/p>\n<p>3.7 \u65b0\u7248\u529f\u80fd.<\/p>\n<p><em>class<\/em> importlib.util.LazyLoader(<em>loader<\/em>)<\/p>\n<p>\u6b64\u7c7b\u4f1a\u5ef6\u8fdf\u6267\u884c\u6a21\u5757\u52a0\u8f7d\u5668\uff0c\u76f4\u81f3\u8be5\u6a21\u5757\u6709\u4e00\u4e2a\u5c5e\u6027\u88ab\u8bbf\u95ee\u5230\u3002<\/p>\n<p>\u6b64\u7c7b <strong>\u53ea<\/strong> \u9002\u7528\u4e8e\u5b9a\u4e49\u4e86 exec_module() \u7684\u52a0\u8f7d\u5668\uff0c\u56e0\u4e3a\u9700\u8981\u63a7\u5236\u6a21\u5757\u7684\u7c7b\u578b\u3002 \u540c\u7406\uff0c\u52a0\u8f7d\u5668\u7684 create_module() \u65b9\u6cd5\u5fc5\u987b\u8fd4\u56de <code>None<\/code> \u6216 <code>__class__<\/code> \u5c5e\u6027\u53ef\u88ab\u6539\u53d8\u4e14\u4e0d\u7528 slots \u7684\u7c7b\u578b\u3002 \u6700\u540e\uff0c\u7528\u4e8e\u66ff\u6362 sys.modules \u5185\u5bb9\u7684\u6a21\u5757\u5c06\u65e0\u6cd5\u5de5\u4f5c\uff0c\u56e0\u4e3a\u65e0\u6cd5\u5728\u6574\u4e2a\u89e3\u91ca\u5668\u4e2d\u5b89\u5168\u5730\u66ff\u6362\u6a21\u5757\u7684\u5f15\u7528\uff1b\u5982\u679c\u68c0\u6d4b\u5230\u8fd9\u79cd\u66ff\u6362\uff0c\u5c06\u89e6\u53d1 ValueError\u3002<\/p>\n<p>\u5907\u6ce8<\/p>\n<p>\u5982\u679c\u9879\u76ee\u5bf9\u542f\u52a8\u65f6\u95f4\u8981\u6c42\u5f88\u9ad8\uff0c\u53ea\u8981\u6a21\u5757\u672a\u88ab\u7528\u8fc7\uff0c\u6b64\u7c7b\u80fd\u591f\u6700\u5c0f\u5316\u52a0\u8f7d\u6a21\u5757\u7684\u5f00\u9500\u3002\u5bf9\u4e8e\u542f\u52a8\u65f6\u95f4\u5e76\u4e0d\u91cd\u8981\u7684\u9879\u76ee\u6765\u8bf4\uff0c\u7531\u4e8e\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u7684\u9519\u8bef\u4fe1\u606f\u4f1a\u88ab\u6682\u65f6\u6401\u7f6e\uff0c\u56e0\u6b64\u5f3a\u70c8\u4e0d\u5efa\u8bae\u4f7f\u7528\u6b64\u7c7b\u3002<\/p>\n<p>3.5 \u65b0\u7248\u529f\u80fd.<\/p>\n<p>\u5728 3.6 \u7248\u66f4\u6539: \u5f00\u59cb\u8c03\u7528 create_module()\uff0c\u79fb\u9664 importlib.machinery.BuiltinImporter \u548c importlib.machinery.ExtensionFileLoader \u7684\u517c\u5bb9\u6027\u8b66\u544a\u3002<\/p>\n<ul>\n<li>\n<p><em>classmethod<\/em> factory(<em>loader<\/em>)<\/p>\n<p>\u9759\u6001\u65b9\u6cd5\uff0c\u8fd4\u56de\u521b\u5efa\u5ef6\u8fdf\u52a0\u8f7d\u5668\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u5c31\u662f\u8bf4\u7528\u5728\u52a0\u8f7d\u5668\u7528\u7c7b\u800c\u4e0d\u662f\u5b9e\u4f8b\u4f20\u9012\u7684\u573a\u5408\u3002<\/p>\n<pre>\n   \n   \n   \n   <ol>\n    \n    \n    \n    <li><code><span>suffixes <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>SOURCE_SUFFIXES<\/span><\/code><\/li>\n    \n    \n    \n    <li><code><span>loader <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>SourceFileLoader<\/span><\/code><\/li>\n    \n    \n    \n    <li><code><span>lazy_loader <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>LazyLoader<\/span><span>.<\/span><span>factory<\/span><span>(<\/span><span>loader<\/span><span>)<\/span><\/code><\/li>\n    \n    \n    \n    <li><code><span>finder <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>FileFinder<\/span><span>(<\/span><span>path<\/span><span>,<\/span><span> <\/span><span>(<\/span><span>lazy_loader<\/span><span>,<\/span><span> suffixes<\/span><span>))<\/span><\/code><\/li>\n   \n   \n   \n   <\/ol><\/pre>\n<\/li>\n<\/ul>\n<h2>\u4f8b\u5b50<\/h2>\n<h3>\u7528\u7f16\u7a0b\u65b9\u5f0f\u5bfc\u5165<\/h3>\n<p>\u8981\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\uff0c\u8bf7\u4f7f\u7528 importlib.import_module() \uff1a<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>import<\/span><span> importlib<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>itertools <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>import_module<\/span><span>(<\/span><span>'itertools'<\/span><span>)<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<h3>\u68c0\u67e5\u67d0\u6a21\u5757\u53ef\u5426\u5bfc\u5165\u3002<\/h3>\n<p>\u5982\u679c\u4f60\u9700\u8981\u5728\u4e0d\u5b9e\u9645\u6267\u884c\u5bfc\u5165\u7684\u60c5\u51b5\u4e0b\u786e\u5b9a\u67d0\u4e2a\u6a21\u5757\u662f\u5426\u53ef\u88ab\u5bfc\u5165\uff0c\u5219\u4f60\u5e94\u5f53\u4f7f\u7528 importlib.util.find_spec()\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\u5982\u679c <code>name<\/code> \u662f\u4e00\u4e2a\u5b50\u6a21\u5757\uff08\u5373\u5305\u542b\u4e00\u4e2a\u70b9\u53f7\uff09\uff0c\u5219 importlib.util.find_spec() \u5c06\u4f1a\u5bfc\u5165\u7236\u6a21\u5757\u3002<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>import<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>import<\/span><span> sys<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span># For illustrative purposes.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>name <\/span><span>=<\/span><span> <\/span><span>'itertools'<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>if<\/span><span> name <\/span><span>in<\/span><span> sys<\/span><span>.<\/span><span>modules<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>print<\/span><span>(<\/span><span>f<\/span><span>\"{name!r} already in sys.modules\"<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>elif<\/span><span> <\/span><span>(<\/span><span>spec <\/span><span>:=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>find_spec<\/span><span>(<\/span><span>name<\/span><span>))<\/span><span> <\/span><span>is<\/span><span> <\/span><span>not<\/span><span> <\/span><span>None<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span># If you chose to perform the actual import ...<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>module<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>module_from_spec<\/span><span>(<\/span><span>spec<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> sys<\/span><span>.<\/span><span>modules<\/span><span>[<\/span><span>name<\/span><span>]<\/span><span> <\/span><span>=<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> spec<\/span><span>.<\/span><span>loader<\/span><span>.<\/span><span>exec_module<\/span><span>(<\/span><span>module<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>print<\/span><span>(<\/span><span>f<\/span><span>\"{name!r} has been imported\"<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>else<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>print<\/span><span>(<\/span><span>f<\/span><span>\"can't find the {name!r} module\"<\/span><span>)<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<h3>\u76f4\u63a5\u5bfc\u5165\u6e90\u7801\u6587\u4ef6\u3002<\/h3>\n<p>To import a Python source file directly, use the following recipe:<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>import<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>import<\/span><span> sys<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span># For illustrative purposes.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>import<\/span><span> tokenize<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>file_path <\/span><span>=<\/span><span> tokenize<\/span><span>.<\/span><span>__file__<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>module_name <\/span><span>=<\/span><span> tokenize<\/span><span>.<\/span><span>__name__<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>spec <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>spec_from_file_location<\/span><span>(<\/span><span>module_name<\/span><span>,<\/span><span> file_path<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>module<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>module_from_spec<\/span><span>(<\/span><span>spec<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>sys<\/span><span>.<\/span><span>modules<\/span><span>[<\/span><span>module_name<\/span><span>]<\/span><span> <\/span><span>=<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>spec<\/span><span>.<\/span><span>loader<\/span><span>.<\/span><span>exec_module<\/span><span>(<\/span><span>module<\/span><span>)<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<h3>\u5b9e\u73b0\u5ef6\u8fdf\u5bfc\u5165<\/h3>\n<p>\u4ee5\u4e0b\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u5ef6\u8fdf\u5bfc\u5165\uff1a<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>import<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>import<\/span><span> sys<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>def<\/span><span> lazy_import<\/span><span>(<\/span><span>name<\/span><span>):<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> spec <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>find_spec<\/span><span>(<\/span><span>name<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> loader <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>LazyLoader<\/span><span>(<\/span><span>spec<\/span><span>.<\/span><span>loader<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> spec<\/span><span>.<\/span><span>loader <\/span><span>=<\/span><span> loader<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> <\/span><span>module<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>module_from_spec<\/span><span>(<\/span><span>spec<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> sys<\/span><span>.<\/span><span>modules<\/span><span>[<\/span><span>name<\/span><span>]<\/span><span> <\/span><span>=<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> loader<\/span><span>.<\/span><span>exec_module<\/span><span>(<\/span><span>module<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><span> <\/span><span>return<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>...<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> lazy_typing <\/span><span>=<\/span><span> lazy_import<\/span><span>(<\/span><span>\"typing\"<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>#lazy_typing is a real module object,<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> <\/span><span>#but it is not loaded in memory yet.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>&gt;&gt;&gt;<\/span><span> lazy_typing<\/span><span>.<\/span><span>TYPE_CHECKING<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>False<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<h3>\u5bfc\u5165\u5668\u7684\u914d\u7f6e<\/h3>\n<p>\u5bf9\u4e8e\u6df1\u5ea6\u5b9a\u5236\u7684\u5bfc\u5165\uff0c\u901a\u5e38\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a importer\u3002 \u8fd9\u610f\u5473\u7740\u5f97\u540c\u65f6\u7ba1\u7406 finder \u548c loader\u3002 \u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u6709\u4e24\u79cd\u7c7b\u578b\u7684\u67e5\u627e\u5668\u53ef\u4f9b\u9009\u62e9\uff1a meta path finder \u6216 path entry finder\u3002 \u524d\u8005\u5e94\u4f4d\u4e8e sys.meta_path \u4e4b\u4e0a\uff0c\u800c\u540e\u8005\u662f\u7528 path entry hook \u5728 sys.path_hooks \u4e0a\u521b\u5efa\u4f46\u4e0e sys.path \u4e00\u8d77\u5de5\u4f5c\uff0c\u53ef\u80fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u67e5\u627e\u5668\u3002\u4ee5\u4e0b\u4f8b\u5b50\u5c06\u6f14\u793a\u5982\u4f55\u6ce8\u518c\u81ea\u5df1\u7684\u5bfc\u5165\u5668\uff0c\u4ee5\u4f9b\u5bfc\u5165\u4f7f\u7528\uff08\u5173\u4e8e\u81ea\u5efa\u5bfc\u5165\u5668\u8bf7\u9605\u8bfb\u672c\u5305\u5185\u5b9a\u4e49\u7684\u7c7b\u6587\u6863\uff09\uff1a<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>import<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>import<\/span><span> sys<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span># For illustrative purposes only.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>SpamMetaPathFinder<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>PathFinder<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>SpamPathEntryFinder<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>FileFinder<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>loader_details <\/span><span>=<\/span><span> <\/span><span>(<\/span><span>importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>SourceFileLoader<\/span><span>,<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> importlib<\/span><span>.<\/span><span>machinery<\/span><span>.<\/span><span>SOURCE_SUFFIXES<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span># Setting up a meta path finder.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span># Make sure to put the finder in the proper location in the list in terms of<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span># priority.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>sys<\/span><span>.<\/span><span>meta_path<\/span><span>.<\/span><span>append<\/span><span>(<\/span><span>SpamMetaPathFinder<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span># Setting up a path entry finder.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span># Make sure to put the path hook in the proper location in the list in terms<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span># of priority.<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>sys<\/span><span>.<\/span><span>path_hooks<\/span><span>.<\/span><span>append<\/span><span>(<\/span><span>SpamPathEntryFinder<\/span><span>.<\/span><span>path_hook<\/span><span>(<\/span><span>loader_details<\/span><span>))<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n<h3>importlib.import_module() \u7684\u8fd1\u4f3c\u5b9e\u73b0<\/h3>\n<p>Import itself is implemented in Python code, making it possible to expose most of the import machinery through importlib. The following helps illustrate the various APIs that importlib exposes by providing an approximate implementation of importlib.import_module():<\/p>\n<pre>\n \n \n \n <ol>\n  \n  \n  \n  <li><code><span>import<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span>import<\/span><span> sys<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span>def<\/span><span> import_module<\/span><span>(<\/span><span>name<\/span><span>,<\/span><span> <\/span><span>package<\/span><span>=<\/span><span>None<\/span><span>):<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>\"\"\"An approximate implementation of import.\"\"\"<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> absolute_name <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>resolve_name<\/span><span>(<\/span><span>name<\/span><span>,<\/span><span> <\/span><span>package<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>try<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>return<\/span><span> sys<\/span><span>.<\/span><span>modules<\/span><span>[<\/span><span>absolute_name<\/span><span>]<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>except<\/span><span> <\/span><span>KeyError<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>pass<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><\/code><\/li>\n  \n  \n  \n  <li><code><span> path <\/span><span>=<\/span><span> <\/span><span>None<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>if<\/span><span> <\/span><span>'.'<\/span><span> <\/span><span>in<\/span><span> absolute_name<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> parent_name<\/span><span>,<\/span><span> _<\/span><span>,<\/span><span> child_name <\/span><span>=<\/span><span> absolute_name<\/span><span>.<\/span><span>rpartition<\/span><span>(<\/span><span>'.'<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> parent_module <\/span><span>=<\/span><span> import_module<\/span><span>(<\/span><span>parent_name<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> path <\/span><span>=<\/span><span> parent_module<\/span><span>.<\/span><span>__spec__<\/span><span>.<\/span><span>submodule_search_locations<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>for<\/span><span> finder <\/span><span>in<\/span><span> sys<\/span><span>.<\/span><span>meta_path<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> spec <\/span><span>=<\/span><span> finder<\/span><span>.<\/span><span>find_spec<\/span><span>(<\/span><span>absolute_name<\/span><span>,<\/span><span> path<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>if<\/span><span> spec <\/span><span>is<\/span><span> <\/span><span>not<\/span><span> <\/span><span>None<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>break<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>else<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> msg <\/span><span>=<\/span><span> f<\/span><span>'No module named {absolute_name!r}'<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>raise<\/span><span> <\/span><span>ModuleNotFoundError<\/span><span>(<\/span><span>msg<\/span><span>,<\/span><span> name<\/span><span>=<\/span><span>absolute_name<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>module<\/span><span> <\/span><span>=<\/span><span> importlib<\/span><span>.<\/span><span>util<\/span><span>.<\/span><span>module_from_spec<\/span><span>(<\/span><span>spec<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> sys<\/span><span>.<\/span><span>modules<\/span><span>[<\/span><span>absolute_name<\/span><span>]<\/span><span> <\/span><span>=<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> spec<\/span><span>.<\/span><span>loader<\/span><span>.<\/span><span>exec_module<\/span><span>(<\/span><span>module<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>if<\/span><span> path <\/span><span>is<\/span><span> <\/span><span>not<\/span><span> <\/span><span>None<\/span><span>:<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> setattr<\/span><span>(<\/span><span>parent_module<\/span><span>,<\/span><span> child_name<\/span><span>,<\/span><span> <\/span><span>module<\/span><span>)<\/span><\/code><\/li>\n  \n  \n  \n  <li><code><span> <\/span><span>return<\/span><span> <\/span><span>module<\/span><\/code><\/li>\n \n \n \n <\/ol><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>importlib \u2014- import \u7684\u5b9e\u73b0 3.1 \u65b0\u7248\u529f\u80fd. \u6e90\u4ee3\u7801 Lib\/importlib\/__i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-167690","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/167690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=167690"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/167690\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=167690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=167690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=167690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}