{"id":962933,"date":"2024-12-27T04:12:58","date_gmt":"2024-12-26T20:12:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962933.html"},"modified":"2024-12-27T04:13:00","modified_gmt":"2024-12-26T20:13:00","slug":"sh%e8%84%9a%e6%9c%ac%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/962933.html","title":{"rendered":"sh\u811a\u672c\u5982\u4f55\u8c03\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180510\/86f67aea-e1d2-4874-8577-c3d86922746c.webp\" alt=\"sh\u811a\u672c\u5982\u4f55\u8c03\u7528python\" \/><\/p>\n<p><p> \u5728Linux\u6216Unix\u73af\u5883\u4e2d\uff0c<strong>sh\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8c03\u7528Python\u811a\u672c<\/strong>\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528shebang (<code>#!<\/code>) \u6307\u5b9aPython\u89e3\u91ca\u5668\u8def\u5f84\u3001\u4ee5\u53ca\u5c06Python\u4ee3\u7801\u5d4c\u5165\u5230sh\u811a\u672c\u4e2d\u3002<strong>\u4f7f\u7528shebang\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u662f\u6700\u4e3a\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u5728sh\u811a\u672c\u4e2d\u901a\u8fc7\u547d\u4ee4\u884c\u65b9\u5f0f\u8c03\u7528Python\u811a\u672c\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u5bb9\u6613\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>python3<\/code>\u547d\u4ee4\u7528\u4e8e\u8c03\u7528Python\u89e3\u91ca\u5668\u6267\u884c\u4f4d\u4e8e<code>\/path\/to\/script.py<\/code>\u7684Python\u811a\u672c\u3002\u786e\u4fdd\u60a8\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14\u5df2\u5c06Python\u8def\u5f84\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Shebang (<code>#!<\/code>)<\/p>\n<\/p>\n<p><p>\u4f7f\u7528shebang\u662f\u8c03\u7528Python\u811a\u672c\u7684\u53e6\u4e00\u79cd\u5e38\u89c1\u65b9\u5f0f\u3002shebang\u6307\u5b9a\u4e86\u811a\u672c\u7684\u89e3\u91ca\u5668\u8def\u5f84\uff0c\u4f7f\u5f97\u811a\u672c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u800c\u65e0\u9700\u663e\u5f0f\u8c03\u7528\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/usr\/bin\/env python3<\/p>\n<p>print(&quot;Hello from Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u662f\u811a\u672c\u53d8\u5f97\u66f4\u52a0\u53ef\u79fb\u690d\uff0c\u56e0\u4e3a<code>\/usr\/bin\/env<\/code>\u4f1a\u81ea\u52a8\u67e5\u627ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84\uff0c\u786e\u4fdd\u811a\u672c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5c06Python\u4ee3\u7801\u5d4c\u5165\u5230sh\u811a\u672c\u4e2d<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5728sh\u811a\u672c\u4e2d\u76f4\u63a5\u5d4c\u5165Python\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u521b\u5efa\u5355\u72ecPython\u6587\u4ef6\u7684\u60c5\u51b5\u4e0b\u6267\u884cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>python3 - &lt;&lt;END<\/p>\n<p>print(&quot;Hello from embedded Python code!&quot;)<\/p>\n<p>END<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cPython\u4ee3\u7801\u88ab\u5d4c\u5165\u5230sh\u811a\u672c\u4e2d\uff0c\u901a\u8fc7<code>python3 - &lt;&lt;END<\/code>\u7684\u65b9\u5f0f\u6267\u884c\u3002<code>&lt;&lt;END<\/code>\u8868\u793a\u8f93\u5165\u7ed3\u675f\u7b26\uff0c<code>END<\/code>\u53ef\u4ee5\u66ff\u6362\u4e3a\u4efb\u610f\u6807\u8bc6\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u901a\u8fc7\u7ba1\u9053\u4f20\u9012\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u7ba1\u9053\u5728sh\u548cPython\u811a\u672c\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e24\u79cd\u811a\u672c\u8bed\u8a00\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>echo &quot;Data from shell script&quot; | python3 -c &quot;import sys; print(sys.stdin.read())&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0csh\u811a\u672c\u901a\u8fc7\u7ba1\u9053\u5c06\u6570\u636e\u4f20\u9012\u7ed9Python\u811a\u672c\uff0cPython\u811a\u672c\u8bfb\u53d6\u6807\u51c6\u8f93\u5165\u5e76\u8f93\u51fa\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8e\u9700\u8981\u5728sh\u811a\u672c\u548cPython\u811a\u672c\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u6570\u636e<\/p>\n<\/p>\n<p><p>\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u6570\u636e\u4ecesh\u811a\u672c\u4f20\u9012\u5230Python\u811a\u672c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4f20\u9012\u7b80\u5355\u53c2\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>python3 \/path\/to\/script.py arg1 arg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>sys.argv<\/code>\u8bbf\u95ee\u4f20\u9012\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Arguments received:&quot;, sys.argv[1:])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5728sh\u811a\u672c\u4e2d\u8c03\u7528Python\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u4ecesh\u811a\u672c\u4e2d\u8c03\u7528\u7279\u5b9a\u7684Python\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728Python\u811a\u672c\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5165\u53e3\u51fd\u6570\uff0c\u7136\u540e\u5728sh\u811a\u672c\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728Python\u811a\u672c\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;This is a Python function&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728sh\u811a\u672c\u4e2d\u8c03\u7528\u8be5Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ecesh\u811a\u672c\u8c03\u7528\u7279\u5b9aPython\u903b\u8f91\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u4f20\u9012\u6570\u636e<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u60a8\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u5728sh\u811a\u672c\u548cPython\u811a\u672c\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728sh\u811a\u672c\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u5728Python\u811a\u672c\u4e2d\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>export DATA=&quot;Data from shell script&quot;<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os.environ<\/code>\u8bbf\u95ee\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(&quot;Data received:&quot;, os.environ.get(&#39;DATA&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u6587\u4ef6\u4f20\u9012\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u4f20\u9012\u5927\u91cf\u6570\u636e\u6216\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u573a\u666f\u4e0b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u5728sh\u811a\u672c\u548cPython\u811a\u672c\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u9760\u4e14\u6613\u4e8e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/sh<\/p>\n<p>echo &quot;Data from shell script&quot; &gt; data.txt<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p>    print(&quot;Data received:&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728sh\u811a\u672c\u4e2d\u8c03\u7528Python\u811a\u672c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528shebang\u6307\u5b9a\u89e3\u91ca\u5668\u8def\u5f84\u3001\u5c06Python\u4ee3\u7801\u5d4c\u5165\u5230sh\u811a\u672c\u4e2d<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7ba1\u9053\u3001\u547d\u4ee4\u884c\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u548c\u6587\u4ef6\u5728sh\u811a\u672c\u548cPython\u811a\u672c\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u811a\u672c\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u589e\u5f3a\u811a\u672c\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728sh\u811a\u672c\u4e2d\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5728sh\u811a\u672c\u4e2d\u901a\u8fc7\u8c03\u7528Python\u89e3\u91ca\u5668\u6765\u6267\u884cPython\u811a\u672c\u3002\u53ea\u9700\u5728sh\u6587\u4ef6\u4e2d\u4f7f\u7528<code>python<\/code>\u6216<code>python3<\/code>\u547d\u4ee4\uff0c\u540e\u9762\u8ddf\u4e0aPython\u811a\u672c\u7684\u8def\u5f84\u5373\u53ef\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-sh\">#!\/bin\/sh\npython3 \/path\/to\/your_script.py\n<\/code><\/pre>\n<p>\u786e\u4fdd\u811a\u672c\u5177\u6709\u6267\u884c\u6743\u9650\uff0c\u5e76\u4e14Python\u89e3\u91ca\u5668\u5728\u60a8\u7684\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728sh\u811a\u672c\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u53c2\u6570\u4ecesh\u811a\u672c\u4f20\u9012\u7ed9Python\u811a\u672c\u3002\u5728sh\u811a\u672c\u4e2d\uff0c\u60a8\u53ea\u9700\u5728\u8c03\u7528Python\u811a\u672c\u65f6\u6dfb\u52a0\u53c2\u6570\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-sh\">#!\/bin\/sh\npython3 \/path\/to\/your_script.py arg1 arg2\n<\/code><\/pre>\n<p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u6765\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\uff0c<code>sys.argv[0]<\/code>\u662f\u811a\u672c\u540d\uff0c<code>sys.argv[1]<\/code>\u662f\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728sh\u811a\u672c\u4e2d\u5904\u7406Python\u811a\u672c\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u5728sh\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u66ff\u6362\u6216\u7ba1\u9053\u6765\u6355\u83b7Python\u811a\u672c\u7684\u8f93\u51fa\u3002\u4f7f\u7528\u53cd\u5f15\u53f7\u6216<code>$()<\/code>\u8bed\u6cd5\u53ef\u4ee5\u5c06Python\u7684\u8f93\u51fa\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-sh\">#!\/bin\/sh\noutput=$(python3 \/path\/to\/your_script.py)\necho &quot;Python script output: $output&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728sh\u811a\u672c\u4e2d\u5904\u7406\u548c\u4f7f\u7528Python\u811a\u672c\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Linux\u6216Unix\u73af\u5883\u4e2d\uff0csh\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8c03\u7528Python\u811a\u672c\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8c03\u7528Python [&hellip;]","protected":false},"author":3,"featured_media":962936,"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\/962933"}],"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=962933"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962933\/revisions"}],"predecessor-version":[{"id":962940,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962933\/revisions\/962940"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962936"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}