{"id":496,"date":"2020-01-09T00:50:17","date_gmt":"2020-01-08T16:50:17","guid":{"rendered":"https:\/\/kaispace.cn\/?p=496"},"modified":"2020-01-09T00:50:17","modified_gmt":"2020-01-08T16:50:17","slug":"%e5%85%b3%e4%ba%8eimportlib-%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bd%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=496","title":{"rendered":"\u5173\u4e8eimportlib \u52a8\u6001\u52a0\u8f7d\u7684\u4f7f\u7528"},"content":{"rendered":"<h1>\u5173\u4e8eimportlib \u52a8\u6001\u52a0\u8f7d\u7684\u4f7f\u7528<\/h1>\n<p>\u9879\u76ee\u4e2d\u6709\u5173\u4e8e\u5bf9Python\u5305\u52a8\u6001\u52a0\u8f7d\u7684\u9700\u6c42\uff0c\u800c\u4e14\u8981\u6c42\u662f\u4e0d\u5c40\u9650\u4e8e\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u5168\u5c40\u5bfc\u5305\uff0c\u800c\u4e14\u8981\u80fd\u652f\u6301\u5305\u66f4\u65b0\uff0c\u6e90\u7a0b\u5e8f\u540c\u6b65\u66f4\u65b0<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u5230\u4e86importlib\u8fd9\u4e2a\u5305,\u5148\u653e\u51fa\u5de5\u5177\u7c7b<\/p>\n<pre><code class=\"language-python line-numbers\">import importlib\nimport sys\nimport os\n\n\nclass NameConflictError(Exception):\n    def __init__(self, error_msg: str) -&gt; None:\n        super().__init__(self)\n        self.error_msg = error_msg\n\n    def __str__(self) -&gt; str:\n        return self.error_msg\n\n\nclass ModuleLoader(object):\n    @staticmethod\n    def get_module(module_name: str, package_path=None):\n        if package_path is None:\n            package_path = os.getcwd()\n        # Ensure wanted module import from wanted pkg\n        if package_path in sys.path:\n            sys.path.remove(package_path)\n        sys.path.insert(0, package_path)\n        mod = importlib.import_module(module_name)\n        exist_module_pkg_path = os.path.dirname(mod.__file__)\n        if package_path != exist_module_pkg_path:\n            mod = importlib.reload(mod)\n            # Read again\n            exist_module_pkg_path = os.path.dirname(mod.__file__)\n        if package_path != exist_module_pkg_path:\n            raise NameConflictError(\n                f\"The module {package_path} not exist or priority lower than {exist_module_pkg_path}\")\n        else:\n            return mod\n\n    @staticmethod\n    def get_member(module_name: str, func_name: str, package_path=None):\n        return getattr(ModuleLoader.get_module(module_name, package_path), func_name)\n<\/code><\/pre>\n<p>\u5176\u5b9e\u8fd9\u8fb9\u6709\u51e0\u79cd\u5bfc\u51fa\u573a\u666f\u662f\u9700\u8981\u6ce8\u610f\u7684\uff0c\u573a\u666f\u5982\u4e0b<\/p>\n<h3>\u524d\u63d0A\u5305\u4e3a\u5f53\u524d\u8bfb\u53d6\u5305\uff0cB\u5305\u4e3a\u5386\u53f2\u5df2\u8bfb\u5165\u8fc7\u7684\u5305 F\u4e3a\u6240\u9700\u8bfb\u53d6\u7684\u51fd\u6570(\u2705\u4e3a\u76f8\u540c \u274c\u4e3a\u4e0d\u540c)<\/h3>\n<table>\n<thead>\n<tr>\n<th>Pkg\u8def\u5f84<\/th>\n<th>Module\u8def\u5f84<\/th>\n<th>\u51fd\u6570\u540d<\/th>\n<th>\u7ed3\u679c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>A\u5305\u6709\u5219\u6210\u529f,\u5426\u5219\u62a5\u9519<\/td>\n<\/tr>\n<tr>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u2705<\/td>\n<td>A\u5305\u6709\u5219\u6210\u529f,\u5426\u5219\u62a5\u9519<\/td>\n<\/tr>\n<tr>\n<td>\u274c<\/td>\n<td>\u2705<\/td>\n<td>\u274c<\/td>\n<td>\u5047\u5982A\u5305\u6709F\u51fd\u6570\u5219\u8bfbA\u5305\u7684F\u51fd\u6570\uff0c\u5426\u5219\u8bfbB\u5305\u7684F\u51fd\u6570,\u5747\u65e0\u5219\u62a5\u9519<\/td>\n<\/tr>\n<tr>\n<td>\u2705<\/td>\n<td>\u274c<\/td>\n<td>\u274cor \u2705<\/td>\n<td>A\u5305\u6709\u5219\u6210\u529f,\u5426\u5219\u62a5\u9519<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5176\u5b9e\u603b\u7ed3\u4e0b\u6765\uff0cpython\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5173\u4e8emodule\u72ec\u7acb\u7684\u5bf9\u8c61\uff0c\u5f53\u8bfb\u5165\u591a\u4e2a\u4e0d\u540c\u7684\u540c\u540dmodule\u65f6\uff0cpython\u53ea\u4f1a\u66f4\u65b0\u4fee\u6539\u8fc7\u7684\u51fd\u6570\u6216\u8005\u6dfb\u52a0\u65b0\u7684\u51fd\u6570\uff0c\u800c\u4e0d\u4f1a\u5bf9\u88ab\u5220\u9664\u7684\u51fd\u6570\u8fdb\u884c\u53bb\u9664\u3002<\/h4>\n<h4>\u4f8b\u5982:<\/h4>\n<pre><code class=\"language-python line-numbers\"># test.A\ndef add():\n  return 100\n\ndef del():\n  return 500\n\n# --------\u4e0a\u4e0b\u4e0d\u540c\u5305--------\n\n# try.A\ndef add():\n  return 200\n\n<\/code><\/pre>\n<h4>\u4e0a\u8ff0\u60c5\u51b5\u5047\u5982\u5148\u52a0\u8f7dtest.A,\u6267\u884cadd,\u518d\u52a0\u8f7dtry.A,\u6267\u884cadd, \u6b64\u65f6\u7684\u8f93\u51fa\u662f100, 200, \u4f46\u5047\u5982\u6b64\u65f6\u4f60\u518d\u6267\u884cdel, \u56e0\u4e3atry.A\u662f\u540e\u8bfb\u5165\u7684\uff0c\u5176\u672c\u8eab\u662f\u6ca1\u6709del\u51fd\u6570\u7684\uff0c\u4f46\u662f\u56e0\u4e3a\u5386\u53f2\u8bfb\u5165\u7684test.A\u6709\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u4e3a500<\/h4>\n<h4>\u8fd8\u6709\u4e00\u4e2a\u70b9\u5c31\u662f\u53ea\u6709reload\u624d\u4f1a\u91cd\u65b0\u52a0\u8f7d\u5305\uff0c\u6bd4\u5982\u5148\u8bfb\u5165\u4e86test.A\uff0c\u8981\u60f3\u8bfb\u5165try.A\u7684\u8bdd\u5c31\u5fc5\u987breload\uff0c\u5426\u5219\u8fd8\u4f1a\u662ftest.A<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8eimportlib \u52a8\u6001\u52a0\u8f7d\u7684\u4f7f\u7528 \u9879\u76ee\u4e2d\u6709\u5173\u4e8e\u5bf9Python\u5305\u52a8\u6001\u52a0\u8f7d\u7684\u9700\u6c42\uff0c\u800c\u4e14\u8981\u6c42\u662f\u4e0d\u5c40\u9650\u4e8e\u5f53\u524d\u76ee\u5f55&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,15],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/496"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=496"}],"version-history":[{"count":0,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/496\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}