{"id":1076814,"date":"2025-01-08T11:56:38","date_gmt":"2025-01-08T03:56:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076814.html"},"modified":"2025-01-08T11:56:40","modified_gmt":"2025-01-08T03:56:40","slug":"python%e9%a1%b9%e7%9b%ae%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%85%88%e6%89%a7%e8%a1%8c%e7%9a%84%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076814.html","title":{"rendered":"python\u9879\u76ee\u5982\u4f55\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181158\/12363e34-48b9-4c10-bda6-ea321333290b.webp\" alt=\"python\u9879\u76ee\u5982\u4f55\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6\" \/><\/p>\n<p><p> Python\u9879\u76ee\u5982\u4f55\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6\uff1f<strong>\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u4e3a\u5165\u53e3\u6587\u4ef6\u3001\u4f7f\u7528<code>__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__<\/code>\u6a21\u5757\u3001\u914d\u7f6e\u811a\u672c\u6267\u884c\u5165\u53e3<\/strong>\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u4f5c\u4e3a\u5165\u53e3\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199<code>if __name__ == &#39;__main__&#39;:<\/code>\u8fd9\u6bb5\u4ee3\u7801\u3002\u8fd9\u6837\uff0c\u5f53\u4f60\u8fd0\u884c\u9879\u76ee\u65f6\uff0cPython\u89e3\u91ca\u5668\u5c06\u9996\u5148\u6267\u884c\u8be5\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u6307\u5b9a\u6587\u4ef6\u4e3a\u5165\u53e3\u6587\u4ef6<\/strong>\u662fPython\u9879\u76ee\u4e2d\u8bbe\u7f6e\u5148\u6267\u884c\u6587\u4ef6\u7684\u4e00\u79cd\u6700\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5728\u9879\u76ee\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0<code>if __name__ == &#39;__main__&#39;:<\/code>\u6761\u4ef6\u8bed\u53e5\u3002\u8fd9\u6837\uff0c\u5f53\u4f60\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u9996\u5148\u6267\u884c\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>Python\u9879\u76ee\u5982\u4f55\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6<\/strong><\/h2>\n<p><h2>\u4e00\u3001\u6307\u5b9a\u6587\u4ef6\u4e3a\u5165\u53e3\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728Python\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u6709\u4e00\u4e2a\u4e3b\u6587\u4ef6\u4f5c\u4e3a\u5165\u53e3\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0<code>if __name__ == &#39;__main__&#39;:<\/code>\u6761\u4ef6\u8bed\u53e5\uff0c\u8fd9\u6837\u5f53\u4f60\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u9996\u5148\u6267\u884c\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53\u4f60\u8fd0\u884c<code>main.py<\/code>\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u9996\u5148\u6267\u884c<code>main()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>__main__<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684<code>__main__<\/code>\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002\u5f53\u4f60\u8fd0\u884c\u4e00\u4e2aPython\u6587\u4ef6\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u5c06\u8be5\u6587\u4ef6\u7684<code>__name__<\/code>\u53d8\u91cf\u8bbe\u7f6e\u4e3a<code>__main__<\/code>\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa<code>main.py<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>main.py<\/code>\u7684\u6587\u4ef6\uff0c\u8fd9\u662f\u4f60\u7684\u9879\u76ee\u5165\u53e3\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u4e3b\u7a0b\u5e8f\u7684\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528<code>if __name__ == &#39;__main__&#39;:<\/code>\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u4ee3\u7801\u5728\u76f4\u63a5\u8fd0\u884c\u65f6\u88ab\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u5176\u4ed6\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u5176\u4ed6\u6a21\u5757\uff0c\u5e76\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module.py<\/p>\n<p>def module_function():<\/p>\n<p>    print(&quot;This is a function from module&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5bfc\u5165<code>module.py<\/code>\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p>    module.module_function()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u8fd0\u884c<code>main.py<\/code>\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u9996\u5148\u6267\u884c<code>main.py<\/code>\u4e2d\u7684\u4ee3\u7801\uff0c\u5e76\u8c03\u7528<code>module.py<\/code>\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python main.py<\/p>\n<p>This is the main function<\/p>\n<p>This is a function from module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u914d\u7f6e\u811a\u672c\u6267\u884c\u5165\u53e3<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>if __name__ == &#39;__main__&#39;:<\/code>\u6761\u4ef6\u8bed\u53e5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u811a\u672c\u6267\u884c\u5165\u53e3\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002\u8fd9\u5728\u521b\u5efa\u547d\u4ee4\u884c\u5de5\u5177\u6216\u53ef\u6267\u884c\u811a\u672c\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>setuptools<\/code>\u914d\u7f6e\u811a\u672c\u5165\u53e3<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u5e93\u6765\u914d\u7f6e\u9879\u76ee\u7684\u811a\u672c\u5165\u53e3\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u811a\u672c\u5165\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;myproject&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    py_modules=[&#39;main&#39;],<\/p>\n<p>    entry_points={<\/p>\n<p>        &#39;console_scripts&#39;: [<\/p>\n<p>            &#39;myproject=main:main&#39;,<\/p>\n<p>        ],<\/p>\n<p>    },<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>console_scripts<\/code>\u5165\u53e3\u70b9\u6765\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>myproject<\/code>\u7684\u547d\u4ee4\uff0c\u5b83\u4f1a\u8c03\u7528<code>main.py<\/code>\u4e2d\u7684<code>main<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b89\u88c5\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pip install .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>myproject<\/code>\u547d\u4ee4\uff0c\u8fd9\u5c06\u8c03\u7528<code>main.py<\/code>\u4e2d\u7684<code>main<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ myproject<\/p>\n<p>This is the main function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684Python\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002<code>__init__.py<\/code>\u6587\u4ef6\u901a\u5e38\u7528\u4e8e\u5c06\u76ee\u5f55\u6807\u8bc6\u4e3aPython\u5305\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u5728\u5176\u4e2d\u7f16\u5199\u521d\u59cb\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u5305\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mypackage<\/code>\u7684\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa<code>__init__.py<\/code>\u548c<code>main.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mypackage\/<\/p>\n<p>    __init__.py<\/p>\n<p>    main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7f16\u5199<code>__init__.py<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5bfc\u5165<code>main.py<\/code>\u4e2d\u7684\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/__init__.py<\/p>\n<p>from .main import main<\/p>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7f16\u5199<code>main.py<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u5b9a\u4e49\u4e3b\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/main.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u8fd0\u884c\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u5bfc\u5165<code>mypackage<\/code>\u5305\u65f6\uff0c<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u5e76\u8c03\u7528<code>main.py<\/code>\u4e2d\u7684<code>main<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python -c &quot;import mypackage&quot;<\/p>\n<p>This is the main function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u6839\u636e\u547d\u4ee4\u884c\u53c2\u6570\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5e76\u6839\u636e\u53c2\u6570\u503c\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u7f16\u5199\u547d\u4ee4\u884c\u89e3\u6790\u5668<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u547d\u4ee4\u884c\u89e3\u6790\u5668\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>cli.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># cli.py<\/p>\n<p>import argparse<\/p>\n<p>def main():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&#39;My Project&#39;)<\/p>\n<p>    parser.add_argument(&#39;--module&#39;, choices=[&#39;module1&#39;, &#39;module2&#39;], required=True, help=&#39;Module to run&#39;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    if args.module == &#39;module1&#39;:<\/p>\n<p>        from module1 import main as module1_main<\/p>\n<p>        module1_main()<\/p>\n<p>    elif args.module == &#39;module2&#39;:<\/p>\n<p>        from module2 import main as module2_main<\/p>\n<p>        module2_main()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e24\u4e2a\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49<code>main<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module1.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is module 1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u518d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module2.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is module 2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6765\u6307\u5b9a\u8981\u8fd0\u884c\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u8fd0\u884c<code>module1<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python cli.py --module module1<\/p>\n<p>This is module 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u8fd0\u884c<code>module2<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python cli.py --module module2<\/p>\n<p>This is module 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528<code>__main__.py<\/code>\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u8fd0\u884c\u5305\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5728\u5305\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>__main__.py<\/code>\u7684\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4f1a\u5728\u8fd0\u884c\u5305\u65f6\u88ab\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u5305\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mypackage<\/code>\u7684\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa<code>__main__.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mypackage\/<\/p>\n<p>    __main__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7f16\u5199<code>__main__.py<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728<code>__main__.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u7f16\u5199\u4e3b\u7a0b\u5e8f\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/__main__.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is the main function&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u5305<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u8fd0\u884c\u5305\u65f6\uff0c<code>__main__.py<\/code>\u6587\u4ef6\u4f1a\u88ab\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python -m mypackage<\/p>\n<p>This is the main function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u6839\u636e\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6e\u9879\u76ee\u7684\u5165\u53e3\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u6839\u636e\u53d8\u91cf\u503c\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u7f16\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2aPython\u6587\u4ef6\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>env.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># env.py<\/p>\n<p>import os<\/p>\n<p>def main():<\/p>\n<p>    module = os.getenv(&#39;MYPROJECT_MODULE&#39;)<\/p>\n<p>    if module == &#39;module1&#39;:<\/p>\n<p>        from module1 import main as module1_main<\/p>\n<p>        module1_main()<\/p>\n<p>    elif module == &#39;module2&#39;:<\/p>\n<p>        from module2 import main as module2_main<\/p>\n<p>        module2_main()<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e24\u4e2a\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49<code>main<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module1.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is module 1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u518d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module2.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;This is module 2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8fd0\u884c\u9879\u76ee<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u8981\u8fd0\u884c\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u8fd0\u884c<code>module1<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ export MYPROJECT_MODULE=module1<\/p>\n<p>$ python env.py<\/p>\n<p>This is module 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u8fd0\u884c<code>module2<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ export MYPROJECT_MODULE=module2<\/p>\n<p>$ python env.py<\/p>\n<p>This is module 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u9879\u76ee\u4e2d\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u6307\u5b9a\u6587\u4ef6\u4e3a\u5165\u53e3\u6587\u4ef6\u3001\u4f7f\u7528<code>__main__<\/code>\u6a21\u5757\u3001\u914d\u7f6e\u811a\u672c\u6267\u884c\u5165\u53e3\u3001\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u3001\u4f7f\u7528<code>__main__.py<\/code>\u6587\u4ef6\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u590d\u6742\u5ea6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u63d0\u9ad8\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u6307\u5b9a\u4e3b\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u540d\u4e3a<code>main.py<\/code>\u6216<code>__main__.py<\/code>\u7684\u6587\u4ef6\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u3002\u4f60\u53ef\u4ee5\u5728\u8be5\u6587\u4ef6\u4e2d\u8c03\u7528\u5176\u4ed6\u6a21\u5757\u6216\u529f\u80fd\uff0c\u4ece\u800c\u5b9e\u73b0\u6574\u4e2a\u7a0b\u5e8f\u7684\u903b\u8f91\u3002\u786e\u4fdd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;:<\/code>\u8bed\u53e5\u6765\u542f\u52a8\u7a0b\u5e8f\u7684\u4e3b\u8981\u90e8\u5206\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5728\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u65f6\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u7279\u5b9a\u7684Python\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8fd0\u884c\u7279\u5b9a\u7684Python\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<code>python your_file.py<\/code>\u3002\u786e\u4fdd\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u4f4d\u4e8e\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u6216\u8005\u63d0\u4f9b\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u6b63\u786e\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ba1\u7406\u4e00\u4e2a\u5927\u578bPython\u9879\u76ee\u7684\u6587\u4ef6\u7ed3\u6784\uff1f<\/strong><br \/>\u5927\u578b\u9879\u76ee\u901a\u5e38\u4f1a\u6d89\u53ca\u591a\u4e2a\u6a21\u5757\u548c\u5305\u3002\u5efa\u8bae\u6309\u7167\u529f\u80fd\u5c06\u6587\u4ef6\u7ec4\u7ec7\u8d77\u6765\uff0c\u4f8b\u5982\u53ef\u4ee5\u521b\u5efa<code>src<\/code>\u3001<code>tests<\/code>\u548c<code>docs<\/code>\u6587\u4ef6\u5939\uff0c\u5206\u522b\u7528\u4e8e\u6e90\u4ee3\u7801\u3001\u6d4b\u8bd5\u4ee3\u7801\u548c\u6587\u6863\u3002\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6\u53ef\u4ee5\u5c06\u67d0\u4e2a\u76ee\u5f55\u6807\u8bc6\u4e3aPython\u5305\uff0c\u8fd9\u6837\u5728\u9879\u76ee\u5185\u90e8\u5f15\u7528\u6a21\u5757\u65f6\u66f4\u4e3a\u65b9\u4fbf\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u4e00\u4e2a\u6e05\u6670\u7684\u547d\u540d\u89c4\u8303\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u9879\u76ee\u5982\u4f55\u8bbe\u7f6e\u5148\u6267\u884c\u7684\u6587\u4ef6\uff1f\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u4e3a\u5165\u53e3\u6587\u4ef6\u3001\u4f7f\u7528__mAIn__\u6a21\u5757\u3001\u914d\u7f6e\u811a\u672c\u6267\u884c\u5165\u53e3 [&hellip;]","protected":false},"author":3,"featured_media":1076821,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076814"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1076814"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076814\/revisions"}],"predecessor-version":[{"id":1076822,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076814\/revisions\/1076822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076821"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}