{"id":173988,"date":"2024-05-08T18:22:45","date_gmt":"2024-05-08T10:22:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173988.html"},"modified":"2024-05-08T18:22:51","modified_gmt":"2024-05-08T10:22:51","slug":"%e5%a6%82%e4%bd%95%e4%be%9d%e6%ac%a1%e6%89%a7%e8%a1%8c%e5%a4%9a%e4%b8%aapython%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173988.html","title":{"rendered":"\u5982\u4f55\u4f9d\u6b21\u6267\u884c\u591a\u4e2apython\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27050014\/73948781-0ab7-4a2c-9145-b48fccf828b2.webp\" alt=\"\u5982\u4f55\u4f9d\u6b21\u6267\u884c\u591a\u4e2apython\u7a0b\u5e8f\" \/><\/p>\n<p><p>\u8981\u6709\u6548\u5730\u4f9d\u6b21\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\uff0c<strong>\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6\u3001\u5229\u7528Python\u811a\u672c\u3001\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u3001\u6216\u8005\u91c7\u7528\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f<\/strong> \u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e0d\u540c\u7a0b\u5e8f\u7684\u987a\u5e8f\u6267\u884c\u3002 \u5176\u4e2d\uff0c<strong>\u5229\u7528Python\u811a\u672c<\/strong>\u6765\u7ba1\u7406\u7a0b\u5e8f\u6267\u884c\u6d41\u7a0b\u662f\u975e\u5e38\u7075\u6d3b\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u811a\u672c\u4e2d\u8c03\u7528\u5176\u4ed6\u811a\u672c\uff0c\u5bf9\u7a0b\u5e8f\u7684\u6267\u884c\u987a\u5e8f\u8fdb\u884c\u7cbe\u786e\u63a7\u5236\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u5904\u7406\u7a0b\u5e8f\u95f4\u7684\u6570\u636e\u4f20\u9012\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6279\u5904\u7406\u6587\u4ef6\uff08Batch file\uff09\uff0c\u7279\u6307\u5728Windows\u73af\u5883\u4e0b\u7684\u4e00\u79cd\u811a\u672c\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7684\u547d\u4ee4\uff0cWindows\u4f1a\u6309\u987a\u5e8f\u6267\u884c\u6587\u4ef6\u4e2d\u7684\u547d\u4ee4\u3002\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\uff0c\u53ea\u9700\u5c06\u60f3\u8981\u987a\u5e8f\u6267\u884c\u7684Python\u7a0b\u5e8f\u7684\u542f\u52a8\u547d\u4ee4\u9010\u884c\u5199\u5165\u5230\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5c06\u8be5\u6587\u672c\u6587\u4ef6\u7684\u6269\u5c55\u540d\u4fdd\u5b58\u4e3a<code>.bat<\/code>\uff08\u5728Windows\u73af\u5883\uff09\u6216<code>.sh<\/code>\uff08\u5728\u7c7bUnix\u7cfb\u7edf\uff0c\u5982Linux\u6216MacOS\uff09\u3002<\/p>\n<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/li>\n<li>\u5c06\u542f\u52a8Python\u7a0b\u5e8f\u7684\u547d\u4ee4\u9010\u884c\u8f93\u5165\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<p><pre><code>python \u7b2c\u4e00\u4e2a\u7a0b\u5e8f.py<\/p>\n<p>python \u7b2c\u4e8c\u4e2a\u7a0b\u5e8f.py<\/p>\n<p>python \u7b2c\u4e09\u4e2a\u7a0b\u5e8f.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u547d\u540d\u4e3a<code>run_programs.bat<\/code>\u6216<code>run_programs.sh<\/code>\uff0c\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li>\u901a\u8fc7\u53cc\u51fb\u8be5\u6587\u4ef6\u6216\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u5b83\u6765\u8fd0\u884c\u6240\u6709\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u7f3a\u70b9\u662f\u5b83\u5e76\u4e0d\u652f\u6301\u590d\u6742\u7684\u9519\u8bef\u5904\u7406\u6216\u6761\u4ef6\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528PYTHON\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5c06Python\u7a0b\u5e8f\u4f5c\u4e3a\u4e00\u4e2a\u6a21\u5757\u6765\u8c03\u7528\u662f\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e3b\u811a\u672c\uff0c\u5176\u4e2d\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u6765\u5f15\u5165\u5176\u4ed6Python\u811a\u672c\u6216\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u6765\u987a\u5e8f\u6267\u884c\u5404\u4e2a\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u6bd4\u5982\u540d\u4e3a<code>run_all.py<\/code>\u3002<\/li>\n<li>\u5728\u8be5\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u5176\u4ed6\u8981\u6267\u884c\u7684Python\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u3002<\/li>\n<li>\u63a5\u7740\uff0c\u8c03\u7528\u8fd9\u4e9b\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u4e3b\u8981\u529f\u80fd\u6216\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import program1<\/p>\n<p>import program2<\/p>\n<p>import program3<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    program1.main()  # \u5047\u8bbe\u6bcf\u4e2a\u7a0b\u5e8f\u90fd\u6709\u4e00\u4e2amain\u65b9\u6cd5<\/p>\n<p>    program2.main()<\/p>\n<p>    program3.main()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u987a\u5e8f\u6267\u884c\u591a\u4e2a\u7a0b\u5e8f\uff0c\u800c\u4e14\u53ef\u4ee5\u5904\u7406\u6bcf\u4e2a\u7a0b\u5e8f\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\uff0c\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027\u5927\u5927\u589e\u5f3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528<code>os<\/code>\u6216<code>subprocess<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u547d\u4ee4\u6765\u542f\u52a8\u5176\u4ed6\u7a0b\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u9002\u7528\u4e8e\u9700\u8981\u987a\u5e8f\u6267\u884c\u4e0d\u540cPython\u7a0b\u5e8f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u66f4\u7ec6\u81f4\u5730\u63a7\u5236\u547d\u4ee4\u7684\u6267\u884c\u548c\u83b7\u53d6\u7a0b\u5e8f\u7684\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_programs():<\/p>\n<p>    subprocess.run([&quot;python&quot;, &quot;\u7b2c\u4e00\u4e2a\u7a0b\u5e8f.py&quot;])<\/p>\n<p>    subprocess.run([&quot;python&quot;, &quot;\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f.py&quot;])<\/p>\n<p>    subprocess.run([&quot;python&quot;, &quot;\u7b2c\u4e09\u4e2a\u7a0b\u5e8f.py&quot;])<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    run_programs()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u76f4\u63a5\u5728Python\u4ee3\u7801\u4e2d\u7ba1\u7406\u7a0b\u5e8f\u6267\u884c\u7684\u6574\u4e2a\u8fc7\u7a0b\uff0c\u5305\u62ec\u9519\u8bef\u5904\u7406\u548c\u7a0b\u5e8f\u8f93\u51fa\u7684\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u91c7\u7528\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b9a\u671f\u6267\u884c\u6216\u9700\u6309\u7279\u5b9a\u6761\u4ef6\u89e6\u53d1\u6267\u884c\u7684Python\u7a0b\u5e8f\uff0c\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f\u662f\u4e00\u4e2a\u5408\u7406\u7684\u9009\u62e9\u3002Windows\u7684\u8ba1\u5212\u4efb\u52a1\uff08Task Scheduler\uff09\u548cLinux\u7684cron\u4f5c\u4e1a\u90fd\u662f\u7ba1\u7406\u4efb\u52a1\u6267\u884c\u7684\u6709\u529b\u5de5\u5177\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e<strong>Windows<\/strong>\u73af\u5883\uff0c\u53ef\u4ee5\u901a\u8fc7\u201c\u8ba1\u5212\u4efb\u52a1\u201d\u6765\u8bbe\u7f6e\u4efb\u52a1\u7684\u89e6\u53d1\u6761\u4ef6\u3001\u6267\u884c\u7a0b\u5e8f\u4ee5\u53ca\u76f8\u5173\u53c2\u6570\u3002<\/li>\n<li>\u5bf9\u4e8e<strong>Linux<\/strong>\u7cfb\u7edf\uff0c<code>cron<\/code>\u8868\u8fbe\u5f0f\u80fd\u591f\u5b9a\u4e49\u4efb\u52a1\u7684\u6267\u884c\u9891\u7387\u548c\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u81ea\u52a8\u5316\u7684\u957f\u671f\u6216\u5468\u671f\u6027\u4efb\u52a1\u6267\u884c\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u4e00\u6b21\u6027\u4efb\u52a1\u4e32\u8054\u53ef\u80fd\u5c31\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4f9d\u6b21\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u3001\u529f\u80fd\u9700\u6c42\u4ee5\u53ca\u4e2a\u4eba\u7684\u7f16\u7a0b\u4e60\u60ef\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u8fbe\u5230\u6700\u4f18\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\uff1f<\/strong><br \/>\n\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u53ef\u4ee5\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\uff1a<\/p>\n<ul>\n<li>\u786e\u5b9a\u8981\u6267\u884c\u7684Python\u7a0b\u5e8f\u7684\u987a\u5e8f\u3002<\/li>\n<li>\u5728\u547d\u4ee4\u884c\u542f\u52a8\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f7f\u7528<code>python program1.py<\/code>\u547d\u4ee4\u3002<\/li>\n<li>\u7b49\u5f85\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u6267\u884c\u5b8c\u6210\u540e\uff0c\u518d\u542f\u52a8\u4e0b\u4e00\u4e2a\u7a0b\u5e8f\u3002\u4f7f\u7528\u76f8\u540c\u7684\u547d\u4ee4\u683c\u5f0f\u8fd0\u884c\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f\uff0c\u5982<code>python program2.py<\/code>\u3002<\/li>\n<li>\u4f9d\u6b64\u7c7b\u63a8\uff0c\u6309\u9700\u8981\u6267\u884c\u6240\u6709\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6211\u60f3\u4f9d\u6b21\u5728\u547d\u4ee4\u884c\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\uff0c\u6709\u6ca1\u6709\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6279\u5904\u7406\u811a\u672c\u6216\u8005\u4e00\u4e2ashell\u811a\u672c\u6765\u81ea\u52a8\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u6279\u5904\u7406\u811a\u672c\uff08\u5bf9\u4e8eWindows\u7cfb\u7edf\uff09\uff1a<\/p>\n<pre><code>@echo off\necho \u6b63\u5728\u6267\u884c\u7b2c\u4e00\u4e2a\u7a0b\u5e8f...\npython program1.py\n\necho \u6b63\u5728\u6267\u884c\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f...\npython program2.py\n\necho \u6240\u6709\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u3002\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u540d\u79f0\u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684\u7a0b\u5e8f\uff0c\u5e76\u4f9d\u6b64\u7c7b\u63a8\u6dfb\u52a0\u66f4\u591a\u7684<code>echo<\/code>\u548c<code>python<\/code>\u547d\u4ee4\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728Python\u4e2d\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u4f9d\u6b21\u6267\u884c\u591a\u4e2a\u7a0b\u5e8f\uff1f<\/strong><br \/>\n\u4f60\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u5728Python\u4e2d\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2a\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\n# \u6267\u884c\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\nsubprocess.run([&#039;python&#039;, &#039;program1.py&#039;])\n\n# \u6267\u884c\u7b2c\u4e8c\u4e2a\u7a0b\u5e8f\nsubprocess.run([&#039;python&#039;, &#039;program2.py&#039;])\n\n# \u6dfb\u52a0\u66f4\u591a\u7684\u7a0b\u5e8f\u6267\u884c\u547d\u4ee4\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u7a0b\u5e8f\u6267\u884c\u547d\u4ee4\uff0c\u786e\u4fdd\u5b83\u4eec\u4ee5\u6b63\u786e\u7684\u987a\u5e8f\u6267\u884c\u3002\u8fd0\u884c\u811a\u672c\u540e\uff0c\u6240\u6709\u7a0b\u5e8f\u4f1a\u4f9d\u6b21\u6267\u884c\u5b8c\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u6709\u6548\u5730\u4f9d\u6b21\u6267\u884c\u591a\u4e2aPython\u7a0b\u5e8f\uff0c\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6\u3001\u5229\u7528Python\u811a\u672c\u3001\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u547d\u4ee4\u3001\u6216\u8005\u91c7\u7528\u4efb\u52a1\u8c03\u5ea6 [&hellip;]","protected":false},"author":3,"featured_media":173998,"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\/173988"}],"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=173988"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173998"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}