{"id":1185895,"date":"2025-01-15T19:46:22","date_gmt":"2025-01-15T11:46:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185895.html"},"modified":"2025-01-15T19:46:24","modified_gmt":"2025-01-15T11:46:24","slug":"python%e9%87%8c%e7%9a%84%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185895.html","title":{"rendered":"python\u91cc\u7684\u4ee3\u7801\u5982\u4f55\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134839\/7f5a900a-fd5a-457f-b7ac-2321e75fbf5f.webp\" alt=\"python\u91cc\u7684\u4ee3\u7801\u5982\u4f55\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>Python\u4ee3\u7801\u7684\u8fd0\u884c\u65b9\u5f0f\u5305\u62ec\u89e3\u91ca\u6267\u884c\u3001\u7f16\u8bd1\u6267\u884c\u3001\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3001\u811a\u672c\u6587\u4ef6\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u7b49\u591a\u79cd\u65b9\u5f0f\u3002\u5728\u8fd9\u4e9b\u65b9\u5f0f\u4e2d\uff0c\u4f7f\u7528\u811a\u672c\u6587\u4ef6\u548cIDE\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u3002<\/strong><\/p>\n<\/p>\n<p><h4>\u89e3\u91ca\u6267\u884c<\/h4>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740Python\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u7531\u89e3\u91ca\u5668\u9010\u884c\u89e3\u91ca\u548c\u6267\u884c\u3002\u4e0e\u7f16\u8bd1\u578b\u8bed\u8a00\u4e0d\u540c\uff0cPython\u4ee3\u7801\u4e0d\u9700\u8981\u5728\u8fd0\u884c\u4e4b\u524d\u8fdb\u884c\u7f16\u8bd1\u3002\u89e3\u91ca\u5668\u9010\u884c\u8bfb\u53d6Python\u4ee3\u7801\uff0c\u8fdb\u884c\u8bed\u6cd5\u68c0\u67e5\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u5feb\u901f\u6d4b\u8bd5\u548c\u8c03\u8bd5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u8bd1\u6267\u884c<\/h4>\n<\/p>\n<p><p>\u867d\u7136Python\u662f\u4e00\u79cd\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u7f16\u8bd1\u6267\u884c\u3002Python\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u89e3\u91ca\u5668\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\uff08bytecode\uff09\uff0c\u7136\u540e\u7531Python\u865a\u62df\u673a\uff08PVM\uff09\u6267\u884c\u3002\u5b57\u8282\u7801\u662f\u4e00\u79cd\u4e2d\u95f4\u8bed\u8a00\uff0c\u5b83\u6bd4\u6e90\u4ee3\u7801\u66f4\u63a5\u8fd1\u673a\u5668\u8bed\u8a00\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002Python\u7684\u7f16\u8bd1\u8fc7\u7a0b\u662f\u81ea\u52a8\u8fdb\u884c\u7684\uff0c\u5f00\u53d1\u8005\u4e0d\u9700\u8981\u624b\u52a8\u5e72\u9884\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff08Interactive Interpreter\uff09\uff0c\u4e5f\u88ab\u79f0\u4e3aREPL\uff08Read-Eval-Print Loop\uff09\u3002\u901a\u8fc7\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9010\u884c\u8f93\u5165Python\u4ee3\u7801\u5e76\u7acb\u5373\u770b\u5230\u6267\u884c\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u5feb\u901f\u6d4b\u8bd5\u4ee3\u7801\u7247\u6bb5\u548c\u5b66\u4e60Python\u8bed\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u542f\u52a8\u548c\u4f7f\u7528\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u754c\u9762\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\u5373\u53ef\u542f\u52a8\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3002\u542f\u52a8\u540e\uff0c\u4f1a\u663e\u793aPython\u7684\u7248\u672c\u4fe1\u606f\u548c\u4e00\u4e2a\u63d0\u793a\u7b26\uff08<code>&gt;&gt;&gt;<\/code>\uff09\u3002\u5728\u63d0\u793a\u7b26\u4e0b\u8f93\u5165\u4ee3\u7801\u5e76\u6309\u56de\u8f66\u952e\uff0c\u89e3\u91ca\u5668\u4f1a\u7acb\u5373\u6267\u884c\u4ee3\u7801\u5e76\u663e\u793a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&gt;&gt;&gt; print(&quot;Hello, World!&quot;)<\/p>\n<p>Hello, World!<\/p>\n<p>&gt;&gt;&gt; 2 + 2<\/p>\n<p>4<\/p>\n<p>&gt;&gt;&gt; x = 10<\/p>\n<p>&gt;&gt;&gt; x * 2<\/p>\n<p>20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5feb\u901f\u6d4b\u8bd5\u4ee3\u7801<\/strong>\uff1a\u53ef\u4ee5\u5feb\u901f\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5c0f\u6bb5\u4ee3\u7801\uff0c\u907f\u514d\u7f16\u5199\u548c\u8fd0\u884c\u6574\u4e2a\u811a\u672c\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5b66\u4e60\u548c\u63a2\u7d22<\/strong>\uff1a\u975e\u5e38\u9002\u5408\u5b66\u4e60Python\u8bed\u6cd5\u548c\u6807\u51c6\u5e93\u51fd\u6570\uff0c\u53ef\u4ee5\u7acb\u5373\u770b\u5230\u6267\u884c\u7ed3\u679c\u3002<\/li>\n<li><strong>\u5373\u65f6\u53cd\u9988<\/strong>\uff1a\u8f93\u5165\u4ee3\u7801\u540e\u7acb\u5373\u770b\u5230\u6267\u884c\u7ed3\u679c\uff0c\u6709\u52a9\u4e8e\u7406\u89e3\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u811a\u672c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff0cPython\u4ee3\u7801\u8fd8\u53ef\u4ee5\u4fdd\u5b58\u5728\u811a\u672c\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7\u89e3\u91ca\u5668\u8fd0\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u7f16\u5199\u8f83\u957f\u7684\u4ee3\u7801\u548c\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u548c\u8fd0\u884c\u811a\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u811a\u672c\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u6587\u4ef6\u6269\u5c55\u540d\u4e3a<code>.py<\/code>\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>hello.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u811a\u672c\u6587\u4ef6<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u754c\u9762\u4e2d\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8f93\u5165<code>python hello.py<\/code>\u6216<code>python3 hello.py<\/code>\uff0c\u89e3\u91ca\u5668\u4f1a\u8bfb\u53d6\u5e76\u6267\u884c\u811a\u672c\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python hello.py<\/p>\n<p>Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u811a\u672c\u6587\u4ef6\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801<\/strong>\uff1a\u53ef\u4ee5\u5c06\u4ee3\u7801\u7ec4\u7ec7\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\uff0c\u66f4\u5bb9\u6613\u7ba1\u7406\u548c\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u91cd\u7528\u548c\u5171\u4eab\u4ee3\u7801<\/strong>\uff1a\u811a\u672c\u6587\u4ef6\u53ef\u4ee5\u88ab\u5176\u4ed6\u9879\u76ee\u5f15\u7528\u548c\u91cd\u7528\uff0c\u65b9\u4fbf\u5171\u4eab\u548c\u534f\u4f5c\u3002<\/li>\n<li><strong>\u590d\u6742\u5e94\u7528\u7a0b\u5e8f<\/strong>\uff1a\u9002\u5408\u7f16\u5199\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u9879\u76ee\uff0c\u652f\u6301\u6a21\u5757\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/h3>\n<\/p>\n<p><p>\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u662f\u4e00\u79cd\u96c6\u6210\u4e86\u4ee3\u7801\u7f16\u8f91\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u7b49\u529f\u80fd\u7684\u8f6f\u4ef6\u5de5\u5177\uff0c\u4e13\u95e8\u7528\u4e8e\u8f6f\u4ef6\u5f00\u53d1\u3002\u4f7f\u7528IDE\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u5e38\u7528\u7684Python IDE\u5305\u62ecPyCharm\u3001Visual Studio Code\u3001Eclipse\uff08\u914d\u5408PyDev\u63d2\u4ef6\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528IDE\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u914d\u7f6eIDE<\/strong>\uff1a\u4e0b\u8f7d\u5e76\u5b89\u88c5IDE\uff0c\u6839\u636e\u9700\u8981\u914d\u7f6ePython\u89e3\u91ca\u5668\u548c\u76f8\u5173\u63d2\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efa\u9879\u76ee\u548c\u811a\u672c\u6587\u4ef6<\/strong>\uff1a\u5728IDE\u4e2d\u521b\u5efa\u4e00\u4e2aPython\u9879\u76ee\uff0c\u5e76\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u6216\u591a\u4e2a\u811a\u672c\u6587\u4ef6\u3002<\/li>\n<li><strong>\u7f16\u5199\u4ee3\u7801<\/strong>\uff1a\u5728\u811a\u672c\u6587\u4ef6\u4e2d\u7f16\u5199Python\u4ee3\u7801\uff0cIDE\u63d0\u4f9b\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u3001\u4ee3\u7801\u63d0\u793a\u7b49\u529f\u80fd\uff0c\u5e2e\u52a9\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u3002<\/li>\n<li><strong>\u8fd0\u884c\u548c\u8c03\u8bd5\u4ee3\u7801<\/strong>\uff1a\u901a\u8fc7IDE\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fd0\u884c\u4ee3\u7801\u3001\u8bbe\u7f6e\u65ad\u70b9\u3001\u68c0\u67e5\u53d8\u91cf\u503c\u3001\u5206\u6790\u9519\u8bef\u7b49\u3002<\/li>\n<\/ol>\n<p><h4>IDE\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u63d0\u9ad8\u5f00\u53d1\u6548\u7387<\/strong>\uff1a\u96c6\u6210\u4e86\u4ee3\u7801\u7f16\u8f91\u3001\u8c03\u8bd5\u3001\u6d4b\u8bd5\u7b49\u529f\u80fd\uff0c\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7ba1\u7406\u548c\u5bfc\u822a<\/strong>\uff1a\u63d0\u4f9b<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/23316.html\" target=\"_blank\">\u9879\u76ee\u7ba1\u7406<\/a>\u3001\u4ee3\u7801\u5bfc\u822a\u3001\u7248\u672c\u63a7\u5236\u7b49\u529f\u80fd\uff0c\u65b9\u4fbf\u7ba1\u7406\u548c\u7ef4\u62a4\u4ee3\u7801\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/strong>\uff1a\u63d0\u4f9b\u5f3a\u5927\u7684\u8c03\u8bd5\u548c\u6d4b\u8bd5\u5de5\u5177\uff0c\u5e2e\u52a9\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4ee3\u7801\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u9664\u4e86IDE\uff0c\u4ee3\u7801\u7f16\u8f91\u5668\u4e5f\u662f\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801\u7684\u5e38\u7528\u5de5\u5177\u3002\u4ee3\u7801\u7f16\u8f91\u5668\u901a\u5e38\u6bd4IDE\u66f4\u8f7b\u91cf\u7ea7\uff0c\u9002\u5408\u7f16\u5199\u7b80\u5355\u7684\u811a\u672c\u548c\u5c0f\u578b\u9879\u76ee\u3002\u5e38\u7528\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u5305\u62ecSublime Text\u3001Atom\u3001Notepad++\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u7f16\u8f91\u5668\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u914d\u7f6e\u4ee3\u7801\u7f16\u8f91\u5668<\/strong>\uff1a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u6839\u636e\u9700\u8981\u914d\u7f6ePython\u89e3\u91ca\u5668\u548c\u76f8\u5173\u63d2\u4ef6\u3002<\/li>\n<li><strong>\u521b\u5efa\u811a\u672c\u6587\u4ef6<\/strong>\uff1a\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u6587\u4ef6\u6269\u5c55\u540d\u4e3a<code>.py<\/code>\u3002<\/li>\n<li><strong>\u7f16\u5199\u4ee3\u7801<\/strong>\uff1a\u5728\u811a\u672c\u6587\u4ef6\u4e2d\u7f16\u5199Python\u4ee3\u7801\uff0c\u4ee3\u7801\u7f16\u8f91\u5668\u63d0\u4f9b\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u7b49\u529f\u80fd\uff0c\u5e2e\u52a9\u7f16\u5199\u4ee3\u7801\u3002<\/li>\n<li><strong>\u8fd0\u884c\u4ee3\u7801<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u754c\u9762\u4e2d\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8f93\u5165<code>python script.py<\/code>\u6216<code>python3 script.py<\/code>\uff0c\u89e3\u91ca\u5668\u4f1a\u8bfb\u53d6\u5e76\u6267\u884c\u811a\u672c\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><h4>\u4ee3\u7801\u7f16\u8f91\u5668\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u8f7b\u91cf\u7ea7<\/strong>\uff1a\u6bd4IDE\u66f4\u8f7b\u91cf\u7ea7\uff0c\u542f\u52a8\u901f\u5ea6\u5feb\uff0c\u5360\u7528\u8d44\u6e90\u5c11\uff0c\u9002\u5408\u7f16\u5199\u7b80\u5355\u7684\u811a\u672c\u548c\u5c0f\u578b\u9879\u76ee\u3002<\/li>\n<li><strong>\u53ef\u6269\u5c55<\/strong>\uff1a\u652f\u6301\u63d2\u4ef6\u6269\u5c55\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b89\u88c5\u5404\u79cd\u63d2\u4ef6\uff0c\u589e\u52a0\u529f\u80fd\u548c\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0<\/strong>\uff1a\u5927\u591a\u6570\u4ee3\u7801\u7f16\u8f91\u5668\u90fd\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u9879\u76ee\u65f6\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08Virtual Environment\uff09\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u548c\u5e93\uff0c\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u7248\u672c\u51b2\u7a81\u3002\u865a\u62df\u73af\u5883\u4e3a\u6bcf\u4e2a\u9879\u76ee\u63d0\u4f9b\u72ec\u7acb\u7684Python\u89e3\u91ca\u5668\u548c\u5305\u7ba1\u7406\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u548c\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u865a\u62df\u73af\u5883\u5de5\u5177<\/strong>\uff1aPython\u81ea\u5e26<code>venv<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u5982<code>virtualenv<\/code>\u3002\u5b89\u88c5<code>virtualenv<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong>\uff1a\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>venv<\/code>\u521b\u5efa\u540d\u4e3a<code>env<\/code>\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python -m venv env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong>\uff1a\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u7ec8\u7aef\u4f1a\u5207\u6362\u5230\u865a\u62df\u73af\u5883\u4e2d\u3002\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u5e93\u548c\u5305\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40\u73af\u5883\u3002\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<ul>\n<li>Windows\uff1a\n<pre><code class=\"language-bash\">$ .\\env\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>macOS\u548cLinux\uff1a\n<pre><code class=\"language-bash\">$ source env\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u4f9d\u8d56\u5e93<\/strong>\uff1a\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u4f9d\u8d56\u5e93\u3002\u4f8b\u5982\uff0c\u5b89\u88c5<code>requests<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">(env) $ pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u4ee3\u7801<\/strong>\uff1a\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884cPython\u4ee3\u7801\uff0c\u786e\u4fdd\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\u4e2d\u7684\u89e3\u91ca\u5668\u548c\u4f9d\u8d56\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong>\uff1a\u5b8c\u6210\u5f00\u53d1\u540e\uff0c\u53ef\u4ee5\u9000\u51fa\u865a\u62df\u73af\u5883\uff0c\u8fd4\u56de\u5168\u5c40\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">(env) $ deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u865a\u62df\u73af\u5883\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u9694\u79bb<\/strong>\uff1a\u6bcf\u4e2a\u9879\u76ee\u6709\u72ec\u7acb\u7684\u4f9d\u8d56\u73af\u5883\uff0c\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u7248\u672c\u51b2\u7a81\u3002<\/li>\n<li><strong>\u7248\u672c\u7ba1\u7406<\/strong>\uff1a\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u9879\u76ee\u914d\u7f6e\u4e0d\u540c\u7248\u672c\u7684Python\u89e3\u91ca\u5668\u548c\u5e93\uff0c\u65b9\u4fbf\u6d4b\u8bd5\u548c\u517c\u5bb9\u6027\u68c0\u67e5\u3002<\/li>\n<li><strong>\u6613\u4e8e\u90e8\u7f72<\/strong>\uff1a\u9879\u76ee\u7684\u4f9d\u8d56\u73af\u5883\u53ef\u4ee5\u8f7b\u677e\u590d\u5236\u548c\u90e8\u7f72\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u8fd0\u884c\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4ee3\u7801\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u53d1\u73b0\u548c\u89e3\u51b3\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528print\u8bed\u53e5\u8c03\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u8f93\u51fa\u53d8\u91cf\u503c\u548c\u6267\u884c\u72b6\u6001\uff0c\u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\u8c03\u8bd5\u65b9\u6cd5\u3002\u901a\u8fc7\u67e5\u770b\u8f93\u51fa\u4fe1\u606f\uff0c\u53ef\u4ee5\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u548c\u53d8\u91cf\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    print(f&quot;a: {a}, b: {b}&quot;)<\/p>\n<p>    result = a + b<\/p>\n<p>    print(f&quot;result: {result}&quot;)<\/p>\n<p>    return result<\/p>\n<p>add(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5185\u7f6e\u8c03\u8bd5\u5668pdb<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u8c03\u8bd5\u5668<code>pdb<\/code>\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u4ee3\u7801\u3001\u68c0\u67e5\u53d8\u91cf\u503c\u7b49\u3002\u4f7f\u7528<code>pdb<\/code>\u8c03\u8bd5\u4ee3\u7801\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65ad\u70b9<\/strong>\uff1a\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6267\u884c\u5230\u6b64\u5904\u65f6\u6682\u505c\uff0c\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    import pdb; pdb.set_trace()<\/p>\n<p>    result = a + b<\/p>\n<p>    return result<\/p>\n<p>add(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u52a8\u8c03\u8bd5\u5668<\/strong>\uff1a\u8fd0\u884c\u4ee3\u7801\u540e\uff0c\u7a0b\u5e8f\u4f1a\u5728\u65ad\u70b9\u5904\u6682\u505c\uff0c\u5e76\u8fdb\u5165<code>pdb<\/code>\u8c03\u8bd5\u6a21\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u8c03\u8bd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>n<\/code>\uff08next\uff09\uff1a\u5355\u6b65\u6267\u884c\u4ee3\u7801\u3002<\/li>\n<li><code>c<\/code>\uff08continue\uff09\uff1a\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9\u3002<\/li>\n<li><code>l<\/code>\uff08list\uff09\uff1a\u67e5\u770b\u5f53\u524d\u4ee3\u7801\u7247\u6bb5\u3002<\/li>\n<li><code>p<\/code>\uff08print\uff09\uff1a\u6253\u5370\u53d8\u91cf\u503c\u3002<\/li>\n<li><code>q<\/code>\uff08quit\uff09\uff1a\u9000\u51fa\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570IDE\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u652f\u6301\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u3001\u68c0\u67e5\u53d8\u91cf\u3001\u8c03\u7528\u6808\u5206\u6790\u7b49\u3002\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd\u53ef\u4ee5\u66f4\u52a0\u76f4\u89c2\u548c\u9ad8\u6548\u5730\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u65ad\u70b9<\/strong>\uff1a\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u70b9\u51fb\u884c\u53f7\uff0c\u8bbe\u7f6e\u65ad\u70b9\u3002<\/li>\n<li><strong>\u542f\u52a8\u8c03\u8bd5<\/strong>\uff1a\u70b9\u51fb\u8c03\u8bd5\u6309\u94ae\u6216\u4f7f\u7528\u5feb\u6377\u952e\u542f\u52a8\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u64cd\u4f5c<\/strong>\uff1a\u4f7f\u7528IDE\u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5355\u6b65\u6267\u884c\u3001\u68c0\u67e5\u53d8\u91cf\u3001\u5206\u6790\u8c03\u7528\u6808\u7b49\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4ee3\u7801\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6d4b\u8bd5\u662f\u4fdd\u8bc1\u4ee3\u7801\u8d28\u91cf\u548c\u53ef\u9760\u6027\u7684\u5173\u952e\u6b65\u9aa4\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6d4b\u8bd5\u6846\u67b6\u548c\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528unittest\u6846\u67b6<\/h4>\n<\/p>\n<p><p><code>unittest<\/code>\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u7684\u57fa\u672c\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\uff0c\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u7c7b\uff0c\u7ee7\u627f<code>unittest.TestCase<\/code>\uff0c\u5e76\u5b9e\u73b0\u6d4b\u8bd5\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from mymodule import add<\/p>\n<p>class TestAdd(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(2, 3), 5)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216IDE\u4e2d\u8fd0\u884c\u6d4b\u8bd5\u6587\u4ef6\uff0c<code>unittest<\/code>\u4f1a\u81ea\u52a8\u53d1\u73b0\u5e76\u6267\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ python test_mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528pytest\u6846\u67b6<\/h4>\n<\/p>\n<p><p><code>pytest<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u6613\u4e8e\u4f7f\u7528\u7684\u7b2c\u4e09\u65b9\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u591a\u79cd\u6d4b\u8bd5\u7c7b\u578b\u548c\u63d2\u4ef6\u6269\u5c55\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pytest<\/strong>\uff1a\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5<code>pytest<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pip install pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\uff0c\u7f16\u5199\u6d4b\u8bd5\u51fd\u6570\uff0c\u51fd\u6570\u540d\u4ee5<code>test_<\/code>\u5f00\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import add<\/p>\n<p>def test_add():<\/p>\n<p>    assert add(2, 3) == 5<\/p>\n<p>    assert add(-1, 1) == 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b<\/strong>\uff1a\u5728\u7ec8\u7aef\u6216IDE\u4e2d\u8fd0\u884c<code>pytest<\/code>\uff0c<code>pytest<\/code>\u4f1a\u81ea\u52a8\u53d1\u73b0\u5e76\u6267\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528mock\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u6d4b\u8bd5\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u6a21\u62df\u67d0\u4e9b\u5bf9\u8c61\u6216\u51fd\u6570\u7684\u884c\u4e3a\u3002<code>unittest.mock<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u4f7f\u7528mock\u5bf9\u8c61\u7684\u5de5\u5177\uff0c\u5e2e\u52a9\u7f16\u5199\u66f4\u7075\u6d3b\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528mock\u5bf9\u8c61<\/strong>\uff1a\u521b\u5efamock\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u5176\u8fd4\u56de\u503c\u6216\u884c\u4e3a\uff0c\u5e76\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unittest.mock import Mock<\/p>\n<p>def test_mock():<\/p>\n<p>    mock_obj = Mock()<\/p>\n<p>    mock_obj.method.return_value = 42<\/p>\n<p>    assert mock_obj.method() == 42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u62df\u51fd\u6570\u6216\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>patch<\/code>\u88c5\u9970\u5668\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u6a21\u62df\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from unittest.mock import patch<\/p>\n<p>@patch(&#39;mymodule.some_function&#39;)<\/p>\n<p>def test_patch(mock_func):<\/p>\n<p>    mock_func.return_value = &quot;mocked&quot;<\/p>\n<p>    assert mymodule.some_function() == &quot;mocked&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u4ee3\u7801\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u4ee3\u7801\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u4f18\u5316\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u4f18\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u4ee3\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5217\u8868\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528for\u5faa\u73af<\/p>\n<p>squares = []<\/p>\n<p>for x in range(10):<\/p>\n<p>    squares.append(x2)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>squares = [x2 for x in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\u548c\u6807\u51c6\u5e93\uff0c\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u548c\u5e93\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>sum<\/code>\u51fd\u6570\u66ff\u4ee3\u624b\u52a8\u7d2f\u52a0\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6c42\u548c\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u7d2f\u52a0<\/p>\n<p>total = 0<\/p>\n<p>for x in range(10):<\/p>\n<p>    total += x<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>total = sum(range(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u548cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u548c\u5e76\u884c\u6267\u884c\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86<code>threading<\/code>\u548c<code>multiprocessing<\/code>\u6a21\u5757\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u73b0\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;Task executed&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u548c\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=task)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;Task executed&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u548c\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>process = multiprocessing.Process(target=task)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u7f13\u5b58\u548c\u8bb0\u5fc6\u5316<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u91cd\u590d\u8ba1\u7b97\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u7f13\u5b58\u548c\u8bb0\u5fc6\u5316\uff08Memoization\uff09\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\uff0c\u5e2e\u52a9\u5b9e\u73b0\u8bb0\u5fc6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=None)<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    if n &lt; 2:<\/p>\n<p>        return n<\/p>\n<p>    return fibonacci(n-1) + fibonacci(n-2)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>print(fibonacci(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4ee3\u7801\u8d28\u91cf<\/h3>\n<\/p>\n<p><p>\u4fdd\u6301\u4ee3\u7801\u8d28\u91cf\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u5ffd\u89c6\u7684\u91cd\u8981\u73af\u8282\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u4fdd\u6301\u548c\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u53ef\u4ee5\u81ea\u52a8\u683c\u5f0f\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u7b26\u5408\u4e00\u5b9a\u7684\u7f16\u7801\u89c4\u8303\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002\u5e38\u7528\u7684Python\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u5305\u62ec<code>black<\/code>\u3001<code>autopep8<\/code>\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5black<\/strong>\uff1a\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5<code>black<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pip install black<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u4ee3\u7801<\/strong>\uff1a\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c<code>black<\/code>\uff0c\u683c\u5f0f\u5316\u6307\u5b9a\u7684\u4ee3\u7801\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ black mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u9759\u6001\u5206\u6790\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u9759\u6001\u5206\u6790\u5de5\u5177\u53ef\u4ee5\u5728\u4e0d\u8fd0\u884c\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\u3001\u4ee3\u7801\u98ce\u683c\u95ee\u9898\u548c\u6f5c\u5728\u7684\u6f0f\u6d1e\u3002\u5e38\u7528\u7684Python\u4ee3\u7801\u9759\u6001\u5206\u6790\u5de5\u5177\u5305\u62ec<code>pylint<\/code>\u3001<code>flake8<\/code>\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pylint<\/strong>\uff1a\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5<code>pylint<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pip install pylint<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u6790\u4ee3\u7801<\/strong>\uff1a\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c<code>pylint<\/code>\uff0c\u5206\u6790\u6307\u5b9a\u7684\u4ee3\u7801\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">$ pylint mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>\u4f7f\u7528\u4ee3\u7801\u8986\u76d6\u7387\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u8986\u76d6\u7387\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u4ee3\u7801\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u6267\u884c\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u901a\u8fc7Python\u89e3\u91ca\u5668\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u8f93\u5165\u4ee3\u7801\uff0c\u6216\u8005\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VS Code\u7b49\u3002\u60a8\u8fd8\u53ef\u4ee5\u5c06\u4ee3\u7801\u4fdd\u5b58\u5230\u4e00\u4e2a\u4ee5\u201c.py\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\u201cpython \u6587\u4ef6\u540d.py\u201d\u547d\u4ee4\u8fd0\u884c\u5b83\u3002\u6b64\u5916\uff0cJupyter Notebook\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5de5\u5177\uff0c\u9002\u5408\u4ea4\u4e92\u5f0f\u7f16\u7a0b\u548c\u6570\u636e\u79d1\u5b66\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u8bd5\u4ee3\u7801\u7684\u6709\u6548\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0cPython\u81ea\u5e26\u7684\u8c03\u8bd5\u5de5\u5177pdb\u5141\u8bb8\u9010\u884c\u6267\u884c\u4ee3\u7801\u5e76\u68c0\u67e5\u53d8\u91cf\u72b6\u6001\u3002\u6b64\u5916\uff0c\u8bb8\u591aIDE\u90fd\u63d0\u4f9b\u5185\u7f6e\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u5982\u65ad\u70b9\u8bbe\u7f6e\u548c\u53d8\u91cf\u76d1\u63a7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u8bc6\u522b\u548c\u4fee\u590d\u9519\u8bef\u3002\u4f7f\u7528print\u8bed\u53e5\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8c03\u8bd5\u6280\u672f\uff0c\u80fd\u591f\u8ba9\u60a8\u5b9e\u65f6\u67e5\u770b\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u4ee3\u7801\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fddPython\u4ee3\u7801\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u6b63\u5e38\u8fd0\u884c\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u6548\u7684\u7b56\u7565\u3002\u901a\u8fc7\u5de5\u5177\u5982venv\u6216conda\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u72ec\u7acb\u7684\u73af\u5883\uff0c\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5e93\uff0c\u4ece\u800c\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528requirements.txt\u6587\u4ef6\u5217\u51fa\u6240\u6709\u4f9d\u8d56\u9879\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u91cd\u73b0\u60a8\u7684\u5f00\u53d1\u73af\u5883\u3002\u6b64\u5916\uff0c\u4f7f\u7528Docker\u5bb9\u5668\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\uff0c\u786e\u4fdd\u5728\u4efb\u4f55\u73af\u5883\u4e2d\u90fd\u6709\u4e00\u81f4\u7684\u8fd0\u884c\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4ee3\u7801\u7684\u8fd0\u884c\u65b9\u5f0f\u5305\u62ec\u89e3\u91ca\u6267\u884c\u3001\u7f16\u8bd1\u6267\u884c\u3001\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3001\u811a\u672c\u6587\u4ef6\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u7b49\u591a\u79cd\u65b9\u5f0f\u3002\u5728 [&hellip;]","protected":false},"author":3,"featured_media":1185899,"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\/1185895"}],"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=1185895"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185895\/revisions"}],"predecessor-version":[{"id":1185900,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185895\/revisions\/1185900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185899"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}