{"id":209114,"date":"2021-01-16T08:26:56","date_gmt":"2021-01-16T00:26:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=209114"},"modified":"2021-01-08T12:27:59","modified_gmt":"2021-01-08T04:27:59","slug":"python-package-import","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-package-import.html","title":{"rendered":"\u8fd9\u4e09\u4e2a\u5173\u4e8ePython\u5305\u5bfc\u5165\u7684\u77e5\u8bc6\u70b9\u4f60\u77e5\u9053\u5417\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f7f\u7528 from module import * \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u5165 module \u91cc\u7684\u6240\u6709\u53d8\u91cf\uff0c\u82e5\u4f60\u53ea\u60f3\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u5176\u4e2d\u51e0\u4e2a\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728 module \u4e2d\u4f7f\u7528 __all__ \u6765\u63a7\u5236\u60f3\u8981\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u7684\u53d8\u91cf\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1. \u4f7f\u7528 __all__ \u63a7\u5236\u53ef\u88ab\u5bfc\u5165\u7684\u53d8\u91cf<\/strong><\/div>\n

\u4f7f\u7528 from module import * \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u5165 module \u91cc\u7684\u6240\u6709\u53d8\u91cf\uff0c\u82e5\u4f60\u53ea\u60f3\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u5176\u4e2d\u51e0\u4e2a\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728 module \u4e2d\u4f7f\u7528 __all__ \u6765\u63a7\u5236\u60f3\u8981\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u7684\u53d8\u91cf\u3002<\/p>\n

\r\n# profile.py \r\nname='\u5c0f\u660e' \r\nage=18 \r\n \r\n__all__=['name'] <\/pre>\n

\u6253\u5f00 python console \u9a8c\u8bc1\u4e00\u4e0b<\/p>\n

\r\n>>> from profile import * \r\n>>> print(name) \r\n\u5c0f\u660e \r\n>>> print(age) \r\nTraceback (most recent call last): \r\n  File \"\", line 1, in  \r\nNameError: name 'age' is not defined \r\n__all__ \u4ec5\u5bf9\u4e8e\u4f7f\u7528from module import * \u8fd9\u79cd\u60c5\u51b5\u9002\u7528\u3002<\/module><\/stdin><\/pre>\n

\u5b83\u7ecf\u5e38\u5728\u4e00\u4e2a\u5305\u7684 __init__.py \u4e2d\u51fa\u73b0\u3002<\/p>\n

2. \u547d\u540d\u7a7a\u95f4\u5305\u7684\u795e\u5947\u4e4b\u5904<\/strong><\/div>\n

\u547d\u540d\u7a7a\u95f4\u5305\uff0c\u5bf9\u4e8e\u4e0d\u5c11\u4eba\u6765\u8bf4\uff0c\u53ef\u80fd\u662f\u4e00\u4e2a\u964c\u751f\u7684\u540d\u5b57\u3002<\/p>\n

\u4e0e\u6211\u4eec\u719f\u6089\u7684\u5e38\u89c4\u5305\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u6ca1\u6709 __init__.py \u6587\u4ef6\u3002<\/p>\n

\u66f4\u4e3a\u7279\u6b8a\u7684\u662f\uff0c\u5b83\u53ef\u4ee5\u8de8\u7a7a\u95f4\u5730\u5c06\u4e24\u4e2a\u4e0d\u76f8\u90bb\u7684\u5b50\u5305\uff0c\u5408\u5e76\u6210\u4e00\u4e2a\u865a\u62df\u673a\u7684\u5305\uff0c\u6211\u4eec\u5c06\u5176\u79f0\u4e4b\u4e3a \u547d\u540d\u7a7a\u95f4\u5305\u3002<\/p>\n

\u4f8b\u5982\uff0c\u4e00\u4e2a\u9879\u76ee\u7684\u90e8\u5206\u4ee3\u7801\u5e03\u5c40\u5982\u4e0b<\/p>\n

\r\nfoo-package\/ \r\n    spam\/ \r\n        blah.py \r\n \r\nbar-package\/ \r\n    spam\/ \r\n        grok.py <\/pre>\n

\u5728\u8fd92\u4e2a\u76ee\u5f55\u91cc\uff0c\u90fd\u6709\u7740\u5171\u540c\u7684\u547d\u540d\u7a7a\u95f4spam\u3002\u5728\u4efb\u4f55\u4e00\u4e2a\u76ee\u5f55\u91cc\u90fd\u6ca1\u6709__init__.py\u6587\u4ef6\u3002<\/p>\n

\u8ba9\u6211\u4eec\u770b\u770b\uff0c\u5982\u679c\u5c06foo-package\u548cbar-package\u90fd\u52a0\u5230python\u6a21\u5757\u8def\u5f84\u5e76\u5c1d\u8bd5\u5bfc\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48?<\/p>\n

\r\n>>> import sys \r\n>>> sys.path.extend(['foo-package', 'bar-package']) \r\n>>> import spam.blah \r\n>>> import spam.grok \r\n>>> <\/pre>\n

\u5f53\u4e00\u4e2a\u5305\u4e3a\u547d\u540d\u7a7a\u95f4\u5305\u65f6\uff0c\u4ed6\u5c31\u4e0d\u518d\u548c\u5e38\u89c4\u5305\u4e00\u6837\u5177\u6709 __file_ \u5c5e\u6027\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f __path__<\/p>\n

\r\n>>> import sys \r\n>>> sys.path.extend(['foo-package', 'bar-package']) \r\n>>> import spam.blah \r\n>>> import spam.grok \r\n>>> spam.__path__ \r\n_NamespacePath(['foo-package\/spam', 'bar-package\/spam']) \r\n>>> spam.__file__ \r\nTraceback (most recent call last): \r\n    File \"\", line 1, in  \r\nAttributeError: 'module' object has no attribute '__file__' <\/module><\/stdin><\/pre>\n
3. \u6a21\u5757\u91cd\u8f7d\u4e2d\u7684\u4e00\u4e2a\u5751<\/strong><\/div>\n

\u7531\u4e8e\u6709 sys.modules \u7684\u5b58\u5728\uff0c\u5f53\u4f60\u5bfc\u5165\u4e00\u4e2a\u5df2\u5bfc\u5165\u7684\u6a21\u5757\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u6ca1\u6709\u6548\u679c\u7684\u3002<\/p>\n

\u4e3a\u4e86\u8fbe\u5230\u6a21\u5757\u7684\u91cd\u8f7d\uff0c\u6709\u7684\u4eba\u4f1a\u5c06\u5df2\u5bfc\u5165\u7684\u5305\u4ece sys.modules \u4e2d\u79fb\u9664\u540e\u518d\u5bfc\u5165<\/p>\n

\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\u5b50<\/p>\n

\r\n>>> import foo.bar \r\nsuccessful to be imported \r\n>>> \r\n>>> import foo.bar \r\n>>> \r\n>>> import sys \r\n>>> sys.modules['foo.bar'] \r\n \r\n>>> del sys.modules['foo.bar'] \r\n>>> \r\n>>> import foo.bar \r\nsuccessful to be imported <\/module><\/pre>\n

\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\u6211\u4f7f\u7528\u7684\u662fimport foo.bar \uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f from foo import bar \u8fd9\u79cd\u5bfc\u5165\u5f62\u5f0f\uff0c\u4f1a\u53d1\u73b0\u91cd\u8f7d\u662f\u540c\u6837\u662f\u65e0\u6548\u7684\u3002<\/p>\n

\u8fd9\u5e94\u8be5\u7b97\u662f\u4e00\u4e2a\u5c0f\u5751\uff0c\u4e0d\u77e5\u9053\u7684\u4eba\uff0c\u4f1a\u6389\u5165\u5751\u4e2d\u722c\u4e0d\u51fa\u6765\u3002<\/p>\n

\r\n>>> import foo.bar \r\nsuccessful to be imported \r\n>>> \r\n>>> import foo.bar \r\n>>> \r\n>>> import sys \r\n>>> del sys.modules['foo.bar'] \r\n>>> from foo import bar \r\n>>> <\/pre>\n

\u56e0\u6b64\uff0c\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u53ef\u80fd\u9700\u8981\u907f\u514d\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002\u800c\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e2d\uff0c\u5b83\u4f1a\u63d0\u4f9b\u4e00\u5b9a\u7684\u4fbf\u5229\uff0c\u4f46\u4f60\u8981\u77e5\u9053\u8fd9\u4e2a\u91cd\u8f7d\u7684\u5f0a\u7aef\uff0c\u4ee5\u514d\u6389\u5165\u5751\u91cc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528 from module import * \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u5165 module \u91cc\u7684\u6240\u6709\u53d8\u91cf\uff0c\u82e5\u4f60\u53ea\u60f3\u4ece\u6a21\u5757\u4e2d […]<\/p>\n","protected":false},"author":317,"featured_media":160637,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-209114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=209114"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209114\/revisions"}],"predecessor-version":[{"id":209117,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/209114\/revisions\/209117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160637"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=209114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=209114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=209114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}