{"id":1179640,"date":"2025-01-15T18:26:30","date_gmt":"2025-01-15T10:26:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179640.html"},"modified":"2025-01-15T18:26:33","modified_gmt":"2025-01-15T10:26:33","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85%e6%a0%87%e5%87%86%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179640.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b89\u88c5\u6807\u51c6\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25114021\/6c9c8eb2-741e-45a0-8972-4ee6a1922d7a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b89\u88c5\u6807\u51c6\u5e93\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6807\u51c6\u5e93\u5df2\u7ecf\u968fPython\u7684\u5b89\u88c5\u4e00\u8d77\u63d0\u4f9b\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/strong>\u3001<strong>\u5982\u679c\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684\u6807\u51c6\u5e93\u6216\u8005\u6269\u5c55\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5<\/strong>\u3001<strong>\u6ce8\u610f\u533a\u5206\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6807\u51c6\u5e93\u7684\u6982\u5ff5\u4e0e\u7279\u70b9<\/h3>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u662f\u6307Python\u89e3\u91ca\u5668\u968f\u9644\u7684\u4e00\u7ec4\u6a21\u5757\u548c\u5305\uff0c\u8fd9\u4e9b\u6a21\u5757\u548c\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5e38\u89c1\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4f7f\u5f97Python\u5f00\u53d1\u8005\u65e0\u9700\u989d\u5916\u5b89\u88c5\u5373\u53ef\u4f7f\u7528\u3002\u6807\u51c6\u5e93\u5305\u62ec\u4e86\u6587\u4ef6I\/O\u64cd\u4f5c\u3001\u7cfb\u7edf\u8c03\u7528\u3001\u6570\u636e\u7ed3\u6784\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u6587\u672c\u5904\u7406\u3001\u6570\u5b66\u8ba1\u7b97\u7b49\u5404\u4e2a\u65b9\u9762\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u63d0\u4f9b<\/strong>\uff1aPython\u5b89\u88c5\u5305\u81ea\u5e26\uff0c\u65e0\u9700\u989d\u5916\u4e0b\u8f7d\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u517c\u5bb9<\/strong>\uff1a\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8868\u73b0\u4e00\u81f4\u3002<\/li>\n<li><strong>\u4e30\u5bcc\u4e14\u7a33\u5b9a<\/strong>\uff1a\u529f\u80fd\u4e30\u5bcc\u4e14\u7ecf\u8fc7\u957f\u671f\u7ef4\u62a4\uff0c\u7a33\u5b9a\u53ef\u9760\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u4f7f\u7528\u6807\u51c6\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u6807\u51c6\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<p>import math<\/p>\n<p>print(os.getcwd())  # \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p>print(sys.version)  # \u83b7\u53d6Python\u7248\u672c\u4fe1\u606f<\/p>\n<p>print(math.sqrt(16))  # \u8ba1\u7b97\u5e73\u65b9\u6839<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u66f4\u65b0\u548c\u5b89\u88c5\u6807\u51c6\u5e93<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u6807\u51c6\u5e93\u968fPython\u5b89\u88c5\u5305\u63d0\u4f9b\uff0c\u4f46\u6709\u65f6\u53ef\u80fd\u9700\u8981\u66f4\u65b0\u6216\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5e93\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u65b0\u7248\u672c\u63d0\u4f9b\u4e86\u4fee\u590d\u6216\u65b0\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528pip\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u6807\u51c6\u5e93<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u5927\u90e8\u5206\u6807\u51c6\u5e93\u4e0d\u9700\u8981\u66f4\u65b0\uff0c\u4f46\u6709\u4e9b\u6269\u5c55\u5e93\u53ef\u80fd\u9700\u8981\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\u3002\u4f8b\u5982\uff0c<code>dataclasses<\/code>\u6a21\u5757\u5728Python 3.7\u5f15\u5165\uff0c\u4f46\u5728\u4f4e\u7248\u672c\u4e2d\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install dataclasses<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5de5\u5177\u6765\u66f4\u65b0Python\u53ca\u5176\u6807\u51c6\u5e93\u3002\u4f8b\u5982\uff0c\u5728Debian\/Ubuntu\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>apt-get<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u5df2\u7ecf\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5b8c\u6210\u67d0\u4e9b\u7279\u5b9a\u4efb\u52a1\u3002\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u8fdb\u884c\u5b89\u88c5\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u975e\u5e38\u7b80\u5355\u3002\u4f8b\u5982\uff0c\u5b89\u88c5<code>requests<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5347\u7ea7\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5347\u7ea7\u5df2\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7ba1\u7406\u9879\u76ee\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fbf\u4e8e\u9879\u76ee\u7684\u4f9d\u8d56\u7ba1\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u5217\u51fa\u4e86\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u7b2c\u4e09\u65b9\u5e93\u53ca\u5176\u7248\u672c\u3002\u521b\u5efa<code>requirements.txt<\/code>\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u7248\u672c\u51b2\u7a81<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u4e0d\u540c\u7684\u5e93\u53ef\u80fd\u4f9d\u8d56\u4e8e\u4e0d\u540c\u7248\u672c\u7684\u540c\u4e00\u5e93\uff0c\u5bfc\u81f4\u7248\u672c\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08virtual environment\uff09\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # \u5728Windows\u4e0a\u4f7f\u7528 myenv\\Scripts\\activate<\/p>\n<p>pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e93\u7684\u517c\u5bb9\u6027<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u5e93\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u4e0a\u53ef\u80fd\u8868\u73b0\u4e0d\u540c\u6216\u4e0d\u517c\u5bb9\u3002\u5728\u5b89\u88c5\u5e93\u4e4b\u524d\uff0c\u52a1\u5fc5\u68c0\u67e5\u5e93\u7684\u6587\u6863\uff0c\u4ee5\u786e\u4fdd\u5176\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u529f\u80fd\u652f\u6301\uff0c\u5927\u5927\u7b80\u5316\u4e86\u5f00\u53d1\u8fc7\u7a0b\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u5404\u79cd\u7f16\u7a0b\u4efb\u52a1\u3002\u5728\u4f7f\u7528\u548c\u7ba1\u7406\u8fd9\u4e9b\u5e93\u65f6\uff0c\u9700\u6ce8\u610f\u7248\u672c\u517c\u5bb9\u6027\u548c\u4f9d\u8d56\u7ba1\u7406\uff0c\u786e\u4fdd\u9879\u76ee\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u8981\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>Python\u6807\u51c6\u5e93\u968fPython\u5b89\u88c5\u5305\u63d0\u4f9b\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u3002<\/strong><\/li>\n<li><strong>\u6807\u51c6\u5e93\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u6269\u5c55\u5e93\u3002<\/strong><\/li>\n<li><strong>\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\u548c\u7ba1\u7406\uff0c\u4f7f\u7528requirements.txt\u6587\u4ef6\u4fbf\u4e8e\u9879\u76ee\u4f9d\u8d56\u7ba1\u7406\u3002<\/strong><\/li>\n<li><strong>\u4f7f\u7528\u865a\u62df\u73af\u5883\u89e3\u51b3\u7248\u672c\u51b2\u7a81\u95ee\u9898\u3002<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u5185\u5bb9\u7684\u638c\u63e1\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u5730\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u8ba4\u6211\u7684Python\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u6807\u51c6\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6807\u51c6\u5e93\u901a\u5e38\u968fPython\u7684\u5b89\u88c5\u800c\u81ea\u52a8\u5305\u542b\u3002\u8981\u786e\u8ba4\u662f\u5426\u5df2\u5b89\u88c5\u6807\u51c6\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u6216Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165 <code>import<\/code> \u8bed\u53e5\u6765\u68c0\u67e5\u7279\u5b9a\u6a21\u5757\uff0c\u4f8b\u5982 <code>import os<\/code> \u6216 <code>import sys<\/code>\u3002\u5982\u679c\u6ca1\u6709\u9519\u8bef\u63d0\u793a\uff0c\u8bf4\u660e\u76f8\u5e94\u7684\u6807\u51c6\u5e93\u5df2\u7ecf\u5b89\u88c5\u5e76\u53ef\u7528\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u6807\u51c6\u5e93\uff0c\u5982\u4f55\u67e5\u627e\u5176\u6587\u6863\u548c\u7528\u6cd5\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u6587\u6863\u975e\u5e38\u5168\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u5b98\u65b9\u6587\u6863\u7f51\u7ad9\uff08\u5982docs.python.org\uff09\u6765\u83b7\u53d6\u8be6\u7ec6\u4fe1\u606f\u3002\u6bcf\u4e2a\u6a21\u5757\u90fd\u5305\u542b\u4f7f\u7528\u793a\u4f8b\u548cAPI\u8bf4\u660e\uff0c\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e38\u89c1\u7684\u6807\u51c6\u5e93\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5f00\u53d1\u9700\u6c42\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u6db5\u76d6\u591a\u4e2a\u9886\u57df\uff0c\u5e38\u89c1\u7684\u5e93\u5305\u62ec\uff1a<code>os<\/code>\uff08\u64cd\u4f5c\u7cfb\u7edf\u63a5\u53e3\uff09\u3001<code>sys<\/code>\uff08Python\u89e3\u91ca\u5668\u76f8\u5173\u529f\u80fd\uff09\u3001<code>datetime<\/code>\uff08\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\uff09\u3001<code>json<\/code>\uff08JSON\u6570\u636e\u5904\u7406\uff09\u3001<code>re<\/code>\uff08\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\uff09\u7b49\u3002\u6839\u636e\u5177\u4f53\u7684\u5f00\u53d1\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u76f8\u5e94\u7684\u6a21\u5757\u6765\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6807\u51c6\u5e93\u5df2\u7ecf\u968fPython\u7684\u5b89\u88c5\u4e00\u8d77\u63d0\u4f9b\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u3001\u5982\u679c\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684\u6807\u51c6\u5e93\u6216\u8005\u6269\u5c55 [&hellip;]","protected":false},"author":3,"featured_media":1179648,"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\/1179640"}],"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=1179640"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179640\/revisions"}],"predecessor-version":[{"id":1179651,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179640\/revisions\/1179651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}