{"id":1009491,"date":"2024-12-27T11:12:13","date_gmt":"2024-12-27T03:12:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009491.html"},"modified":"2024-12-27T11:12:16","modified_gmt":"2024-12-27T03:12:16","slug":"shell%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009491.html","title":{"rendered":"shell\u5982\u4f55\u8c03\u7528python\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084412\/97e679ba-9cf6-434b-9f1c-b688534b1ce9.webp\" alt=\"shell\u5982\u4f55\u8c03\u7528python\u811a\u672c\" \/><\/p>\n<p><p> \u5728Shell\u4e2d\u8c03\u7528Python\u811a\u672c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528shebang\u884c\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8c03\u7528<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python\u89e3\u91ca\u5668\u6765\u8fd0\u884cPython\u811a\u672c\u3002<strong>\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\u3001\u6613\u4e8e\u7406\u89e3\uff0c\u7f3a\u70b9\u662f\u5728\u591a\u4e2a\u73af\u5883\u4e2d\u4f7f\u7528\u65f6\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u4e0d\u540c\u7684Python\u7248\u672c<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-shell\">python script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6709\u591a\u4e2aPython\u7248\u672c\uff0c\u53ef\u4ee5\u6307\u5b9a\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python3 script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528\u89e3\u91ca\u5668\u7684\u65b9\u5f0f\u975e\u5e38\u65b9\u4fbf\uff0c\u56e0\u4e3a\u51e0\u4e4e\u6240\u6709\u7684Linux\u548cMacOS\u7cfb\u7edf\u90fd\u9884\u88c5\u4e86Python\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u547d\u4ee4\u5373\u53ef\u6267\u884c\u811a\u672c\u3002\u5982\u679c\u4f60\u7684\u811a\u672c\u53ea\u9700\u8981\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u5982\u679c\u9700\u8981\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u811a\u672c\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u811a\u672c\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e2d\u53ef\u80fd\u53ea\u6709Python 2\uff0c\u800c\u5728\u5176\u4ed6\u7cfb\u7edf\u4e2d\u53ef\u80fd\u53ea\u6709Python 3\u3002\u56e0\u6b64\uff0c\u6307\u5b9aPython\u7248\u672c\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Shebang\u884c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Shebang\u884c\u5141\u8bb8Python\u811a\u672c\u50cfShell\u811a\u672c\u4e00\u6837\u76f4\u63a5\u8fd0\u884c\uff0c<strong>\u4f18\u70b9\u662f\u7b80\u5316\u4e86\u6267\u884c\u8fc7\u7a0b\uff0c\u7f3a\u70b9\u662f\u9700\u8981\u786e\u4fdd\u811a\u672c\u7684\u53ef\u6267\u884c\u6743\u9650<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u6307\u5b9a\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8d4b\u4e88\u53ef\u6267\u884c\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chmod +x script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">.\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>Shebang\u884c\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6ce8\u91ca\u884c\uff0c\u7528\u4e8e\u544a\u8bc9\u7cfb\u7edf\u4f7f\u7528\u54ea\u4e2a\u89e3\u91ca\u5668\u6765\u6267\u884c\u811a\u672c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7fPython\u811a\u672c\u50cfShell\u811a\u672c\u4e00\u6837\u76f4\u63a5\u8fd0\u884c\uff0c\u65e0\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u663e\u5f0f\u8c03\u7528Python\u89e3\u91ca\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4e00\u4e2a\u4e3b\u8981\u4f18\u70b9\u662f\u7b80\u5316\u4e86\u6267\u884c\u8fc7\u7a0b\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9891\u7e41\u6267\u884c\u811a\u672c\u65f6\u3002\u7136\u800c\uff0c\u9700\u8981\u786e\u4fdd\u811a\u672c\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\uff0c\u8fd9\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6b65\u9aa4\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e2d\uff0c<code>\/usr\/bin\/env<\/code>\u547d\u4ee4\u7684\u4f4d\u7f6e\u53ef\u80fd\u4e0d\u540c\uff0c\u56e0\u6b64\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8c03\u7528Python\u811a\u672c\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c<strong>\u4f18\u70b9\u662f\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684Python\u73af\u5883\uff0c\u7f3a\u70b9\u662f\u5728\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">export PYTHONPATH=\/path\/to\/your\/scripts<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python -m script<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u6307\u5b9aPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u548c\u6a21\u5757\u7684\u67e5\u627e\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5728\u590d\u6742\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u53ef\u4ee5\u8f7b\u677e\u5207\u6362\u4e0d\u540c\u7684Python\u73af\u5883\u3002\u4f8b\u5982\uff0c\u5728\u5f00\u53d1\u548c\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684Python\u6216\u4e0d\u540c\u7684\u4f9d\u8d56\u5305\u3002\u901a\u8fc7\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u8fd9\u4e9b\u5dee\u5f02\u3002\u7136\u800c\uff0c\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u591a\u4e2a\u9879\u76ee\u4e4b\u95f4\u5207\u6362\u65f6\uff0c\u9700\u8981\u5c0f\u5fc3\u7ba1\u7406\u73af\u5883\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Shell\u811a\u672c\u5c01\u88c5Python\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199Shell\u811a\u672c\u6765\u5c01\u88c5Python\u811a\u672c\u7684\u8c03\u7528\uff0c\u4ece\u800c\u7b80\u5316\u64cd\u4f5c\u3002<strong>\u4f18\u70b9\u662f\u53ef\u4ee5\u6279\u91cf\u7ba1\u7406\u548c\u6267\u884c\u591a\u4e2aPython\u811a\u672c\uff0c\u7f3a\u70b9\u662f\u589e\u52a0\u4e86\u590d\u6742\u6027<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aShell\u811a\u672c\uff0c\u4f8b\u5982<code>run.sh<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">#!\/bin\/bash<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8d4b\u4e88\u53ef\u6267\u884c\u6743\u9650\u5e76\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">chmod +x run.sh<\/p>\n<p>.\/run.sh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Shell\u811a\u672c\u5c01\u88c5Python\u811a\u672c\u8c03\u7528\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2aPython\u811a\u672c\u7684\u6267\u884c\u6574\u5408\u5230\u4e00\u4e2a\u6279\u5904\u7406\u4e2d\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5904\u7406\u6216\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u987a\u5e8f\u6267\u884c\u591a\u4e2aPython\u811a\u672c\u3002\u901a\u8fc7Shell\u811a\u672c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5e76\u6dfb\u52a0\u989d\u5916\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u6761\u4ef6\u5224\u65ad\u548c\u9519\u8bef\u5904\u7406\u3002\u7136\u800c\uff0c\u8fd9\u4e5f\u589e\u52a0\u4e86\u7cfb\u7edf\u7684\u590d\u6742\u6027\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u7ef4\u62a4\u548c\u66f4\u65b0\u591a\u4e2a\u811a\u672c\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6bcf\u4e2a\u811a\u672c\u7684\u8def\u5f84\u548c\u53c2\u6570\u90fd\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Shell\u4e2d\u8c03\u7528Python\u811a\u672c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002\u5728\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230\u7cfb\u7edf\u73af\u5883\u3001\u811a\u672c\u7684\u590d\u6742\u6027\u548c\u6267\u884c\u9891\u7387\u7b49\u56e0\u7d20\u3002<strong>\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\uff0c\u4f7f\u7528Shebang\u884c\u53ef\u4ee5\u7b80\u5316\u811a\u672c\u6267\u884c\uff0c\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u8c03\u7528\u9002\u5408\u590d\u6742\u9879\u76ee\uff0c\u4f7f\u7528Shell\u811a\u672c\u5c01\u88c5\u5219\u4fbf\u4e8e\u6279\u91cf\u7ba1\u7406<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Shell\u4e2d\u6267\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Shell\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14\u811a\u672c\u6587\u4ef6\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>python script.py<\/code>\u6216<code>python3 script.py<\/code>\u6765\u6267\u884c\u811a\u672c\uff0c\u5176\u4e2d<code>script.py<\/code>\u662f\u4f60\u7684Python\u6587\u4ef6\u540d\u3002\u5982\u679c\u4f60\u7684\u811a\u672c\u5305\u542b\u53ef\u6267\u884c\u6743\u9650\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c<code>.\/script.py<\/code>\u3002<\/p>\n<p><strong>Python\u811a\u672c\u9700\u8981\u4ec0\u4e48\u73af\u5883\u624d\u80fd\u5728Shell\u4e2d\u8fd0\u884c\uff1f<\/strong><br \/>Python\u811a\u672c\u5728Shell\u4e2d\u8fd0\u884c\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5e94\u7248\u672c\u7684Python\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u6765\u786e\u8ba4\u5b89\u88c5\u3002\u5982\u679c\u811a\u672c\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd8\u9700\u8981\u4f7f\u7528<code>pip install package_name<\/code>\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u4ee5\u786e\u4fdd\u811a\u672c\u80fd\u591f\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Shell\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff1f<\/strong><br \/>\u5728Shell\u4e2d\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\u3002\u6267\u884c\u547d\u4ee4\u65f6\uff0c\u76f4\u63a5\u5728\u811a\u672c\u540d\u540e\u9762\u6dfb\u52a0\u53c2\u6570\uff0c\u4f8b\u5982<code>python script.py arg1 arg2<\/code>\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u5217\u8868\u6765\u83b7\u53d6\u8fd9\u4e9b\u53c2\u6570\uff0c<code>sys.argv[0]<\/code>\u662f\u811a\u672c\u540d\uff0c\u540e\u7eed\u7684\u5143\u7d20\u662f\u4f20\u9012\u7684\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5411\u811a\u672c\u63d0\u4f9b\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Shell\u4e2d\u8c03\u7528Python\u811a\u672c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528shebang\u884c\u3001\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1009501,"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\/1009491"}],"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=1009491"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009491\/revisions"}],"predecessor-version":[{"id":1009505,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009491\/revisions\/1009505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009501"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}