{"id":10021230,"date":"2026-02-01T02:40:22","date_gmt":"2026-01-31T18:40:22","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021230"},"modified":"2026-02-01T02:40:22","modified_gmt":"2026-01-31T18:40:22","slug":"python%e4%b8%ad%e7%9a%84%e6%a8%a1%e5%9d%97%e5%af%bc%e5%85%a5%e6%9c%ba%e5%88%b6%e4%b8%8e%e5%8c%85%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021230","title":{"rendered":"Python\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u4e0e\u5305\u7ba1\u7406"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u4e0e\u5305\u7ba1\u7406<\/h2>\n<h3>1. \u6a21\u5757\u4e0e\u5305\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u6a21\u5757\uff08Module\uff09\u548c\u5305\uff08Package\uff09\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u57fa\u672c\u5355\u4f4d\u3002\u7406\u89e3\u6a21\u5757\u548c\u5305\u7684\u6982\u5ff5\u662f\u638c\u63e1Python\u5bfc\u5165\u673a\u5236\u7684\u57fa\u7840\u3002<\/p>\n<h4>1.1 \u6a21\u5757\u7684\u5b9a\u4e49<\/h4>\n<p>\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u5b9a\u4e49\u548c\u8bed\u53e5\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u5c31\u662f\u6a21\u5757\u540d\u52a0\u4e0a<code>.py<\/code>\u540e\u7f00\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u540d\u4e3a<code>example.py<\/code>\u7684\u6587\u4ef6\u5c31\u662f\u4e00\u4e2a\u540d\u4e3a<code>example<\/code>\u7684\u6a21\u5757\u3002<\/p>\n<p>\u6a21\u5757\u7684\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u7ec4\u7ec7<\/strong>\uff1a\u5c06\u76f8\u5173\u7684\u4ee3\u7801\u7ec4\u7ec7\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027<\/li>\n<li><strong>\u4ee3\u7801\u91cd\u7528<\/strong>\uff1a\u901a\u8fc7\u5bfc\u5165\u673a\u5236\uff0c\u6a21\u5757\u53ef\u4ee5\u88ab\u5176\u4ed6\u4ee3\u7801\u91cd\u7528<\/li>\n<li><strong>\u547d\u540d\u7a7a\u95f4\u9694\u79bb<\/strong>\uff1a\u6bcf\u4e2a\u6a21\u5757\u6709\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81<\/li>\n<\/ul>\n<h4>1.2 \u5305\u7684\u5b9a\u4e49<\/h4>\n<p>\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u5b83\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\uff08\u5728Python 3.3+\u4e2d\uff0c<code>__init__.py<\/code>\u6587\u4ef6\u662f\u53ef\u9009\u7684\uff0c\u4f46\u4e3a\u4e86\u4fdd\u6301\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u4ecd\u7136\u6dfb\u52a0\uff09\u3002<\/p>\n<p>\u5305\u7684\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li><strong>\u5c42\u6b21\u5316\u7ec4\u7ec7<\/strong>\uff1a\u5c06\u76f8\u5173\u7684\u6a21\u5757\u7ec4\u7ec7\u5230\u4e00\u4e2a\u76ee\u5f55\u7ed3\u6784\u4e2d<\/li>\n<li><strong>\u547d\u540d\u7a7a\u95f4\u7ba1\u7406<\/strong>\uff1a\u901a\u8fc7\u5305\u7684\u5c42\u6b21\u7ed3\u6784\uff0c\u63d0\u4f9b\u66f4\u6e05\u6670\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<li><strong>\u6a21\u5757\u5206\u7ec4<\/strong>\uff1a\u5c06\u529f\u80fd\u76f8\u5173\u7684\u6a21\u5757\u5206\u7ec4\u5230\u4e00\u4e2a\u5305\u4e2d<\/li>\n<\/ul>\n<h4>1.3 \u6a21\u5757\u4e0e\u5305\u7684\u5173\u7cfb<\/h4>\n<p>\u6a21\u5757\u548c\u5305\u7684\u5173\u7cfb\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a<\/p>\n<ul>\n<li><strong>\u6a21\u5757<\/strong>\uff1a\u5355\u4e2aPython\u6587\u4ef6\uff0c\u662f\u4ee3\u7801\u7ec4\u7ec7\u7684\u6700\u5c0f\u5355\u4f4d<\/li>\n<li><strong>\u5305<\/strong>\uff1a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u662f\u6a21\u5757\u7684\u96c6\u5408<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u6a21\u5757\u4e0e\u5305\u7684\u57fa\u672c\u6982\u5ff5\u793a\u4f8b\n\n# 1. \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u5757\n# \u6587\u4ef6\u540d: mymodule.py\n&quot;&quot;&quot;\n\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u6a21\u5757\n&quot;&quot;&quot;\n\n# \u6a21\u5757\u7ea7\u53d8\u91cf\nMODULE_VAR = &quot;\u8fd9\u662f\u6a21\u5757\u7ea7\u53d8\u91cf&quot;\n\n# \u6a21\u5757\u7ea7\u51fd\u6570\ndef module_function():\n    &quot;&quot;&quot;\u6a21\u5757\u7ea7\u51fd\u6570&quot;&quot;&quot;\n    return &quot;\u8fd9\u662f\u6a21\u5757\u7ea7\u51fd\u6570\u7684\u8fd4\u56de\u503c&quot;\n\n# \u6a21\u5757\u7ea7\u7c7b\nclass ModuleClass:\n    &quot;&quot;&quot;\u6a21\u5757\u7ea7\u7c7b&quot;&quot;&quot;\n    def __init__(self, name):\n        self.name = name\n    \n    def get_name(self):\n        return self.name\n\n# 2. \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5305\n# \u76ee\u5f55\u7ed3\u6784:\n# mypackage\/\n#     __init__.py\n#     module1.py\n#     module2.py\n\n# \u6587\u4ef6\u540d: mypackage\/__init__.py\n&quot;&quot;&quot;\n\u8fd9\u662fmypackage\u5305\u7684\u521d\u59cb\u5316\u6587\u4ef6\n&quot;&quot;&quot;\n\n# \u5305\u7ea7\u53d8\u91cf\nPACKAGE_VAR = &quot;\u8fd9\u662f\u5305\u7ea7\u53d8\u91cf&quot;\n\n# \u4ece\u5b50\u6a21\u5757\u5bfc\u5165\nfrom . import module1\nfrom . import module2\n\n# \u6587\u4ef6\u540d: mypackage\/module1.py\n&quot;&quot;&quot;\n\u8fd9\u662fmypackage\u5305\u7684module1\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function1():\n    return &quot;module1\u7684\u51fd\u6570&quot;\n\n# \u6587\u4ef6\u540d: mypackage\/module2.py\n&quot;&quot;&quot;\n\u8fd9\u662fmypackage\u5305\u7684module2\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function2():\n    return &quot;module2\u7684\u51fd\u6570&quot;\n\n# 3. \u6d4b\u8bd5\u6a21\u5757\u548c\u5305\u7684\u5bfc\u5165\n# \u6587\u4ef6\u540d: test_import.py\n\n# \u5bfc\u5165\u6a21\u5757\nimport mymodule\n\n# \u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\nprint(&quot;\u6a21\u5757\u5bfc\u5165\u6d4b\u8bd5\uff1a&quot;)\nprint(f&quot;\u6a21\u5757\u7ea7\u53d8\u91cf: {mymodule.MODULE_VAR}&quot;)\nprint(f&quot;\u6a21\u5757\u7ea7\u51fd\u6570: {mymodule.module_function()}&quot;)\n\n# \u521b\u5efa\u6a21\u5757\u7c7b\u7684\u5b9e\u4f8b\nobj = mymodule.ModuleClass(&quot;\u6d4b\u8bd5&quot;)\nprint(f&quot;\u6a21\u5757\u7ea7\u7c7b: {obj.get_name()}&quot;)\n\n# \u5bfc\u5165\u5305\nimport mypackage\n\n# \u4f7f\u7528\u5305\u4e2d\u7684\u5185\u5bb9\nprint(&quot;\/n\u5305\u5bfc\u5165\u6d4b\u8bd5\uff1a&quot;)\nprint(f&quot;\u5305\u7ea7\u53d8\u91cf: {mypackage.PACKAGE_VAR}&quot;)\nprint(f&quot;module1\u51fd\u6570: {mypackage.module1.function1()}&quot;)\nprint(f&quot;module2\u51fd\u6570: {mypackage.module2.function2()}&quot;)\n\n# \u4ece\u5305\u4e2d\u5bfc\u5165\u7279\u5b9a\u6a21\u5757\nfrom mypackage import module1\nprint(f&quot;\/n\u76f4\u63a5\u5bfc\u5165module1: {module1.function1()}&quot;)\n\n# \u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u5185\u5bb9\nfrom mymodule import MODULE_VAR, module_function\nprint(f&quot;\/n\u76f4\u63a5\u5bfc\u5165\u6a21\u5757\u5185\u5bb9: {MODULE_VAR}, {module_function()}&quot;)<\/code><\/pre>\n<h3>2. Python\u7684\u5bfc\u5165\u673a\u5236<\/h3>\n<p>Python\u7684\u5bfc\u5165\u673a\u5236\u662f\u4e00\u4e2a\u590d\u6742\u4f46\u5f3a\u5927\u7684\u7cfb\u7edf\uff0c\u5b83\u8d1f\u8d23\u67e5\u627e\u3001\u52a0\u8f7d\u548c\u521d\u59cb\u5316\u6a21\u5757\u3002\u7406\u89e3\u5bfc\u5165\u673a\u5236\u5bf9\u4e8e\u638c\u63e1Python\u7f16\u7a0b\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>2.1 \u5bfc\u5165\u8bed\u53e5\u7684\u7c7b\u578b<\/h4>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5bfc\u5165\u8bed\u53e5\uff0c\u7528\u4e8e\u4e0d\u540c\u7684\u5bfc\u5165\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong><code>import module<\/code><\/strong>\uff1a\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/li>\n<li><strong><code>from module import name<\/code><\/strong>\uff1a\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u540d\u79f0<\/li>\n<li><strong><code>from module import *<\/code><\/strong>\uff1a\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u6240\u6709\u540d\u79f0\uff08\u4e0d\u63a8\u8350\uff09<\/li>\n<li><strong><code>import module as alias<\/code><\/strong>\uff1a\u5bfc\u5165\u6a21\u5757\u5e76\u4f7f\u7528\u522b\u540d<\/li>\n<li><strong><code>from module import name as alias<\/code><\/strong>\uff1a\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u540d\u79f0\u5e76\u4f7f\u7528\u522b\u540d<\/li>\n<\/ul>\n<h4>2.2 \u5bfc\u5165\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<p>Python\u7684\u5bfc\u5165\u673a\u5236\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u67e5\u627e\u6a21\u5757<\/strong>\uff1a\u6839\u636e\u5bfc\u5165\u8def\u5f84\u67e5\u627e\u6a21\u5757\u6587\u4ef6<\/li>\n<li><strong>\u52a0\u8f7d\u6a21\u5757<\/strong>\uff1a\u5c06\u6a21\u5757\u6587\u4ef6\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\u5e76\u52a0\u8f7d\u5230\u5185\u5b58<\/li>\n<li><strong>\u521d\u59cb\u5316\u6a21\u5757<\/strong>\uff1a\u6267\u884c\u6a21\u5757\u4e2d\u7684\u4ee3\u7801\uff0c\u521d\u59cb\u5316\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<li><strong>\u7f13\u5b58\u6a21\u5757<\/strong>\uff1a\u5c06\u6a21\u5757\u5bf9\u8c61\u7f13\u5b58\u5230<code>sys.modules<\/code>\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u5bfc\u5165<\/li>\n<\/ol>\n<h4>2.3 \u5bfc\u5165\u8def\u5f84<\/h4>\n<p>Python\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u4f1a\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u67e5\u627e\u6a21\u5757\uff1a<\/p>\n<ol>\n<li><strong>\u5f53\u524d\u76ee\u5f55<\/strong>\uff1a\u9996\u5148\u67e5\u627e\u5f53\u524d\u6267\u884c\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55<\/li>\n<li><strong><code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a\u67e5\u627e<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\u4e2d\u6307\u5b9a\u7684\u76ee\u5f55<\/li>\n<li><strong>\u6807\u51c6\u5e93\u76ee\u5f55<\/strong>\uff1a\u67e5\u627ePython\u6807\u51c6\u5e93\u6240\u5728\u7684\u76ee\u5f55<\/li>\n<li><strong>\u7b2c\u4e09\u65b9\u5e93\u76ee\u5f55<\/strong>\uff1a\u67e5\u627e\u901a\u8fc7pip\u7b49\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u76ee\u5f55<\/li>\n<li><strong><code>.pth<\/code>\u6587\u4ef6<\/strong>\uff1a\u67e5\u627e<code>.pth<\/code>\u6587\u4ef6\u4e2d\u6307\u5b9a\u7684\u76ee\u5f55<\/li>\n<\/ol>\n<pre><code class=\"python\"># \u5bfc\u5165\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406\u793a\u4f8b\nimport sys\nimport os\n\n# \u67e5\u770b\u5bfc\u5165\u8def\u5f84\nprint(&quot;Python\u5bfc\u5165\u8def\u5f84\uff1a&quot;)\nfor path in sys.path:\n    print(f&quot;  {path}&quot;)\n\n# \u67e5\u770b\u5df2\u5bfc\u5165\u7684\u6a21\u5757\nprint(&quot;\/n\u5df2\u5bfc\u5165\u7684\u6a21\u5757\uff1a&quot;)\nfor module_name in list(sys.modules.keys())[:20]:  # \u53ea\u663e\u793a\u524d20\u4e2a\n    print(f&quot;  {module_name}&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u5bfc\u5165\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u5bfc\u5165\uff1a&quot;)\n\n# \u5bfc\u5165\u4e00\u4e2a\u6807\u51c6\u5e93\u6a21\u5757\nimport math\nprint(f&quot;\u5bfc\u5165math\u6a21\u5757\uff1a{math}&quot;)\nprint(f&quot;math\u6a21\u5757\u8def\u5f84\uff1a{math.__file__}&quot;)\n\n# \u5bfc\u5165\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\uff08\u5982\u679c\u5df2\u5b89\u88c5\uff09\ntry:\n    import numpy\n    print(f&quot;\/n\u5bfc\u5165numpy\u6a21\u5757\uff1a{numpy}&quot;)\n    print(f&quot;numpy\u6a21\u5757\u8def\u5f84\uff1a{numpy.__file__}&quot;)\nexcept ImportError:\n    print(&quot;\/nnumpy\u6a21\u5757\u672a\u5b89\u88c5&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\uff1a&quot;)\nprint(f&quot;math\u6a21\u5757\u662f\u5426\u5728sys.modules\u4e2d\uff1a{'math' in sys.modules}&quot;)\n\n# \u5220\u9664\u6a21\u5757\u7f13\u5b58\u5e76\u91cd\u65b0\u5bfc\u5165\nif 'math' in sys.modules:\n    del sys.modules['math']\n    print(f&quot;\u5220\u9664math\u6a21\u5757\u7f13\u5b58\u540e\uff0c\u662f\u5426\u5728sys.modules\u4e2d\uff1a{'math' in sys.modules}&quot;)\n    \n    # \u91cd\u65b0\u5bfc\u5165\n    import math\n    print(f&quot;\u91cd\u65b0\u5bfc\u5165\u540e\uff0cmath\u6a21\u5757\uff1a{math}&quot;)\n\n# \u6d4b\u8bd5\u5bfc\u5165\u8def\u5f84\u7684\u4fee\u6539\nprint(&quot;\/n\u6d4b\u8bd5\u5bfc\u5165\u8def\u5f84\u7684\u4fee\u6539\uff1a&quot;)\n\n# \u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84\ncustom_path = os.path.join(os.getcwd(), &quot;custom_modules&quot;)\nsys.path.insert(0, custom_path)\nprint(f&quot;\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84\uff1a{custom_path}&quot;)\nprint(f&quot;\u81ea\u5b9a\u4e49\u8def\u5f84\u662f\u5426\u5728sys.path\u4e2d\uff1a{custom_path in sys.path}&quot;)\n\n# \u5c1d\u8bd5\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\ntry:\n    import custom_module\n    print(&quot;\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u6210\u529f&quot;)\nexcept ImportError:\n    print(&quot;\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u5931\u8d25\uff08\u81ea\u5b9a\u4e49\u6a21\u5757\u53ef\u80fd\u4e0d\u5b58\u5728\uff09&quot;)<\/code><\/pre>\n<h4>2.4 \u6a21\u5757\u7684\u52a0\u8f7d\u4e0e\u521d\u59cb\u5316<\/h4>\n<p>\u6a21\u5757\u7684\u52a0\u8f7d\u4e0e\u521d\u59cb\u5316\u8fc7\u7a0b\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u67e5\u627e\u6a21\u5757\u6587\u4ef6<\/strong>\uff1a\u6839\u636e\u5bfc\u5165\u8def\u5f84\u67e5\u627e\u6a21\u5757\u6587\u4ef6<\/li>\n<li><strong>\u7f16\u8bd1\u6a21\u5757<\/strong>\uff1a\u5c06\u6a21\u5757\u6587\u4ef6\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\uff08<code>.pyc<\/code>\u6587\u4ef6\uff09<\/li>\n<li><strong>\u521b\u5efa\u6a21\u5757\u5bf9\u8c61<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u5bf9\u8c61\uff0c\u5b58\u50a8\u5728<code>sys.modules<\/code>\u4e2d<\/li>\n<li><strong>\u6267\u884c\u6a21\u5757\u4ee3\u7801<\/strong>\uff1a\u6267\u884c\u6a21\u5757\u4e2d\u7684\u4ee3\u7801\uff0c\u521d\u59cb\u5316\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<li><strong>\u8fd4\u56de\u6a21\u5757\u5bf9\u8c61<\/strong>\uff1a\u5c06\u6a21\u5757\u5bf9\u8c61\u8fd4\u56de\u7ed9\u5bfc\u5165\u8005<\/li>\n<\/ol>\n<p>\u6a21\u5757\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u53ea\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\u6267\u884c\uff0c\u540e\u7eed\u7684\u5bfc\u5165\u4f1a\u76f4\u63a5\u4ece<code>sys.modules<\/code>\u4e2d\u83b7\u53d6\u5df2\u7f13\u5b58\u7684\u6a21\u5757\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"python\"># \u6a21\u5757\u7684\u52a0\u8f7d\u4e0e\u521d\u59cb\u5316\u793a\u4f8b\n\n# \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6a21\u5757\n# \u6587\u4ef6\u540d: test_module.py\n\nprint(&quot;test_module\u6a21\u5757\u521d\u59cb\u5316\u5f00\u59cb&quot;)\n\n# \u6a21\u5757\u7ea7\u53d8\u91cf\nMODULE_VAR = &quot;\u6a21\u5757\u53d8\u91cf&quot;\n\n# \u6a21\u5757\u7ea7\u51fd\u6570\ndef module_function():\n    return &quot;\u6a21\u5757\u51fd\u6570&quot;\n\n# \u6a21\u5757\u521d\u59cb\u5316\u4ee3\u7801\nprint(&quot;test_module\u6a21\u5757\u521d\u59cb\u5316\u4e2d&quot;)\nprint(f&quot;\u6a21\u5757\u53d8\u91cf\u503c: {MODULE_VAR}&quot;)\nprint(&quot;test_module\u6a21\u5757\u521d\u59cb\u5316\u5b8c\u6210&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u7684\u52a0\u8f7d\u4e0e\u521d\u59cb\u5316\n# \u6587\u4ef6\u540d: test_module_load.py\n\nimport sys\n\nprint(&quot;\u7b2c\u4e00\u6b21\u5bfc\u5165\u6a21\u5757\uff1a&quot;)\nimport test_module\n\nprint(&quot;\/n\u7b2c\u4e8c\u6b21\u5bfc\u5165\u6a21\u5757\uff1a&quot;)\nimport test_module  # \u4f1a\u4f7f\u7528\u7f13\u5b58\u7684\u6a21\u5757\n\nprint(&quot;\/n\u4f7f\u7528\u6a21\u5757\u5185\u5bb9\uff1a&quot;)\nprint(f&quot;\u6a21\u5757\u53d8\u91cf: {test_module.MODULE_VAR}&quot;)\nprint(f&quot;\u6a21\u5757\u51fd\u6570: {test_module.module_function()}&quot;)\n\n# \u6d4b\u8bd5\u5220\u9664\u6a21\u5757\u7f13\u5b58\u540e\u91cd\u65b0\u5bfc\u5165\nprint(&quot;\/n\u5220\u9664\u6a21\u5757\u7f13\u5b58\u540e\u91cd\u65b0\u5bfc\u5165\uff1a&quot;)\nif 'test_module' in sys.modules:\n    del sys.modules['test_module']\n    import test_module  # \u4f1a\u91cd\u65b0\u521d\u59cb\u5316\u6a21\u5757\n\n# \u6d4b\u8bd5\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u5185\u5bb9\nprint(&quot;\/n\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u5185\u5bb9\uff1a&quot;)\nfrom test_module import MODULE_VAR, module_function\nprint(f&quot;\u5bfc\u5165\u7684\u53d8\u91cf: {MODULE_VAR}&quot;)\nprint(f&quot;\u5bfc\u5165\u7684\u51fd\u6570: {module_function()}&quot;)<\/code><\/pre>\n<h3>3. \u5305\u7ba1\u7406\u7cfb\u7edf<\/h3>\n<p>Python\u7684\u5305\u7ba1\u7406\u7cfb\u7edf\u662f\u4e00\u4e2a\u7528\u4e8e\u5b89\u88c5\u3001\u5347\u7ea7\u3001\u5378\u8f7d\u548c\u7ba1\u7406Python\u5305\u7684\u5de5\u5177\u96c6\u5408\u3002\u7406\u89e3\u5305\u7ba1\u7406\u7cfb\u7edf\u5bf9\u4e8ePython\u5f00\u53d1\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>3.1 \u5305\u7ba1\u7406\u5de5\u5177<\/h4>\n<p>Python\u7684\u4e3b\u8981\u5305\u7ba1\u7406\u5de5\u5177\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>pip<\/strong>\uff1aPython\u7684\u9ed8\u8ba4\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305<\/li>\n<li><strong>conda<\/strong>\uff1aAnaconda\u53d1\u884c\u7248\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u652f\u6301Python\u5305\u548c\u975ePython\u5305<\/li>\n<li><strong>poetry<\/strong>\uff1a\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684Python\u4f9d\u8d56\u7ba1\u7406\u548c\u6253\u5305\u5de5\u5177<\/li>\n<li><strong>pipenv<\/strong>\uff1a\u4e00\u4e2a\u7ed3\u5408\u4e86pip\u548cvirtualenv\u529f\u80fd\u7684\u5305\u7ba1\u7406\u5de5\u5177<\/li>\n<\/ul>\n<h4>3.2 pip\u7684\u4f7f\u7528<\/h4>\n<p>pip\u662fPython\u6700\u5e38\u7528\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u5305<\/strong>\uff1a<code>pip install package_name<\/code><\/li>\n<li><strong>\u5347\u7ea7\u5305<\/strong>\uff1a<code>pip install --upgrade package_name<\/code><\/li>\n<li><strong>\u5378\u8f7d\u5305<\/strong>\uff1a<code>pip uninstall package_name<\/code><\/li>\n<li><strong>\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305<\/strong>\uff1a<code>pip list<\/code><\/li>\n<li><strong>\u67e5\u770b\u5305\u7684\u4fe1\u606f<\/strong>\uff1a<code>pip show package_name<\/code><\/li>\n<li><strong>\u641c\u7d22\u5305<\/strong>\uff1a<code>pip search package_name<\/code><\/li>\n<li><strong>\u5bfc\u51fa\u4f9d\u8d56<\/strong>\uff1a<code>pip freeze &gt; requirements.txt<\/code><\/li>\n<li><strong>\u5b89\u88c5\u4f9d\u8d56<\/strong>\uff1a<code>pip install -r requirements.txt<\/code><\/li>\n<\/ul>\n<h4>3.3 \u865a\u62df\u73af\u5883<\/h4>\n<p>\u865a\u62df\u73af\u5883\u662f\u4e00\u4e2a\u9694\u79bb\u7684Python\u73af\u5883\uff0c\u5b83\u5141\u8bb8\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684\u5305\uff0c\u907f\u514d\u5305\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<p>Python\u7684\u4e3b\u8981\u865a\u62df\u73af\u5883\u5de5\u5177\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>venv<\/strong>\uff1aPython 3.3+\u5185\u7f6e\u7684\u865a\u62df\u73af\u5883\u5de5\u5177<\/li>\n<li><strong>virtualenv<\/strong>\uff1a\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7684\u865a\u62df\u73af\u5883\u5de5\u5177\uff0c\u652f\u6301Python 2\u548cPython 3<\/li>\n<li><strong>conda<\/strong>\uff1aAnaconda\u53d1\u884c\u7248\u7684\u865a\u62df\u73af\u5883\u5de5\u5177<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5305\u7ba1\u7406\u7cfb\u7edf\u793a\u4f8b\n\n# 1. \u4f7f\u7528pip\u7ba1\u7406\u5305\n# \u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\n\n# \u5b89\u88c5\u5305\n# pip install requests\n\n# \u5347\u7ea7\u5305\n# pip install --upgrade requests\n\n# \u5378\u8f7d\u5305\n# pip uninstall requests\n\n# \u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305\n# pip list\n\n# \u67e5\u770b\u5305\u7684\u4fe1\u606f\n# pip show requests\n\n# \u5bfc\u51fa\u4f9d\u8d56\n# pip freeze &gt; requirements.txt\n\n# \u5b89\u88c5\u4f9d\u8d56\n# pip install -r requirements.txt\n\n# 2. \u4f7f\u7528\u865a\u62df\u73af\u5883\n# \u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\n\n# \u521b\u5efa\u865a\u62df\u73af\u5883\n# python -m venv venv\n\n# \u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff08Windows\uff09\n# venv\/Scripts\/activate\n\n# \u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff08Linux\/Mac\uff09\n# source venv\/bin\/activate\n\n# \u9000\u51fa\u865a\u62df\u73af\u5883\n# deactivate\n\n# 3. \u6d4b\u8bd5\u865a\u62df\u73af\u5883\n# \u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\n\nimport sys\nimport os\n\nprint(&quot;Python\u89e3\u91ca\u5668\u8def\u5f84\uff1a&quot;)\nprint(f&quot;  {sys.executable}&quot;)\n\nprint(&quot;\/n\u865a\u62df\u73af\u5883\u8def\u5f84\uff1a&quot;)\nvenv_path = os.path.dirname(os.path.dirname(sys.executable))\nprint(f&quot;  {venv_path}&quot;)\n\nprint(&quot;\/n\u6d4b\u8bd5\u5305\u5b89\u88c5\uff1a&quot;)\ntry:\n    import requests\n    print(&quot;requests\u6a21\u5757\u5df2\u5b89\u88c5&quot;)\nexcept ImportError:\n    print(&quot;requests\u6a21\u5757\u672a\u5b89\u88c5&quot;)\n\n# 4. \u4f7f\u7528poetry\u7ba1\u7406\u5305\n# \u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\n\n# \u5b89\u88c5poetry\n# pip install poetry\n\n# \u521d\u59cb\u5316\u9879\u76ee\n# poetry init\n\n# \u5b89\u88c5\u5305\n# poetry add requests\n\n# \u5b89\u88c5\u5f00\u53d1\u4f9d\u8d56\n# poetry add --dev pytest\n\n# \u67e5\u770b\u4f9d\u8d56\n# poetry show\n\n# \u8fd0\u884c\u547d\u4ee4\n# poetry run python script.py<\/code><\/pre>\n<h3>4. \u5bfc\u5165\u8def\u5f84\u4e0e\u6a21\u5757\u67e5\u627e<\/h3>\n<p>\u7406\u89e3Python\u7684\u5bfc\u5165\u8def\u5f84\u548c\u6a21\u5757\u67e5\u627e\u673a\u5236\u5bf9\u4e8e\u89e3\u51b3\u5bfc\u5165\u95ee\u9898\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>4.1 \u5bfc\u5165\u8def\u5f84\u7684\u7ec4\u6210<\/h4>\n<p>Python\u7684\u5bfc\u5165\u8def\u5f84\u7531\u4ee5\u4e0b\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li><strong>\u5f53\u524d\u76ee\u5f55<\/strong>\uff1a<code>''<\/code>\uff0c\u8868\u793a\u5f53\u524d\u6267\u884c\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55<\/li>\n<li><strong><code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf<\/strong>\uff1a\u7528\u6237\u8bbe\u7f6e\u7684Python\u5bfc\u5165\u8def\u5f84<\/li>\n<li><strong>\u6807\u51c6\u5e93\u76ee\u5f55<\/strong>\uff1aPython\u6807\u51c6\u5e93\u6240\u5728\u7684\u76ee\u5f55<\/li>\n<li><strong>\u7b2c\u4e09\u65b9\u5e93\u76ee\u5f55<\/strong>\uff1a\u901a\u8fc7pip\u7b49\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\u76ee\u5f55<\/li>\n<li><strong><code>.pth<\/code>\u6587\u4ef6<\/strong>\uff1a\u5305\u542b\u989d\u5916\u5bfc\u5165\u8def\u5f84\u7684\u6587\u4ef6<\/li>\n<\/ol>\n<h4>4.2 \u6a21\u5757\u67e5\u627e\u7684\u987a\u5e8f<\/h4>\n<p>Python\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u4f1a\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u67e5\u627e\uff1a<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u6a21\u5757<\/strong>\uff1a\u9996\u5148\u67e5\u627e\u5185\u7f6e\u6a21\u5757\uff0c\u5982<code>math<\/code>\u3001<code>sys<\/code>\u7b49<\/li>\n<li><strong><code>sys.modules<\/code>\u7f13\u5b58<\/strong>\uff1a\u67e5\u627e\u5df2\u5bfc\u5165\u7684\u6a21\u5757\u7f13\u5b58<\/li>\n<li><strong>\u5bfc\u5165\u8def\u5f84<\/strong>\uff1a\u6309\u7167<code>sys.path<\/code>\u4e2d\u7684\u987a\u5e8f\u67e5\u627e\u6a21\u5757\u6587\u4ef6<\/li>\n<\/ol>\n<h4>4.3 \u6a21\u5757\u6587\u4ef6\u7684\u7c7b\u578b<\/h4>\n<p>Python\u53ef\u4ee5\u5bfc\u5165\u591a\u79cd\u7c7b\u578b\u7684\u6a21\u5757\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li><strong><code>.py<\/code>\u6587\u4ef6<\/strong>\uff1aPython\u6e90\u4ee3\u7801\u6587\u4ef6<\/li>\n<li><strong><code>.pyc<\/code>\u6587\u4ef6<\/strong>\uff1aPython\u5b57\u8282\u7801\u6587\u4ef6<\/li>\n<li><strong><code>.pyo<\/code>\u6587\u4ef6<\/strong>\uff1a\u4f18\u5316\u7684Python\u5b57\u8282\u7801\u6587\u4ef6<\/li>\n<li><strong><code>.so<\/code>\/<code>.dll<\/code>\u6587\u4ef6<\/strong>\uff1aC\u6269\u5c55\u6a21\u5757<\/li>\n<li><strong>\u76ee\u5f55<\/strong>\uff1a\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff08\u5305\uff09<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5bfc\u5165\u8def\u5f84\u4e0e\u6a21\u5757\u67e5\u627e\u793a\u4f8b\nimport sys\nimport os\nimport importlib\n\n# \u67e5\u770b\u5bfc\u5165\u8def\u5f84\nprint(&quot;Python\u5bfc\u5165\u8def\u5f84\uff1a&quot;)\nfor i, path in enumerate(sys.path):\n    print(f&quot;  {i}: {path}&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u67e5\u627e\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u67e5\u627e\uff1a&quot;)\n\n# \u67e5\u627e\u5185\u7f6e\u6a21\u5757\nprint(&quot;\u67e5\u627e\u5185\u7f6e\u6a21\u5757 'math'\uff1a&quot;)\nprint(f&quot;'math' in sys.builtin_module_names: {'math' in sys.builtin_module_names}&quot;)\n\n# \u67e5\u627e\u6807\u51c6\u5e93\u6a21\u5757\nprint(&quot;\/n\u67e5\u627e\u6807\u51c6\u5e93\u6a21\u5757 'os'\uff1a&quot;)\nimport os\nprint(f&quot;os\u6a21\u5757\u8def\u5f84\uff1a{os.__file__}&quot;)\n\n# \u67e5\u627e\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\uff08\u5982\u679c\u5df2\u5b89\u88c5\uff09\ntry:\n    import numpy\n    print(&quot;\/n\u67e5\u627e\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757 'numpy'\uff1a&quot;)\n    print(f&quot;numpy\u6a21\u5757\u8def\u5f84\uff1a{numpy.__file__}&quot;)\nexcept ImportError:\n    print(&quot;\/nnumpy\u6a21\u5757\u672a\u5b89\u88c5&quot;)\n\n# \u6d4b\u8bd5\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u67e5\u627e\nprint(&quot;\/n\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u67e5\u627e\uff1a&quot;)\n\n# \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6a21\u5757\u6587\u4ef6\nmodule_content = '''\ndef test_function():\n    return &quot;\u6d4b\u8bd5\u51fd\u6570&quot;\n'''\n\n# \u5199\u5165\u4e34\u65f6\u6a21\u5757\u6587\u4ef6\nwith open(&quot;temp_module.py&quot;, &quot;w&quot;) as f:\n    f.write(module_content)\n\n# \u5bfc\u5165\u4e34\u65f6\u6a21\u5757\ntry:\n    import temp_module\n    print(&quot;\u6210\u529f\u5bfc\u5165\u4e34\u65f6\u6a21\u5757&quot;)\n    print(f&quot;\u4e34\u65f6\u6a21\u5757\u8def\u5f84\uff1a{temp_module.__file__}&quot;)\n    print(f&quot;\u6d4b\u8bd5\u51fd\u6570\u8fd4\u56de\u503c\uff1a{temp_module.test_function()}&quot;)\nexcept ImportError as e:\n    print(f&quot;\u5bfc\u5165\u4e34\u65f6\u6a21\u5757\u5931\u8d25\uff1a{e}&quot;)\n\n# \u6e05\u7406\u4e34\u65f6\u6a21\u5757\nif 'temp_module' in sys.modules:\n    del sys.modules['temp_module']\n\nif os.path.exists(&quot;temp_module.py&quot;):\n    os.remove(&quot;temp_module.py&quot;)\n\nif os.path.exists(&quot;temp_module.pyc&quot;):\n    os.remove(&quot;temp_module.pyc&quot;)\n\n# \u6d4b\u8bd5\u5bfc\u5165\u8def\u5f84\u7684\u4fee\u6539\nprint(&quot;\/n\u6d4b\u8bd5\u5bfc\u5165\u8def\u5f84\u7684\u4fee\u6539\uff1a&quot;)\n\n# \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u76ee\u5f55\nif not os.path.exists(&quot;test_modules&quot;):\n    os.makedirs(&quot;test_modules&quot;)\n\n# \u5728\u4e34\u65f6\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u6587\u4ef6\nwith open(&quot;test_modules\/my_module.py&quot;, &quot;w&quot;) as f:\n    f.write('def my_function(): return &quot;\u6211\u7684\u51fd\u6570&quot;')\n\n# \u6dfb\u52a0\u4e34\u65f6\u76ee\u5f55\u5230\u5bfc\u5165\u8def\u5f84\nsys.path.insert(0, &quot;test_modules&quot;)\nprint(&quot;\u6dfb\u52a0\u4e34\u65f6\u76ee\u5f55\u5230\u5bfc\u5165\u8def\u5f84&quot;)\n\n# \u5bfc\u5165\u6a21\u5757\ntry:\n    import my_module\n    print(&quot;\u6210\u529f\u5bfc\u5165my_module\u6a21\u5757&quot;)\n    print(f&quot;my_function\u8fd4\u56de\u503c\uff1a{my_module.my_function()}&quot;)\nexcept ImportError as e:\n    print(f&quot;\u5bfc\u5165my_module\u6a21\u5757\u5931\u8d25\uff1a{e}&quot;)\n\n# \u6e05\u7406\nif 'my_module' in sys.modules:\n    del sys.modules['my_module']\n\nimport shutil\nif os.path.exists(&quot;test_modules&quot;):\n    shutil.rmtree(&quot;test_modules&quot;)<\/code><\/pre>\n<h3>5. \u76f8\u5bf9\u5bfc\u5165\u4e0e\u7edd\u5bf9\u5bfc\u5165<\/h3>\n<p>Python\u652f\u6301\u4e24\u79cd\u5bfc\u5165\u65b9\u5f0f\uff1a\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u3002\u7406\u89e3\u8fd9\u4e24\u79cd\u5bfc\u5165\u65b9\u5f0f\u7684\u533a\u522b\u5bf9\u4e8e\u6b63\u786e\u7ec4\u7ec7\u5305\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>5.1 \u7edd\u5bf9\u5bfc\u5165<\/h4>\n<p>\u7edd\u5bf9\u5bfc\u5165\u662f\u6307\u4ece\u5305\u7684\u6839\u76ee\u5f55\u5f00\u59cb\u7684\u5bfc\u5165\uff0c\u4f7f\u7528\u5b8c\u6574\u7684\u5305\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"python\">from package.module import function\nimport package.module<\/code><\/pre>\n<p>\u7edd\u5bf9\u5bfc\u5165\u7684\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u660e\u786e\u6027<\/strong>\uff1a\u5bfc\u5165\u8def\u5f84\u6e05\u6670\u660e\u786e\uff0c\u6613\u4e8e\u7406\u89e3<\/li>\n<li><strong>\u907f\u514d\u51b2\u7a81<\/strong>\uff1a\u907f\u514d\u4e0e\u6807\u51c6\u5e93\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\u7684\u547d\u540d\u51b2\u7a81<\/li>\n<li><strong>\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u5f53\u5305\u7ed3\u6784\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u7edd\u5bf9\u5bfc\u5165\u66f4\u5bb9\u6613\u8c03\u6574<\/li>\n<\/ul>\n<h4>5.2 \u76f8\u5bf9\u5bfc\u5165<\/h4>\n<p>\u76f8\u5bf9\u5bfc\u5165\u662f\u6307\u4ece\u5f53\u524d\u5305\u5f00\u59cb\u7684\u5bfc\u5165\uff0c\u4f7f\u7528\u70b9\u53f7\u8868\u793a\u76f8\u5bf9\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"python\">from . import module  # \u5bfc\u5165\u540c\u7ea7\u6a21\u5757\nfrom .module import function  # \u5bfc\u5165\u540c\u7ea7\u6a21\u5757\u4e2d\u7684\u51fd\u6570\nfrom .. import module  # \u5bfc\u5165\u7236\u7ea7\u5305\u4e2d\u7684\u6a21\u5757\nfrom ..module import function  # \u5bfc\u5165\u7236\u7ea7\u5305\u4e2d\u7684\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/code><\/pre>\n<p>\u76f8\u5bf9\u5bfc\u5165\u7684\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u5f53\u5305\u7684\u540d\u79f0\u6216\u4f4d\u7f6e\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u76f8\u5bf9\u5bfc\u5165\u4e0d\u9700\u8981\u4fee\u6539<\/li>\n<li><strong>\u7b80\u6d01\u6027<\/strong>\uff1a\u5bf9\u4e8e\u5305\u5185\u90e8\u7684\u6a21\u5757\u5bfc\u5165\uff0c\u76f8\u5bf9\u5bfc\u5165\u66f4\u7b80\u6d01<\/li>\n<\/ul>\n<h4>5.3 \u76f8\u5bf9\u5bfc\u5165\u4e0e\u7edd\u5bf9\u5bfc\u5165\u7684\u9009\u62e9<\/h4>\n<p>\u5728\u9009\u62e9\u76f8\u5bf9\u5bfc\u5165\u8fd8\u662f\u7edd\u5bf9\u5bfc\u5165\u65f6\uff0c\u5e94\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<ul>\n<li><strong>\u5305\u5185\u90e8\u5bfc\u5165<\/strong>\uff1a\u5bf9\u4e8e\u5305\u5185\u90e8\u7684\u6a21\u5757\u5bfc\u5165\uff0c\u76f8\u5bf9\u5bfc\u5165\u66f4\u7b80\u6d01<\/li>\n<li><strong>\u8de8\u5305\u5bfc\u5165<\/strong>\uff1a\u5bf9\u4e8e\u8de8\u5305\u7684\u6a21\u5757\u5bfc\u5165\uff0c\u7edd\u5bf9\u5bfc\u5165\u66f4\u660e\u786e<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5982\u679c\u5305\u7ed3\u6784\u8f83\u590d\u6742\uff0c\u7edd\u5bf9\u5bfc\u5165\u53ef\u80fd\u66f4\u6613\u8bfb<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u5728Python 3\u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u76f8\u5bf9\u5bfc\u5165\u4e0e\u7edd\u5bf9\u5bfc\u5165\u793a\u4f8b\n\n# \u5305\u7ed3\u6784\uff1a\n# mypackage\/\n#     __init__.py\n#     module1.py\n#     module2.py\n#     subpackage\/\n#         __init__.py\n#         submodule.py\n\n# \u6587\u4ef6\u540d: mypackage\/__init__.py\n&quot;&quot;&quot;\nmypackage\u5305\u7684\u521d\u59cb\u5316\u6587\u4ef6\n&quot;&quot;&quot;\n\n# \u7edd\u5bf9\u5bfc\u5165\nimport mypackage.module1\nimport mypackage.module2\n\n# \u76f8\u5bf9\u5bfc\u5165\nfrom . import module1\nfrom . import module2\n\n# \u6587\u4ef6\u540d: mypackage\/module1.py\n&quot;&quot;&quot;\nmodule1\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function1():\n    return &quot;module1\u7684\u51fd\u6570&quot;\n\n# \u5bfc\u5165\u540c\u7ea7\u6a21\u5757\nfrom . import module2\nprint(f&quot;module1\u5bfc\u5165module2: {module2.function2()}&quot;)\n\n# \u6587\u4ef6\u540d: mypackage\/module2.py\n&quot;&quot;&quot;\nmodule2\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function2():\n    return &quot;module2\u7684\u51fd\u6570&quot;\n\n# \u6587\u4ef6\u540d: mypackage\/subpackage\/__init__.py\n&quot;&quot;&quot;\nsubpackage\u5305\u7684\u521d\u59cb\u5316\u6587\u4ef6\n&quot;&quot;&quot;\n\n# \u5bfc\u5165\u7236\u7ea7\u5305\u4e2d\u7684\u6a21\u5757\nfrom .. import module1\nprint(f&quot;subpackage\u5bfc\u5165module1: {module1.function1()}&quot;)\n\n# \u6587\u4ef6\u540d: mypackage\/subpackage\/submodule.py\n&quot;&quot;&quot;\nsubmodule\u6a21\u5757\n&quot;&quot;&quot;\n\ndef sub_function():\n    return &quot;submodule\u7684\u51fd\u6570&quot;\n\n# \u5bfc\u5165\u7236\u7ea7\u5305\u4e2d\u7684\u6a21\u5757\nfrom .. import module1\nprint(f&quot;submodule\u5bfc\u5165module1: {module1.function1()}&quot;)\n\n# \u5bfc\u5165\u540c\u7ea7\u6a21\u5757\nfrom . import other_module  # \u5047\u8bbe\u5b58\u5728other_module\u6a21\u5757\n\n# \u6d4b\u8bd5\u5bfc\u5165\n# \u6587\u4ef6\u540d: test_imports.py\n\n# \u7edd\u5bf9\u5bfc\u5165\nimport mypackage\nprint(f&quot;\u7edd\u5bf9\u5bfc\u5165mypackage: {mypackage}&quot;)\n\nfrom mypackage import module1\nprint(f&quot;\u7edd\u5bf9\u5bfc\u5165module1: {module1.function1()}&quot;)\n\nfrom mypackage.subpackage import submodule\nprint(f&quot;\u7edd\u5bf9\u5bfc\u5165submodule: {submodule.sub_function()}&quot;)\n\n# \u6d4b\u8bd5\u76f8\u5bf9\u5bfc\u5165\u7684\u9650\u5236\nprint(&quot;\/n\u76f8\u5bf9\u5bfc\u5165\u7684\u9650\u5236\uff1a&quot;)\nprint(&quot;\u76f8\u5bf9\u5bfc\u5165\u53ea\u80fd\u5728\u5305\u5185\u90e8\u4f7f\u7528\uff0c\u4e0d\u80fd\u5728\u811a\u672c\u4e2d\u76f4\u63a5\u4f7f\u7528&quot;)<\/code><\/pre>\n<h3>6. \u6a21\u5757\u7f13\u5b58\u4e0e\u91cd\u8f7d<\/h3>\n<p>Python\u4f1a\u7f13\u5b58\u5df2\u5bfc\u5165\u7684\u6a21\u5757\uff0c\u4ee5\u63d0\u9ad8\u5bfc\u5165\u6548\u7387\u3002\u7406\u89e3\u6a21\u5757\u7f13\u5b58\u548c\u91cd\u8f7d\u673a\u5236\u5bf9\u4e8e\u5f00\u53d1\u548c\u8c03\u8bd5\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h4>6.1 \u6a21\u5757\u7f13\u5b58<\/h4>\n<p>\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0cPython\u4f1a\u5c06\u6a21\u5757\u5bf9\u8c61\u7f13\u5b58\u5230<code>sys.modules<\/code>\u5b57\u5178\u4e2d\u3002\u540e\u7eed\u7684\u5bfc\u5165\u4f1a\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u6a21\u5757\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u91cd\u65b0\u52a0\u8f7d\u548c\u521d\u59cb\u5316\u6a21\u5757\u3002<\/p>\n<p>\u6a21\u5757\u7f13\u5b58\u7684\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd<\/strong>\uff1a\u907f\u514d\u91cd\u590d\u52a0\u8f7d\u548c\u521d\u59cb\u5316\u6a21\u5757<\/li>\n<li><strong>\u4fdd\u6301\u72b6\u6001<\/strong>\uff1a\u6a21\u5757\u7684\u72b6\u6001\u5728\u591a\u6b21\u5bfc\u5165\u4e4b\u95f4\u4fdd\u6301\u4e00\u81f4<\/li>\n<\/ul>\n<h4>6.2 \u6a21\u5757\u91cd\u8f7d<\/h4>\n<p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4fee\u6539\u6a21\u5757\u4ee3\u7801\u540e\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002Python\u63d0\u4f9b\u4e86<code>importlib.reload()<\/code>\u51fd\u6570\u6765\u91cd\u8f7d\u6a21\u5757\u3002<\/p>\n<p>\u6a21\u5757\u91cd\u8f7d\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li><strong>\u53ea\u91cd\u8f7d\u6a21\u5757\u672c\u8eab<\/strong>\uff1a<code>reload()<\/code>\u53ea\u91cd\u8f7d\u6a21\u5757\u672c\u8eab\uff0c\u4e0d\u4f1a\u91cd\u8f7d\u6a21\u5757\u5bfc\u5165\u7684\u5176\u4ed6\u6a21\u5757<\/li>\n<li><strong>\u4fdd\u6301\u6a21\u5757\u5bf9\u8c61<\/strong>\uff1a<code>reload()<\/code>\u4f1a\u91cd\u7528\u73b0\u6709\u7684\u6a21\u5757\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u65b0\u7684\u6a21\u5757\u5bf9\u8c61<\/li>\n<li><strong>\u66f4\u65b0\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a<code>reload()<\/code>\u4f1a\u66f4\u65b0\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u4e0d\u4f1a\u66f4\u65b0\u5df2\u5bfc\u5165\u7684\u540d\u79f0<\/li>\n<li><strong>\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898<\/strong>\uff1a\u91cd\u8f7d\u6a21\u5757\u53ef\u80fd\u4f1a\u5bfc\u81f4\u72b6\u6001\u4e0d\u4e00\u81f4\uff0c\u5e94\u8c28\u614e\u4f7f\u7528<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u6a21\u5757\u7f13\u5b58\u4e0e\u91cd\u8f7d\u793a\u4f8b\nimport sys\nimport importlib\nimport os\n\n# \u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6a21\u5757\nmodule_content = '''\n# \u6a21\u5757\u7ea7\u53d8\u91cf\ncounter = 0\n\n# \u6a21\u5757\u7ea7\u51fd\u6570\ndef increment():\n    global counter\n    counter += 1\n    return counter\n\nprint(f&quot;\u6a21\u5757\u521d\u59cb\u5316\uff0ccounter={counter}&quot;)\n'''\n\n# \u5199\u5165\u6d4b\u8bd5\u6a21\u5757\u6587\u4ef6\nwith open(&quot;reload_test.py&quot;, &quot;w&quot;) as f:\n    f.write(module_content)\n\n# \u7b2c\u4e00\u6b21\u5bfc\u5165\u6a21\u5757\nprint(&quot;\u7b2c\u4e00\u6b21\u5bfc\u5165\u6a21\u5757\uff1a&quot;)\nimport reload_test\nprint(f&quot;counter\u521d\u59cb\u503c: {reload_test.counter}&quot;)\nprint(f&quot;\u8c03\u7528increment(): {reload_test.increment()}&quot;)\nprint(f&quot;counter\u503c: {reload_test.counter}&quot;)\n\n# \u4fee\u6539\u6a21\u5757\u4ee3\u7801\nprint(&quot;\/n\u4fee\u6539\u6a21\u5757\u4ee3\u7801\uff1a&quot;)\nnew_module_content = '''\n# \u6a21\u5757\u7ea7\u53d8\u91cf\ncounter = 100\n\n# \u6a21\u5757\u7ea7\u51fd\u6570\ndef increment():\n    global counter\n    counter += 1\n    return counter\n\n# \u65b0\u589e\u51fd\u6570\ndef reset():\n    global counter\n    counter = 0\n    return counter\n\nprint(f&quot;\u6a21\u5757\u521d\u59cb\u5316\uff0ccounter={counter}&quot;)\n'''\n\nwith open(&quot;reload_test.py&quot;, &quot;w&quot;) as f:\n    f.write(new_module_content)\n\n# \u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\uff1a&quot;)\nprint(f&quot;counter\u503c\uff08\u4f7f\u7528\u7f13\u5b58\uff09: {reload_test.counter}&quot;)\nprint(f&quot;\u8c03\u7528increment(): {reload_test.increment()}&quot;)\nprint(f&quot;counter\u503c: {reload_test.counter}&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u91cd\u8f7d\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u91cd\u8f7d\uff1a&quot;)\nimportlib.reload(reload_test)\nprint(f&quot;counter\u503c\uff08\u91cd\u8f7d\u540e\uff09: {reload_test.counter}&quot;)\nprint(f&quot;\u8c03\u7528increment(): {reload_test.increment()}&quot;)\nprint(f&quot;counter\u503c: {reload_test.counter}&quot;)\n\n# \u6d4b\u8bd5\u65b0\u589e\u7684\u51fd\u6570\nprint(&quot;\/n\u6d4b\u8bd5\u65b0\u589e\u7684\u51fd\u6570\uff1a&quot;)\nprint(f&quot;\u8c03\u7528reset(): {reload_test.reset()}&quot;)\nprint(f&quot;counter\u503c: {reload_test.counter}&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\u7684\u5220\u9664\nprint(&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u7f13\u5b58\u7684\u5220\u9664\uff1a&quot;)\nif 'reload_test' in sys.modules:\n    del sys.modules['reload_test']\n    print(&quot;\u5220\u9664\u6a21\u5757\u7f13\u5b58&quot;)\n\n# \u91cd\u65b0\u5bfc\u5165\u6a21\u5757\nimport reload_test\nprint(f&quot;counter\u503c\uff08\u91cd\u65b0\u5bfc\u5165\uff09: {reload_test.counter}&quot;)\n\n# \u6e05\u7406\nif 'reload_test' in sys.modules:\n    del sys.modules['reload_test']\n\nif os.path.exists(&quot;reload_test.py&quot;):\n    os.remove(&quot;reload_test.py&quot;)\n\nif os.path.exists(&quot;reload_test.pyc&quot;):\n    os.remove(&quot;reload_test.pyc&quot;)<\/code><\/pre>\n<h3>7. \u5305\u7684\u521d\u59cb\u5316\u4e0e\u547d\u540d\u7a7a\u95f4<\/h3>\n<p>\u5305\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u548c\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u662fPython\u5305\u7cfb\u7edf\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u5bf9\u4e8e\u6b63\u786e\u4f7f\u7528\u548c\u521b\u5efa\u5305\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h4>7.1 \u5305\u7684\u521d\u59cb\u5316<\/h4>\n<p>\u5f53\u5305\u88ab\u5bfc\u5165\u65f6\uff0cPython\u4f1a\u6267\u884c\u5305\u7684<code>__init__.py<\/code>\u6587\u4ef6\uff08\u5982\u679c\u5b58\u5728\uff09\u3002<code>__init__.py<\/code>\u6587\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li><strong>\u5305\u7684\u521d\u59cb\u5316<\/strong>\uff1a\u6267\u884c\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801<\/li>\n<li><strong>\u5bfc\u51fa\u6a21\u5757<\/strong>\uff1a\u4ece\u5305\u4e2d\u5bfc\u51fa\u6a21\u5757\u6216\u540d\u79f0<\/li>\n<li><strong>\u8bbe\u7f6e\u5305\u7ea7\u53d8\u91cf<\/strong>\uff1a\u5b9a\u4e49\u5305\u7ea7\u522b\u7684\u53d8\u91cf\u548c\u5e38\u91cf<\/li>\n<li><strong>\u63a7\u5236\u5bfc\u5165\u884c\u4e3a<\/strong>\uff1a\u63a7\u5236\u5305\u7684\u5bfc\u5165\u884c\u4e3a<\/li>\n<\/ul>\n<h4>7.2 \u5305\u7684\u547d\u540d\u7a7a\u95f4<\/h4>\n<p>\u5305\u7684\u547d\u540d\u7a7a\u95f4\u662f\u901a\u8fc7\u5305\u7684\u5c42\u6b21\u7ed3\u6784\u548c<code>__init__.py<\/code>\u6587\u4ef6\u6765\u7ba1\u7406\u7684\u3002\u7406\u89e3\u5305\u7684\u547d\u540d\u7a7a\u95f4\u5bf9\u4e8e\u907f\u514d\u547d\u540d\u51b2\u7a81\u548c\u6b63\u786e\u7ec4\u7ec7\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h4>7.3 <code>__all__<\/code>\u53d8\u91cf<\/h4>\n<p>\u5728\u6a21\u5757\u6216\u5305\u7684<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49<code>__all__<\/code>\u53d8\u91cf\u6765\u63a7\u5236<code>from module import *<\/code>\u8bed\u53e5\u5bfc\u5165\u7684\u540d\u79f0\u3002<code>__all__<\/code>\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5305\u542b\u4e86\u53ef\u4ee5\u88ab\u5bfc\u5165\u7684\u540d\u79f0\u3002<\/p>\n<pre><code class=\"python\"># \u5305\u7684\u521d\u59cb\u5316\u4e0e\u547d\u540d\u7a7a\u95f4\u793a\u4f8b\n\n# \u5305\u7ed3\u6784\uff1a\n# mypackage\/\n#     __init__.py\n#     module1.py\n#     module2.py\n#     module3.py\n\n# \u6587\u4ef6\u540d: mypackage\/__init__.py\n&quot;&quot;&quot;\nmypackage\u5305\u7684\u521d\u59cb\u5316\u6587\u4ef6\n&quot;&quot;&quot;\n\n# \u5305\u7ea7\u53d8\u91cf\n__version__ = &quot;1.0.0&quot;\n__author__ = &quot;Python Developer&quot;\n\n# \u63a7\u5236from mypackage import *\u7684\u884c\u4e3a\n__all__ = ['module1', 'module2']  # \u53ea\u5bfc\u51famodule1\u548cmodule2\n\n# \u5bfc\u5165\u6a21\u5757\nfrom . import module1\nfrom . import module2\nfrom . import module3\n\n# \u5bfc\u51fa\u7279\u5b9a\u540d\u79f0\nfrom .module1 import function1\nfrom .module2 import function2\n\n# \u5305\u521d\u59cb\u5316\u4ee3\u7801\nprint(f&quot;\u521d\u59cb\u5316mypackage\u5305\uff0c\u7248\u672c: {__version__}&quot;)\n\n# \u6587\u4ef6\u540d: mypackage\/module1.py\n&quot;&quot;&quot;\nmodule1\u6a21\u5757\n&quot;&quot;&quot;\n\n# \u63a7\u5236from mypackage.module1 import *\u7684\u884c\u4e3a\n__all__ = ['function1', 'variable1']\n\n# \u6a21\u5757\u7ea7\u53d8\u91cf\nvariable1 = &quot;module1\u53d8\u91cf&quot;\nvariable2 = &quot;module1\u79c1\u6709\u53d8\u91cf&quot;\n\n# \u6a21\u5757\u7ea7\u51fd\u6570\ndef function1():\n    return &quot;module1\u7684\u51fd\u6570&quot;\n\ndef function2():\n    return &quot;module1\u7684\u53e6\u4e00\u4e2a\u51fd\u6570&quot;\n\n# \u6587\u4ef6\u540d: mypackage\/module2.py\n&quot;&quot;&quot;\nmodule2\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function2():\n    return &quot;module2\u7684\u51fd\u6570&quot;\n\n# \u6587\u4ef6\u540d: mypackage\/module3.py\n&quot;&quot;&quot;\nmodule3\u6a21\u5757\n&quot;&quot;&quot;\n\ndef function3():\n    return &quot;module3\u7684\u51fd\u6570&quot;\n\n# \u6d4b\u8bd5\u5305\u7684\u521d\u59cb\u5316\u4e0e\u547d\u540d\u7a7a\u95f4\n# \u6587\u4ef6\u540d: test_package_init.py\n\n# \u5bfc\u5165\u5305\nimport mypackage\nprint(f&quot;\u5bfc\u5165mypackage\u5305&quot;)\nprint(f&quot;\u5305\u7248\u672c: {mypackage.__version__}&quot;)\nprint(f&quot;\u5305\u4f5c\u8005: {mypackage.__author__}&quot;)\n\n# \u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757\nprint(f&quot;\/n\u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757\uff1a&quot;)\nprint(f&quot;module1.function1(): {mypackage.module1.function1()}&quot;)\nprint(f&quot;module2.function2(): {mypackage.module2.function2()}&quot;)\nprint(f&quot;module3.function3(): {mypackage.module3.function3()}&quot;)\n\n# \u4f7f\u7528\u5bfc\u51fa\u7684\u540d\u79f0\nprint(f&quot;\/n\u4f7f\u7528\u5bfc\u51fa\u7684\u540d\u79f0\uff1a&quot;)\nprint(f&quot;function1(): {mypackage.function1()}&quot;)\nprint(f&quot;function2(): {mypackage.function2()}&quot;)\n\n# \u6d4b\u8bd5from import *\nprint(f&quot;\/n\u6d4b\u8bd5from import *\uff1a&quot;)\nfrom mypackage import *\nprint(f&quot;\u53ef\u5bfc\u5165\u7684\u6a21\u5757: {[name for name in dir() if not name.startswith('_')]}&quot;)\nprint(f&quot;module1\u662f\u5426\u53ef\u5bfc\u5165: {'module1' in dir()}&quot;)\nprint(f&quot;module2\u662f\u5426\u53ef\u5bfc\u5165: {'module2' in dir()}&quot;)\nprint(f&quot;module3\u662f\u5426\u53ef\u5bfc\u5165: {'module3' in dir()}&quot;)\n\n# \u6d4b\u8bd5\u6a21\u5757\u7684from import *\nprint(f&quot;\/n\u6d4b\u8bd5\u6a21\u5757\u7684from import *\uff1a&quot;)\nfrom mypackage.module1 import *\nprint(f&quot;\u53ef\u5bfc\u5165\u7684\u540d\u79f0: {[name for name in dir() if not name.startswith('_')]}&quot;)\nprint(f&quot;variable1\u662f\u5426\u53ef\u5bfc\u5165: {'variable1' in dir()}&quot;)\nprint(f&quot;variable2\u662f\u5426\u53ef\u5bfc\u5165: {'variable2' in dir()}&quot;)\nprint(f&quot;function1\u662f\u5426\u53ef\u5bfc\u5165: {'function1' in dir()}&quot;)\nprint(f&quot;function2\u662f\u5426\u53ef\u5bfc\u5165: {'function2' in dir()}&quot;)<\/code><\/pre>\n<h3>7. \u6a21\u5757\u5bfc\u5165\u7684\u9ad8\u7ea7\u6280\u5de7<\/h3>\n<h4>7.1 \u52a8\u6001\u5bfc\u5165<\/h4>\n<p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\uff0c\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u6839\u636e\u6761\u4ef6\u5bfc\u5165\u4e0d\u540c\u7684\u6a21\u5757\u3002<\/p>\n<h5>7.1.1 \u4f7f\u7528<code>importlib.import_module()<\/code><\/h5>\n<p><code>importlib.import_module()<\/code>\u51fd\u6570\u662f\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u7684\u63a8\u8350\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u5bfc\u5165\u7684\u6a21\u5757\u5bf9\u8c61\u3002<\/p>\n<h5>7.1.2 \u4f7f\u7528<code>__import__()<\/code><\/h5>\n<p><code>__import__()<\/code>\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u3002\u5b83\u662f<code>import<\/code>\u8bed\u53e5\u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u4f46\u4e0d\u63a8\u8350\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<h5>7.1.3 \u4f7f\u7528<code>exec()<\/code><\/h5>\n<p><code>exec()<\/code>\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u52a8\u6001\u751f\u6210\u7684\u5bfc\u5165\u8bed\u53e5\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<h4>7.2 \u6761\u4ef6\u5bfc\u5165<\/h4>\n<p>\u6761\u4ef6\u5bfc\u5165\u662f\u6307\u6839\u636e\u6761\u4ef6\u5bfc\u5165\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u5e73\u53f0\u6216\u4e0d\u540c\u73af\u5883\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<h4>7.3 \u5ef6\u8fdf\u5bfc\u5165<\/h4>\n<p>\u5ef6\u8fdf\u5bfc\u5165\u662f\u6307\u5728\u9700\u8981\u65f6\u624d\u5bfc\u5165\u6a21\u5757\uff0c\u800c\u4e0d\u662f\u5728\u6a21\u5757\u521d\u59cb\u5316\u65f6\u5c31\u5bfc\u5165\u6240\u6709\u6a21\u5757\u3002\u8fd9\u53ef\u4ee5\u51cf\u5c11\u6a21\u5757\u7684\u521d\u59cb\u5316\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"python\"># \u6a21\u5757\u5bfc\u5165\u7684\u9ad8\u7ea7\u6280\u5de7\u793a\u4f8b\nimport importlib\nimport sys\nimport os\n\n# \u52a8\u6001\u5bfc\u5165\u793a\u4f8b\nprint(&quot;\u52a8\u6001\u5bfc\u5165\u793a\u4f8b\uff1a&quot;)\n\n# \u4f7f\u7528importlib.import_module()\nmodule_name = &quot;math&quot;\nmath_module = importlib.import_module(module_name)\nprint(f&quot;\u52a8\u6001\u5bfc\u5165{module_name}\u6a21\u5757\uff1a{math_module}&quot;)\nprint(f&quot;math.pi: {math_module.pi}&quot;)\n\n# \u52a8\u6001\u5bfc\u5165\u5e26\u5305\u7684\u6a21\u5757\npackage_module_name = &quot;os.path&quot;\nos_path_module = importlib.import_module(package_module_name)\nprint(f&quot;\/n\u52a8\u6001\u5bfc\u5165{package_module_name}\u6a21\u5757\uff1a{os_path_module}&quot;)\nprint(f&quot;os.path.abspath('.'): {os_path_module.abspath('.')}&quot;)\n\n# \u6761\u4ef6\u5bfc\u5165\u793a\u4f8b\nprint(&quot;\/n\u6761\u4ef6\u5bfc\u5165\u793a\u4f8b\uff1a&quot;)\n\n# \u6839\u636e\u5e73\u53f0\u5bfc\u5165\u4e0d\u540c\u7684\u6a21\u5757\nif sys.platform == &quot;win32&quot;:\n    print(&quot;Windows\u5e73\u53f0\uff0c\u5bfc\u5165msvcrt\u6a21\u5757&quot;)\n    import msvcrt\nelif sys.platform == &quot;linux&quot;:\n    print(&quot;Linux\u5e73\u53f0\uff0c\u5bfc\u5165termios\u6a21\u5757&quot;)\n    import termios\nelif sys.platform == &quot;darwin&quot;:\n    print(&quot;macOS\u5e73\u53f0\uff0c\u5bfc\u5165termios\u6a21\u5757&quot;)\n    import termios\nelse:\n    print(&quot;\u5176\u4ed6\u5e73\u53f0&quot;)\n\n# \u5ef6\u8fdf\u5bfc\u5165\u793a\u4f8b\nprint(&quot;\/n\u5ef6\u8fdf\u5bfc\u5165\u793a\u4f8b\uff1a&quot;)\n\n# \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6a21\u5757\ndef calculate_sin(x):\n    &quot;&quot;&quot;\u8ba1\u7b97\u6b63\u5f26\u503c&quot;&quot;&quot;\n    import math  # \u5ef6\u8fdf\u5bfc\u5165\n    return math.sin(x)\n\n# \u6d4b\u8bd5\u5ef6\u8fdf\u5bfc\u5165\nprint(&quot;\u8c03\u7528calculate_sin\u51fd\u6570\u524d\uff0cmath\u6a21\u5757\u662f\u5426\u5df2\u5bfc\u5165\uff1a&quot;, &quot;math&quot; in sys.modules)\nresult = calculate_sin(0.5)\nprint(f&quot;sin(0.5) = {result}&quot;)\nprint(&quot;\u8c03\u7528calculate_sin\u51fd\u6570\u540e\uff0cmath\u6a21\u5757\u662f\u5426\u5df2\u5bfc\u5165\uff1a&quot;, &quot;math&quot; in sys.modules)\n\n# \u52a8\u6001\u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570\ndef dynamic_call(module_name, function_name, *args, **kwargs):\n    &quot;&quot;&quot;\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570&quot;&quot;&quot;\n    # \u5bfc\u5165\u6a21\u5757\n    module = importlib.import_module(module_name)\n    # \u83b7\u53d6\u51fd\u6570\n    function = getattr(module, function_name)\n    # \u8c03\u7528\u51fd\u6570\n    return function(*args, **kwargs)\n\n# \u6d4b\u8bd5\u52a8\u6001\u8c03\u7528\nprint(&quot;\/n\u52a8\u6001\u8c03\u7528\u793a\u4f8b\uff1a&quot;)\nresult = dynamic_call(&quot;math&quot;, &quot;sqrt&quot;, 16)\nprint(f&quot;math.sqrt(16) = {result}&quot;)\n\nresult = dynamic_call(&quot;os&quot;, &quot;getcwd&quot;)\nprint(f&quot;os.getcwd() = {result}&quot;)\n\n# \u6d4b\u8bd5\u5bfc\u5165\u4e0d\u5b58\u5728\u7684\u6a21\u5757\ntry:\n    module = importlib.import_module(&quot;non_existent_module&quot;)\nexcept ImportError as e:\n    print(f&quot;\/n\u5bfc\u5165\u4e0d\u5b58\u5728\u7684\u6a21\u5757\u5931\u8d25\uff1a{e}&quot;)<\/code><\/pre>\n<h3>8. \u5305\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>8.1 \u9879\u76ee\u7ed3\u6784<\/h4>\n<p>\u4e00\u4e2a\u826f\u597d\u7684Python\u9879\u76ee\u7ed3\u6784\u5e94\u8be5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9879\u76ee\u6839\u76ee\u5f55<\/strong>\uff1a\u5305\u542b\u9879\u76ee\u7684\u4e3b\u8981\u6587\u4ef6<\/li>\n<li><strong>\u5305\u76ee\u5f55<\/strong>\uff1a\u5305\u542b\u9879\u76ee\u7684\u4ee3\u7801\u5305<\/li>\n<li><strong>\u6d4b\u8bd5\u76ee\u5f55<\/strong>\uff1a\u5305\u542b\u9879\u76ee\u7684\u6d4b\u8bd5\u4ee3\u7801<\/li>\n<li><strong>\u6587\u6863\u76ee\u5f55<\/strong>\uff1a\u5305\u542b\u9879\u76ee\u7684\u6587\u6863<\/li>\n<li><strong>\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u5305\u542b\u9879\u76ee\u7684\u914d\u7f6e\u4fe1\u606f<\/li>\n<\/ul>\n<h4>8.2 \u4f9d\u8d56\u7ba1\u7406<\/h4>\n<p>\u4f9d\u8d56\u7ba1\u7406\u662fPython\u9879\u76ee\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5e94\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong>\uff1a\u4e3a\u6bcf\u4e2a\u9879\u76ee\u521b\u5efa\u72ec\u7acb\u7684\u865a\u62df\u73af\u5883<\/li>\n<li><strong>\u9501\u5b9a\u4f9d\u8d56\u7248\u672c<\/strong>\uff1a\u4f7f\u7528<code>requirements.txt<\/code>\u6216<code>Pipfile.lock<\/code>\u9501\u5b9a\u4f9d\u8d56\u7248\u672c<\/li>\n<li><strong>\u5206\u79bb\u5f00\u53d1\u4f9d\u8d56\u548c\u751f\u4ea7\u4f9d\u8d56<\/strong>\uff1a\u5c06\u5f00\u53d1\u4f9d\u8d56\u548c\u751f\u4ea7\u4f9d\u8d56\u5206\u5f00\u7ba1\u7406<\/li>\n<li><strong>\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56<\/strong>\uff1a\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56\u4ee5\u83b7\u53d6\u5b89\u5168\u8865\u4e01\u548c\u65b0\u529f\u80fd<\/li>\n<\/ul>\n<h4>8.3 \u5305\u7684\u53d1\u5e03<\/h4>\n<p>\u5982\u679c\u8981\u53d1\u5e03\u81ea\u5df1\u7684Python\u5305\uff0c\u5e94\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u6807\u51c6\u7ed3\u6784<\/strong>\uff1a\u9075\u5faaPython\u5305\u7684\u6807\u51c6\u7ed3\u6784<\/li>\n<li><strong>\u7f16\u5199setup.py<\/strong>\uff1a\u521b\u5efa<code>setup.py<\/code>\u6587\u4ef6\u6765\u5b9a\u4e49\u5305\u7684\u5143\u6570\u636e<\/li>\n<li><strong>\u7f16\u5199README.md<\/strong>\uff1a\u521b\u5efaREADME.md\u6587\u4ef6\u6765\u63cf\u8ff0\u5305\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5<\/li>\n<li><strong>\u7f16\u5199\u6587\u6863<\/strong>\uff1a\u4e3a\u5305\u7f16\u5199\u8be6\u7ec6\u7684\u6587\u6863<\/li>\n<li><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong>\uff1a\u786e\u4fdd\u5305\u901a\u8fc7\u6240\u6709\u6d4b\u8bd5<\/li>\n<li><strong>\u4e0a\u4f20\u5230PyPI<\/strong>\uff1a\u5c06\u5305\u4e0a\u4f20\u5230PyPI\u4f9b\u5176\u4ed6\u4eba\u4f7f\u7528<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5305\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\n\n# \u9879\u76ee\u7ed3\u6784\u793a\u4f8b\n'''\nmyproject\/\n\u251c\u2500\u2500 README.md          # \u9879\u76ee\u8bf4\u660e\n\u251c\u2500\u2500 setup.py           # \u5305\u5b89\u88c5\u811a\u672c\n\u251c\u2500\u2500 requirements.txt   # \u4f9d\u8d56\u58f0\u660e\n\u251c\u2500\u2500 requirements-dev.txt # \u5f00\u53d1\u4f9d\u8d56\u58f0\u660e\n\u251c\u2500\u2500 mypackage\/         # \u4e3b\u8981\u5305\u76ee\u5f55\n\u2502   \u251c\u2500\u2500 __init__.py    # \u5305\u521d\u59cb\u5316\u6587\u4ef6\n\u2502   \u251c\u2500\u2500 module1.py     # \u6a21\u57571\n\u2502   \u251c\u2500\u2500 module2.py     # \u6a21\u57572\n\u2502   \u2514\u2500\u2500 subpackage\/    # \u5b50\u5305\n\u2502       \u251c\u2500\u2500 __init__.py\n\u2502       \u2514\u2500\u2500 submodule.py\n\u2514\u2500\u2500 tests\/             # \u6d4b\u8bd5\u76ee\u5f55\n    \u251c\u2500\u2500 __init__.py\n    \u251c\u2500\u2500 test_module1.py\n    \u2514\u2500\u2500 test_module2.py\n'''\n\n# setup.py\u793a\u4f8b\n'''\nfrom setuptools import setup, find_packages\n\nsetup(\n    name=&quot;mypackage&quot;,\n    version=&quot;1.0.0&quot;,\n    description=&quot;A sample Python package&quot;,\n    author=&quot;Python Developer&quot;,\n    author_email=&quot;developer@example.com&quot;,\n    url=&quot;https:\/\/github.com\/username\/mypackage&quot;,\n    packages=find_packages(),\n    install_requires=[\n        &quot;requests&gt;=2.25.0&quot;,\n        &quot;numpy&gt;=1.20.0&quot;\n    ],\n    extras_require={\n        &quot;dev&quot;: [\n            &quot;pytest&gt;=6.0.0&quot;,\n            &quot;black&gt;=21.0.0&quot;\n        ]\n    },\n    classifiers=[\n        &quot;Programming Language :: Python :: 3&quot;,\n        &quot;License :: OSI Approved :: MIT License&quot;,\n        &quot;Operating System :: OS Independent&quot;,\n    ],\n    python_requires=&quot;&gt;=3.6&quot;,\n)\n'''\n\n# requirements.txt\u793a\u4f8b\n'''\nrequests&gt;=2.25.0\nnumpy&gt;=1.20.0\n'''\n\n# requirements-dev.txt\u793a\u4f8b\n'''\n-r requirements.txt\npytest&gt;=6.0.0\nblack&gt;=21.0.0\n'''\n\n# .gitignore\u793a\u4f8b\n'''\n# Python\n__pycache__\/\n*.py[cod]\n*$py.class\n\n# Virtual Environment\nvenv\/\nenv\/\n\n# IDE\n.vscode\/\n.idea\/\n\n# Build artifacts\nbuild\/\ndist\/\n*.egg-info\/\n\n# Testing\n.pytest_cache\/\n\n# Logs\nlogs\/\n*.log\n'''\n\n# \u5305\u53d1\u5e03\u6b65\u9aa4\n'''\n1. \u5b89\u88c5\u6784\u5efa\u5de5\u5177\n   pip install setuptools wheel twine\n\n2. \u6784\u5efa\u5305\n   python setup.py sdist bdist_wheel\n\n3. \u4e0a\u4f20\u5305\u5230PyPI\u6d4b\u8bd5\u73af\u5883\n   twine upload --repository testpypi dist\/*\n\n4. \u4e0a\u4f20\u5305\u5230PyPI\u751f\u4ea7\u73af\u5883\n   twine upload dist\/*\n'''\n\n# \u6d4b\u8bd5\u5305\u5b89\u88c5\n'''\n# \u4ecePyPI\u5b89\u88c5\npip install mypackage\n\n# \u4ece\u672c\u5730\u5b89\u88c5\npip install -e .\n\n# \u5b89\u88c5\u5f00\u53d1\u4f9d\u8d56\npip install -e .[dev]\n'''<\/code><\/pre>\n<h3>9. \u5e38\u89c1\u5bfc\u5165\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u5728Python\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5bfc\u5165\u95ee\u9898\u3002\u7406\u89e3\u8fd9\u4e9b\u95ee\u9898\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u5bf9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h4>9.1 \u5bfc\u5165\u9519\u8bef\u7684\u5e38\u89c1\u539f\u56e0<\/h4>\n<ul>\n<li><strong>\u6a21\u5757\u4e0d\u5b58\u5728<\/strong>\uff1a\u5c1d\u8bd5\u5bfc\u5165\u4e0d\u5b58\u5728\u7684\u6a21\u5757<\/li>\n<li><strong>\u5bfc\u5165\u8def\u5f84\u95ee\u9898<\/strong>\uff1a\u6a21\u5757\u4e0d\u5728Python\u7684\u5bfc\u5165\u8def\u5f84\u4e2d<\/li>\n<li><strong>\u5faa\u73af\u5bfc\u5165<\/strong>\uff1a\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165<\/li>\n<li><strong>\u547d\u540d\u51b2\u7a81<\/strong>\uff1a\u6a21\u5757\u540d\u79f0\u4e0e\u6807\u51c6\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\u540d\u79f0\u51b2\u7a81<\/li>\n<li><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u6ca1\u6709\u8bfb\u53d6\u6a21\u5757\u6587\u4ef6\u7684\u6743\u9650<\/li>\n<li><strong>\u8bed\u6cd5\u9519\u8bef<\/strong>\uff1a\u6a21\u5757\u6587\u4ef6\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef<\/li>\n<\/ul>\n<h4>9.2 \u5faa\u73af\u5bfc\u5165<\/h4>\n<p>\u5faa\u73af\u5bfc\u5165\u662f\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\uff0c\u53ef\u80fd\u5bfc\u81f4\u5bfc\u5165\u5931\u8d25\u6216\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<h5>9.2.1 \u5faa\u73af\u5bfc\u5165\u7684\u793a\u4f8b<\/h5>\n<pre><code class=\"python\"># \u6a21\u5757A\nimport module_b\ndef function_a():\n    return module_b.function_b()\n\n# \u6a21\u5757B\nimport module_a\ndef function_b():\n    return module_a.function_a()<\/code><\/pre>\n<h5>9.2.2 \u5faa\u73af\u5bfc\u5165\u7684\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li><strong>\u91cd\u6784\u4ee3\u7801<\/strong>\uff1a\u5c06\u5171\u4eab\u7684\u4ee3\u7801\u63d0\u53d6\u5230\u4e00\u4e2a\u65b0\u7684\u6a21\u5757\u4e2d<\/li>\n<li><strong>\u5ef6\u8fdf\u5bfc\u5165<\/strong>\uff1a\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6a21\u5757\uff0c\u800c\u4e0d\u662f\u5728\u6a21\u5757\u9876\u90e8\u5bfc\u5165<\/li>\n<li><strong>\u5bfc\u5165\u91cd\u547d\u540d<\/strong>\uff1a\u4f7f\u7528\u522b\u540d\u5bfc\u5165\u6a21\u5757\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81<\/li>\n<li><strong>\u91cd\u65b0\u7ec4\u7ec7\u6a21\u5757\u7ed3\u6784<\/strong>\uff1a\u91cd\u65b0\u7ec4\u7ec7\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb<\/li>\n<\/ul>\n<h4>9.3 \u5bfc\u5165\u8def\u5f84\u95ee\u9898<\/h4>\n<p>\u5bfc\u5165\u8def\u5f84\u95ee\u9898\u662f\u6307\u6a21\u5757\u4e0d\u5728Python\u7684\u5bfc\u5165\u8def\u5f84\u4e2d\uff0c\u5bfc\u81f4\u65e0\u6cd5\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<h5>9.3.1 \u5bfc\u5165\u8def\u5f84\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li><strong>\u6dfb\u52a0\u5bfc\u5165\u8def\u5f84<\/strong>\uff1a\u5c06\u6a21\u5757\u6240\u5728\u7684\u76ee\u5f55\u6dfb\u52a0\u5230<code>sys.path<\/code>\u4e2d<\/li>\n<li><strong>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/strong>\uff1a\u5728\u5305\u5185\u90e8\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/li>\n<li><strong>\u8bbe\u7f6ePYTHONPATH<\/strong>\uff1a\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf<\/li>\n<li><strong>\u4f7f\u7528.pth\u6587\u4ef6<\/strong>\uff1a\u521b\u5efa<code>.pth<\/code>\u6587\u4ef6\u6765\u6dfb\u52a0\u5bfc\u5165\u8def\u5f84<\/li>\n<li><strong>\u5b89\u88c5\u6a21\u5757<\/strong>\uff1a\u5c06\u6a21\u5757\u5b89\u88c5\u5230Python\u7684\u7ad9\u70b9\u5305\u76ee\u5f55\u4e2d<\/li>\n<\/ul>\n<h4>9.4 \u547d\u540d\u51b2\u7a81<\/h4>\n<p>\u547d\u540d\u51b2\u7a81\u662f\u6307\u6a21\u5757\u540d\u79f0\u4e0e\u6807\u51c6\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\u540d\u79f0\u51b2\u7a81\uff0c\u5bfc\u81f4\u5bfc\u5165\u9519\u8bef\u3002<\/p>\n<h5>9.4.1 \u547d\u540d\u51b2\u7a81\u7684\u89e3\u51b3\u65b9\u6848<\/h5>\n<ul>\n<li><strong>\u91cd\u547d\u540d\u6a21\u5757<\/strong>\uff1a\u91cd\u547d\u540d\u4e0e\u6807\u51c6\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u51b2\u7a81\u7684\u6a21\u5757<\/li>\n<li><strong>\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165<\/strong>\uff1a\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u6765\u907f\u514d\u547d\u540d\u51b2\u7a81<\/li>\n<li><strong>\u4f7f\u7528\u522b\u540d<\/strong>\uff1a\u4f7f\u7528\u522b\u540d\u5bfc\u5165\u6a21\u5757\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5e38\u89c1\u5bfc\u5165\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848\u793a\u4f8b\n\n# 1. \u5faa\u73af\u5bfc\u5165\u793a\u4f8b\n# \u6587\u4ef6\u540d: module_a.py\n'''\nimport module_b\n\ndef function_a():\n    print(&quot;function_a called&quot;)\n    return module_b.function_b()\n'''\n\n# \u6587\u4ef6\u540d: module_b.py\n'''\nimport module_a\n\ndef function_b():\n    print(&quot;function_b called&quot;)\n    return module_a.function_a()\n'''\n\n# \u6d4b\u8bd5\u5faa\u73af\u5bfc\u5165\n# \u6587\u4ef6\u540d: test_circular_import.py\n'''\ntry:\n    import module_a\n    print(&quot;\u5bfc\u5165module_a\u6210\u529f&quot;)\n    result = module_a.function_a()\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u5bfc\u5165\u9519\u8bef: {e}&quot;)\n'''\n\n# \u5faa\u73af\u5bfc\u5165\u7684\u89e3\u51b3\u65b9\u6848\n# \u6587\u4ef6\u540d: module_a_fixed.py\n'''\n# \u5ef6\u8fdf\u5bfc\u5165\ndef function_a():\n    import module_b_fixed\n    print(&quot;function_a called&quot;)\n    return module_b_fixed.function_b()\n'''\n\n# \u6587\u4ef6\u540d: module_b_fixed.py\n'''\n# \u5ef6\u8fdf\u5bfc\u5165\ndef function_b():\n    import module_a_fixed\n    print(&quot;function_b called&quot;)\n    return module_a_fixed.function_a()\n'''\n\n# 2. \u5bfc\u5165\u8def\u5f84\u95ee\u9898\u793a\u4f8b\n# \u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a\n# project\/\n#     src\/\n#         mypackage\/\n#             __init__.py\n#             module.py\n#     scripts\/\n#         script.py\n\n# \u6587\u4ef6\u540d: project\/scripts\/script.py\n'''\n# \u5c1d\u8bd5\u5bfc\u5165mypackage\nimport sys\nimport os\n\n# \u6dfb\u52a0src\u76ee\u5f55\u5230\u5bfc\u5165\u8def\u5f84\nsys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), &quot;..&quot;, &quot;src&quot;)))\n\n# \u73b0\u5728\u53ef\u4ee5\u5bfc\u5165mypackage\nimport mypackage\nprint(&quot;\u5bfc\u5165mypackage\u6210\u529f&quot;)\n'''\n\n# 3. \u547d\u540d\u51b2\u7a81\u793a\u4f8b\n# \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3amath.py\u7684\u6587\u4ef6\uff0c\u4e0e\u6807\u51c6\u5e93math\u6a21\u5757\u51b2\u7a81\n\n# \u6587\u4ef6\u540d: math.py\n'''\ndef add(a, b):\n    return a + b\n'''\n\n# \u6d4b\u8bd5\u547d\u540d\u51b2\u7a81\n# \u6587\u4ef6\u540d: test_name_conflict.py\n'''\n# \u8fd9\u4f1a\u5bfc\u5165\u5f53\u524d\u76ee\u5f55\u7684math.py\uff0c\u800c\u4e0d\u662f\u6807\u51c6\u5e93\u7684math\u6a21\u5757\nimport math\nprint(f&quot;math\u6a21\u5757\u8def\u5f84: {math.__file__}&quot;)\n\n# \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u6216\u91cd\u547d\u540d\u6a21\u5757\n# 1. \u91cd\u547d\u540d\u6a21\u5757\u4e3amy_math.py\n# 2. \u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\uff08\u5728Python 3\u4e2d\u9ed8\u8ba4\uff09\n'''\n\n# 4. \u5bfc\u5165\u9519\u8bef\u7684\u8c03\u8bd5\nprint(&quot;\u5bfc\u5165\u9519\u8bef\u7684\u8c03\u8bd5\u793a\u4f8b\uff1a&quot;)\n\n# \u67e5\u770b\u5bfc\u5165\u8def\u5f84\nimport sys\nprint(&quot;Python\u5bfc\u5165\u8def\u5f84\uff1a&quot;)\nfor path in sys.path:\n    print(f&quot;  {path}&quot;)\n\n# \u67e5\u770b\u6a21\u5757\u662f\u5426\u5b58\u5728\nmodule_name = &quot;math&quot;\nprint(f&quot;\/n\u68c0\u67e5{module_name}\u6a21\u5757\uff1a&quot;)\nif module_name in sys.modules:\n    print(f&quot;\u6a21\u5757\u5df2\u5bfc\u5165: {sys.modules[module_name]}&quot;)\nelse:\n    print(&quot;\u6a21\u5757\u672a\u5bfc\u5165&quot;)\n\n# \u5c1d\u8bd5\u5bfc\u5165\u6a21\u5757\ntry:\n    import non_existent_module\n    print(&quot;\u5bfc\u5165\u6210\u529f&quot;)\nexcept ImportError as e:\n    print(f&quot;\u5bfc\u5165\u9519\u8bef: {e}&quot;)\n\n# \u68c0\u67e5\u6587\u4ef6\u6743\u9650\nprint(&quot;\/n\u68c0\u67e5\u6587\u4ef6\u6743\u9650\uff1a&quot;)\nif os.path.exists(&quot;test_module.py&quot;):\n    print(f&quot;\u6587\u4ef6\u5b58\u5728: {os.path.exists('test_module.py')}&quot;)\n    print(f&quot;\u6587\u4ef6\u53ef\u8bfb: {os.access('test_module.py', os.R_OK)}&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)<\/code><\/pre>\n<h3>10. \u603b\u7ed3<\/h3>\n<p>\u672c\u6587\u8be6\u7ec6\u5206\u6790\u4e86Python\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u4e0e\u5305\u7ba1\u7406\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u6a21\u5757\u4e0e\u5305\u7684\u57fa\u672c\u6982\u5ff5<\/strong>\uff1a\u6a21\u5757\u548c\u5305\u7684\u5b9a\u4e49\u3001\u4f5c\u7528\u548c\u5173\u7cfb<\/li>\n<li><strong>Python\u7684\u5bfc\u5165\u673a\u5236<\/strong>\uff1a\u5bfc\u5165\u8bed\u53e5\u7684\u7c7b\u578b\u3001\u5bfc\u5165\u673a\u5236\u7684\u5de5\u4f5c\u539f\u7406\u3001\u5bfc\u5165\u8def\u5f84\u548c\u6a21\u5757\u7684\u52a0\u8f7d\u4e0e\u521d\u59cb\u5316<\/li>\n<li><strong>\u5305\u7ba1\u7406\u7cfb\u7edf<\/strong>\uff1a\u5305\u7ba1\u7406\u5de5\u5177\u3001pip\u7684\u4f7f\u7528\u548c\u865a\u62df\u73af\u5883<\/li>\n<li><strong>\u5bfc\u5165\u8def\u5f84\u4e0e\u6a21\u5757\u67e5\u627e<\/strong>\uff1a\u5bfc\u5165\u8def\u5f84\u7684\u7ec4\u6210\u3001\u6a21\u5757\u67e5\u627e\u7684\u987a\u5e8f\u548c\u6a21\u5757\u6587\u4ef6\u7684\u7c7b\u578b<\/li>\n<li><strong>\u76f8\u5bf9\u5bfc\u5165\u4e0e\u7edd\u5bf9\u5bfc\u5165<\/strong>\uff1a\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u7684\u6982\u5ff5\u3001\u4f7f\u7528\u65b9\u6cd5\u548c\u9009\u62e9<\/li>\n<li><strong>\u6a21\u5757\u7f13\u5b58\u4e0e\u91cd\u8f7d<\/strong>\uff1a\u6a21\u5757\u7f13\u5b58\u7684\u4f5c\u7528\u3001\u6a21\u5757\u91cd\u8f7d\u7684\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879<\/li>\n<li><strong>\u5305\u7684\u521d\u59cb\u5316\u4e0e\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5305\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u3001\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u548c<code>__all__<\/code>\u53d8\u91cf<\/li>\n<li><strong>\u6a21\u5757\u5bfc\u5165\u7684\u9ad8\u7ea7\u6280\u5de7<\/strong>\uff1a\u52a8\u6001\u5bfc\u5165\u3001\u6761\u4ef6\u5bfc\u5165\u548c\u5ef6\u8fdf\u5bfc\u5165<\/li>\n<li><strong>\u5305\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u9879\u76ee\u7ed3\u6784\u3001\u4f9d\u8d56\u7ba1\u7406\u548c\u5305\u7684\u53d1\u5e03<\/li>\n<li><strong>\u5e38\u89c1\u5bfc\u5165\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u5bfc\u5165\u9519\u8bef\u7684\u5e38\u89c1\u539f\u56e0\u3001\u5faa\u73af\u5bfc\u5165\u3001\u5bfc\u5165\u8def\u5f84\u95ee\u9898\u548c\u547d\u540d\u51b2\u7a81<\/li>\n<\/ul>\n<p>Python\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u548c\u5305\u7ba1\u7406\u7cfb\u7edf\u662fPython\u8bed\u8a00\u7684\u91cd\u8981\u7279\u6027\uff0c\u5b83\u4eec\u4e3a\u4ee3\u7801\u7ec4\u7ec7\u3001\u91cd\u7528\u548c\u5206\u53d1\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\u548c\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u6a21\u5757\u5316\u3001\u53ef\u7ef4\u62a4\u548c\u53ef\u6269\u5c55\u7684Python\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u5bfc\u5165\u65b9\u5f0f\u548c\u5305\u7ba1\u7406\u7b56\u7565\uff0c\u9075\u5faaPython\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<h3>11. \u53c2\u8003\u6587\u732e<\/h3>\n<ol>\n<li>Python Documentation: Modules<\/li>\n<li>Python Documentation: Packages<\/li>\n<li>Python Documentation: The Import System<\/li>\n<li>Python Documentation: pip User Guide<\/li>\n<li>PEP 328 &#8212; Imports: Multi-Line and Absolute\/Relative<\/li>\n<li>PEP 404 &#8212; Python 2.7 Release Schedule<\/li>\n<li>PEP 517 &#8212; A build-system independent format for source trees<\/li>\n<li>PEP 518 &#8212; Specifying Minimum Build System Requirements for Python Projects<\/li>\n<li>Python Packaging User Guide<\/li>\n<li>Real Python: Absolute vs Relative Imports in Python<\/li>\n<\/ol>\n<h3>12. \u7ed3\u8bed<\/h3>\n<p>Python\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u4e0e\u5305\u7ba1\u7406\u662fPython\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u4e5f\u662fPython\u751f\u6001\u7cfb\u7edf\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u80fd\u591f\uff1a<\/p>\n<ol>\n<li>\u7406\u89e3Python\u6a21\u5757\u548c\u5305\u7684\u57fa\u672c\u6982\u5ff5<\/li>\n<li>\u638c\u63e1Python\u7684\u5bfc\u5165\u673a\u5236\u548c\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u719f\u7ec3\u4f7f\u7528pip\u548c\u865a\u62df\u73af\u5883\u7ba1\u7406\u4f9d\u8d56<\/li>\n<li>\u6b63\u786e\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165<\/li>\n<li>\u89e3\u51b3\u5e38\u89c1\u7684\u5bfc\u5165\u95ee\u9898<\/li>\n<li>\u9075\u5faaPython\u5305\u7ba1\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ol>\n<p>Python\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u548c\u5305\u7ba1\u7406\u7cfb\u7edf\u8bbe\u8ba1\u7b80\u6d01\u800c\u5f3a\u5927\uff0c\u5b83\u4e0d\u4ec5\u65b9\u4fbf\u4e86\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u91cd\u7528\uff0c\u4e5f\u4fc3\u8fdb\u4e86Python\u751f\u6001\u7cfb\u7edf\u7684\u53d1\u5c55\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u66f4\u52a0\u6a21\u5757\u5316\u3001\u53ef\u7ef4\u62a4\u548c\u53ef\u6269\u5c55\u7684Python\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5728\u672a\u6765\u7684Python\u5f00\u53d1\u4e2d\uff0c\u968f\u7740Python\u8bed\u8a00\u7684\u4e0d\u65ad\u53d1\u5c55\u548c\u751f\u6001\u7cfb\u7edf\u7684\u4e0d\u65ad\u5b8c\u5584\uff0c\u6a21\u5757\u5bfc\u5165\u673a\u5236\u548c\u5305\u7ba1\u7406\u7cfb\u7edf\u4e5f\u4f1a\u4e0d\u65ad\u6539\u8fdb\u548c\u4f18\u5316\u3002\u6211\u4eec\u5e94\u8be5\u4fdd\u6301\u5b66\u4e60\u7684\u6001\u5ea6\uff0c\u5173\u6ce8Python\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585255\">https:\/\/segmentfault.com\/a\/1190000047585255<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u6a21\u5757\u5bfc\u5165\u673a\u5236\u4e0e\u5305\u7ba1\u7406 1. \u6a21\u5757\u4e0e\u5305\u7684\u57fa\u672c\u6982\u5ff5 \u5728Python\u4e2d\uff0c\u6a21\u5757\uff08Module\uff09\u548c\u5305\uff08P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021230","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021230"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021230\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}