{"id":1167045,"date":"2025-01-15T15:39:11","date_gmt":"2025-01-15T07:39:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167045.html"},"modified":"2025-01-15T15:39:14","modified_gmt":"2025-01-15T07:39:14","slug":"python%e5%86%99%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167045.html","title":{"rendered":"python\u5199\u7684\u7a0b\u5e8f\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\/25211024\/e0f0355e-c4c7-4e10-8143-8b7240ece25f.webp\" alt=\"python\u5199\u7684\u7a0b\u5e8f\u5982\u4f55\u8fd0\u884c\" \/><\/p>\n<p><p> \u8981\u8fd0\u884c\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528IDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\/\u7ec8\u7aef\u3001\u4f7f\u7528Jupyter Notebook<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Python\u89e3\u91ca\u5668<\/strong>\u662f\u6700\u57fa\u672c\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7Python\u89e3\u91ca\u5668\u8fd0\u884c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u6d4b\u8bd5\u548c\u8c03\u8bd5\u4ee3\u7801\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u5f00\u53d1\u8005\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u89e3\u91ca\u5668\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<p><p>Python\u89e3\u91ca\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u7684\u5de5\u5177\u3002\u8981\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u4f60\u9996\u5148\u9700\u8981\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u89e3\u91ca\u5668\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4ecePython\u5b98\u7f51\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E5%B9%B6%E5%AE%89%E8%A3%85%E9%80%82%E5%90%88%E4%BD%A0%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%89%88%E6%9C%AC%E3%80%82\">https:\/\/www.python.org\/\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u7248\u672c\u3002<\/a><\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5Python\u65f6\uff0c\u5efa\u8bae\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\/\u7ec8\u7aef\u4e2d\u76f4\u63a5\u4f7f\u7528<code>python<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Notepad\u3001Sublime Text\u3001VS Code\u7b49\uff09\u7f16\u5199Python\u4ee3\u7801\uff0c\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u7f16\u5199\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<p><h4>3. \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\uff08Windows\uff09\u6216\u7ec8\u7aef\uff08Mac\/Linux\uff09\uff0c\u5bfc\u822a\u5230\u4fdd\u5b58<code>hello.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5b89\u88c5\u7684\u662fPython3.x\u7248\u672c\uff0c\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\u9700\u8981\u4f7f\u7528<code>python3<\/code>\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u540e\uff0c\u4f60\u5c06\u770b\u5230\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528IDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09<\/h3>\n<\/p>\n<p><p>IDE\u662f\u4e00\u79cd\u96c6\u6210\u4e86\u4ee3\u7801\u7f16\u8f91\u3001\u8c03\u8bd5\u3001\u8fd0\u884c\u7b49\u529f\u80fd\u7684\u8f6f\u4ef6\u5de5\u5177\u3002\u5e38\u7528\u7684Python IDE\u6709PyCharm\u3001VS Code\u3001Jupyter Notebook\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528PyCharm<\/h4>\n<\/p>\n<p><p>PyCharm\u662f\u4e00\u6b3e\u5f3a\u5927\u7684Python IDE\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u63d2\u4ef6\u652f\u6301\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5PyCharm\u3002<\/li>\n<li>\u6253\u5f00PyCharm\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002<\/li>\n<li>\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff08\u53f3\u952e\u70b9\u51fb\u9879\u76ee\u76ee\u5f55\uff0c\u9009\u62e9\u201cNew\u201d -&gt; \u201cPython File\u201d\uff09\u3002<\/li>\n<li>\u7f16\u5199\u4ee3\u7801\u5e76\u4fdd\u5b58\u3002<\/li>\n<li>\u53f3\u952e\u70b9\u51fb\u4ee3\u7801\u7f16\u8f91\u533a\u57df\uff0c\u9009\u62e9\u201cRun &#39;filename&#39;\u201d\u5373\u53ef\u8fd0\u884c\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><h4>2. \u4f7f\u7528VS Code<\/h4>\n<\/p>\n<p><p>VS Code\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e76\u4e14\u6709\u4e30\u5bcc\u7684\u6269\u5c55\u63d2\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5VS Code\u3002<\/li>\n<li>\u5b89\u88c5Python\u6269\u5c55\u63d2\u4ef6\u3002<\/li>\n<li>\u6253\u5f00VS Code\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002<\/li>\n<li>\u7f16\u5199\u4ee3\u7801\u5e76\u4fdd\u5b58\u3002<\/li>\n<li>\u6253\u5f00\u7ec8\u7aef\uff08\u5feb\u6377\u952eCtrl+<code>\uff09\uff0c\u8f93\u5165<\/code>python filename.py`\u8fd0\u884c\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\/\u7ec8\u7aef<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u8fd0\u884c<code>.py<\/code>\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\/\u7ec8\u7aef\u4e2d\u8f93\u5165\u5355\u884c\u6216\u591a\u884cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\/\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff0c\u8fdb\u5165\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u6a21\u5f0f\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165Python\u4ee3\u7801\u5e76\u7acb\u5373\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/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><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8fd0\u884c\u591a\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u591a\u884cPython\u4ee3\u7801\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\/\u7ec8\u7aef\u4e2d\u8fd0\u884c\u8be5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Jupyter Notebook<\/h3>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u79cd\u57fa\u4e8eWeb\u7684\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u7279\u522b\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Jupyter Notebook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u542f\u52a8Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\/\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Jupyter Notebook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">jupyter notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u542f\u52a8\u540e\uff0c\u6d4f\u89c8\u5668\u5c06\u81ea\u52a8\u6253\u5f00Jupyter Notebook\u4e3b\u9875\u3002\u4f60\u53ef\u4ee5\u5728\u8be5\u9875\u9762\u521b\u5efa\u5e76\u7f16\u8f91<code>.ipynb<\/code>\u6587\u4ef6\uff0c\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>3. \u7f16\u5199\u5e76\u8fd0\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u65b0\u7684Notebook\uff0c\u5728\u5355\u5143\u683c\u4e2d\u7f16\u5199Python\u4ee3\u7801\uff0c\u7136\u540e\u70b9\u51fb\u201cRun\u201d\u6309\u94ae\u8fd0\u884c\u4ee3\u7801\u3002Jupyter Notebook\u652f\u6301\u5206\u6bb5\u8fd0\u884c\u4ee3\u7801\uff0c\u975e\u5e38\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u9879\u76ee\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56\u5305\u3002\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\uff0c\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Mac\/Linux\u7cfb\u7edf\u4e0a\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u7ec8\u7aef\u63d0\u793a\u7b26\u4f1a\u663e\u793a\u865a\u62df\u73af\u5883\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h4>3. \u5b89\u88c5\u4f9d\u8d56\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u9879\u76ee\u4f9d\u8d56\u5305\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u786e\u4fdd\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8c03\u8bd5Python\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5982\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\u3001IDE\u5185\u7f6e\u8c03\u8bd5\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528pdb\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>pdb<\/code>\u662fPython\u7684\u5185\u7f6e\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65ad\u70b9\uff0c\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u503c\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def add(a, b):<\/p>\n<p>    pdb.set_trace()  # \u63d2\u5165\u65ad\u70b9<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee3\u7801\u540e\uff0c\u7a0b\u5e8f\u4f1a\u5728<code>pdb.set_trace()<\/code>\u5904\u6682\u505c\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u8c03\u8bd5\u547d\u4ee4\uff0c\u5982<code>n<\/code>\uff08\u4e0b\u4e00\u884c\uff09\u3001<code>c<\/code>\uff08\u7ee7\u7eed\uff09\u3001<code>p \u53d8\u91cf\u540d<\/code>\uff08\u6253\u5370\u53d8\u91cf\u503c\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528IDE\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570IDE\uff08\u5982PyCharm\u3001VS Code\uff09\u90fd\u63d0\u4f9b\u4e86\u56fe\u5f62\u5316\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u3001\u9010\u884c\u6267\u884c\u3001\u67e5\u770b\u53d8\u91cf\u503c\u7b49\u3002\u4f7f\u7528IDE\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u66f4\u52a0\u76f4\u89c2\u5730\u8fdb\u884c\u8c03\u8bd5\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001Python\u73af\u5883\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\uff0c\u7ba1\u7406Python\u73af\u5883\u548c\u4f9d\u8d56\u5305\u975e\u5e38\u91cd\u8981\u3002\u5e38\u7528\u7684\u73af\u5883\u7ba1\u7406\u5de5\u5177\u6709<code>virtualenv<\/code>\u3001<code>pipenv<\/code>\u3001<code>conda<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528virtualenv<\/h4>\n<\/p>\n<p><p><code>virtualenv<\/code>\u662f\u4e00\u4e2a\u521b\u5efa\u72ec\u7acbPython\u73af\u5883\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\u3002\u4f7f\u7528\u65b9\u6cd5\u7c7b\u4f3c<code>venv<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install virtualenv<\/p>\n<p>virtualenv myenv<\/p>\n<p>source myenv\/bin\/activate  # \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pipenv<\/h4>\n<\/p>\n<p><p><code>pipenv<\/code>\u662f\u4e00\u4e2a\u96c6\u6210\u4e86<code>pip<\/code>\u548c<code>virtualenv<\/code>\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u521b\u5efa\u865a\u62df\u73af\u5883\u5e76\u7ba1\u7406\u4f9d\u8d56\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pipenv<\/p>\n<p>pipenv install requests  # \u5b89\u88c5\u4f9d\u8d56\u5305\u5e76\u81ea\u52a8\u521b\u5efa\u865a\u62df\u73af\u5883<\/p>\n<p>pipenv shell  # \u8fdb\u5165\u865a\u62df\u73af\u5883<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528conda<\/h4>\n<\/p>\n<p><p><code>conda<\/code>\u662fAnaconda\u53d1\u884c\u7248\u4e2d\u7684\u5305\u7ba1\u7406\u548c\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">conda create -n myenv python=3.8<\/p>\n<p>conda activate myenv  # \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/p>\n<p>conda install numpy  # \u5b89\u88c5\u4f9d\u8d56\u5305<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. ImportError: No module named &#39;xxx&#39;<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u8fd0\u884cPython\u7a0b\u5e8f\u65f6\u9047\u5230<code>ImportError<\/code>\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u4f9d\u8d56\u5305\u672a\u5b89\u88c5\u6216\u73af\u5883\u914d\u7f6e\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u4f9d\u8d56\u5305\u5df2\u5b89\u88c5\uff1a\u4f7f\u7528<code>pip install \u5305\u540d<\/code>\u5b89\u88c5\u7f3a\u5931\u7684\u4f9d\u8d56\u5305\u3002<\/li>\n<li>\u68c0\u67e5\u73af\u5883\u914d\u7f6e\uff1a\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684Python\u89e3\u91ca\u5668\u548c\u865a\u62df\u73af\u5883\u3002<\/li>\n<\/ul>\n<p><h4>2. SyntaxError: invalid syntax<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9047\u5230<code>SyntaxError<\/code>\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u4ee3\u7801\u8bed\u6cd5\u9519\u8bef\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u4ee3\u7801\u8bed\u6cd5\uff1a\u786e\u4fdd\u4ee3\u7801\u7b26\u5408Python\u8bed\u6cd5\u89c4\u5219\u3002<\/li>\n<li>\u68c0\u67e5Python\u7248\u672c\uff1a\u67d0\u4e9b\u8bed\u6cd5\u4ec5\u5728\u7279\u5b9aPython\u7248\u672c\u4e2d\u652f\u6301\uff0c\u5982<code>f-string<\/code>\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u652f\u6301\u3002<\/li>\n<\/ul>\n<p><h4>3. ModuleNotFoundError: No module named &#39;xxx&#39;<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u8fd0\u884c\u4ee3\u7801\u65f6\u9047\u5230<code>ModuleNotFoundError<\/code>\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u6a21\u5757\u672a\u627e\u5230\u6216\u8def\u5f84\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u6a21\u5757\u5df2\u5b89\u88c5\uff1a\u4f7f\u7528<code>pip install \u5305\u540d<\/code>\u5b89\u88c5\u7f3a\u5931\u7684\u6a21\u5757\u3002<\/li>\n<li>\u68c0\u67e5\u6a21\u5757\u8def\u5f84\uff1a\u786e\u4fdd\u6a21\u5757\u5728Python\u8def\u5f84\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path<\/code>\u67e5\u770bPython\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><h4>4. TypeError: &#39;xxx&#39; object is not callable<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9047\u5230<code>TypeError<\/code>\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u51fd\u6570\u8c03\u7528\u9519\u8bef\u6216\u53d8\u91cf\u7c7b\u578b\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u51fd\u6570\u8c03\u7528\uff1a\u786e\u4fdd\u51fd\u6570\u540d\u79f0\u548c\u53c2\u6570\u6b63\u786e\u3002<\/li>\n<li>\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\uff1a\u786e\u4fdd\u53d8\u91cf\u7c7b\u578b\u7b26\u5408\u9884\u671f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u67e5\u770b\u53d8\u91cf\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><h3>\u4e5d\u3001Python\u7a0b\u5e8f\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u7a0b\u5e8f\u65f6\uff0c\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u548c\u6548\u7387\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \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\u5e93\uff0c\u8fd9\u4e9b\u51fd\u6570\u548c\u5e93\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u4f7f\u7528\u5b83\u4eec\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u6c42\u548c\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u5faa\u73af\u6c42\u548c\u3002<\/li>\n<li>\u4f7f\u7528<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>defaultdict<\/code>\u548c<code>Counter<\/code>\u5904\u7406\u5b57\u5178\u548c\u8ba1\u6570\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h4>2. \u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/p>\n<p>result = complex_calculation()<\/p>\n<p>for i in range(10):<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u8282\u7701\u5185\u5b58\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        yield i<\/p>\n<p>for value in my_generator():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1b\u5bf9\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f7f\u7528\u591a\u8fdb\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def worker_function(x):<\/p>\n<p>    return x * x<\/p>\n<p>with Pool(4) as p:<\/p>\n<p>    results = p.map(worker_function, [1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001Python\u7a0b\u5e8f\u53d1\u5e03\u548c\u90e8\u7f72<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5b8c\u6210Python\u7a0b\u5e8f\u540e\uff0c\u53d1\u5e03\u548c\u90e8\u7f72\u662f\u6700\u540e\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u53d1\u5e03\u548c\u90e8\u7f72\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5305Python\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>setuptools<\/code>\u53ef\u4ee5\u5c06Python\u7a0b\u5e8f\u6253\u5305\u4e3a\u53ef\u5206\u53d1\u7684\u683c\u5f0f\uff0c\u5982<code>.whl<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;my_package&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        &#39;requests&#39;,<\/p>\n<p>    ],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6253\u5305\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py sdist bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53d1\u5e03\u5230PyPI<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u6253\u5305\u7684Python\u7a0b\u5e8f\u53d1\u5e03\u5230Python\u5305\u7ba1\u7406\u5e73\u53f0PyPI\uff0c\u4f7f\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\u3002\u9996\u5148\uff0c\u6ce8\u518c\u4e00\u4e2aPyPI\u8d26\u6237\uff0c\u7136\u540e\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u53d1\u5e03\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install twine<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u90e8\u7f72\u5230\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06Python\u7a0b\u5e8f\u90e8\u7f72\u5230\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u5982Web\u5e94\u7528\u3001\u540e\u53f0\u4efb\u52a1\u7b49\u3002\u5e38\u7528\u7684\u90e8\u7f72\u65b9\u6cd5\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>gunicorn<\/code>\u6216<code>uWSGI<\/code>\u90e8\u7f72Web\u5e94\u7528\u3002<\/li>\n<li>\u4f7f\u7528<code>supervisor<\/code>\u6216<code>systemd<\/code>\u7ba1\u7406\u540e\u53f0\u4efb\u52a1\u3002<\/li>\n<li>\u4f7f\u7528Docker\u5bb9\u5668\u5316\u90e8\u7f72\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-sh\"># \u4f7f\u7528gunicorn\u90e8\u7f72Flask\u5e94\u7528<\/p>\n<p>gunicorn app:app --bind 0.0.0.0:8000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001Python\u7a0b\u5e8f\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6d4b\u8bd5\u662f\u4fdd\u8bc1\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u73af\u8282\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6d4b\u8bd5\u6846\u67b6\uff0c\u5982<code>unittest<\/code>\u3001<code>pytest<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528unittest<\/h4>\n<\/p>\n<p><p><code>unittest<\/code>\u662fPython\u7684\u5185\u7f6e\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ef\u4ee5\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u4ee3\u7801\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestAddFunction(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(3, 5), 8)<\/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<p><p>\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m unittest test_file.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528pytest<\/h4>\n<\/p>\n<p><p><code>pytest<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u66f4\u7b80\u6d01\u7684\u6d4b\u8bd5\u4ee3\u7801\u548c\u66f4\u591a\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def test_add():<\/p>\n<p>    assert add(3, 5) == 8<\/p>\n<p>    assert add(-1, 1) == 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pytest test_file.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001Python\u7a0b\u5e8f\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u6587\u6863\u662f\u4fdd\u8bc1\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u7684\u91cd\u8981\u73af\u8282\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6587\u6863\u5de5\u5177\uff0c\u5982<code>docstring<\/code>\u3001<code>Sphinx<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528docstring<\/h4>\n<\/p>\n<p><p><code>docstring<\/code>\u662fPython\u7684\u5185\u7f6e\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4e3a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u7b49\u7f16\u5199\u6587\u6863\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Adds two numbers.<\/p>\n<p>    Args:<\/p>\n<p>        a (int): The first number.<\/p>\n<p>        b (int): The second number.<\/p>\n<p>    Returns:<\/p>\n<p>        int: The sum of the two numbers.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Sphinx<\/h4>\n<\/p>\n<p><p>Sphinx\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u751f\u6210HTML\u3001PDF\u7b49\u683c\u5f0f\u7684\u6587\u6863\u3002\u9996\u5148\u5b89\u88c5Sphinx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install sphinx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521d\u59cb\u5316Sphinx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sphinx-quickstart<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6309\u7167\u63d0\u793a\u914d\u7f6eSphinx\uff0c\u7136\u540e\u7f16\u5199<code>.rst<\/code>\u6587\u6863\u6587\u4ef6\uff0c\u6700\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">make html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001Python\u7a0b\u5e8f\u5b89\u5168<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u7a0b\u5e8f\u65f6\uff0c\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h4>1. \u8f93\u5165\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\uff0c\u907f\u514dSQL\u6ce8\u5165\u3001XSS\u7b49\u653b\u51fb\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def validate_username(username):<\/p>\n<p>    if re.match(&quot;^[a-zA-Z0-9_]+$&quot;, username):<\/p>\n<p>        return True<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u52a0\u5bc6<\/h4>\n<\/p>\n<p><p>\u5bf9\u654f\u611f\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u4fdd\u62a4\u7528\u6237\u9690\u79c1\u548c\u6570\u636e\u5b89\u5168\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cryptography.fernet import Fernet<\/p>\n<p>key = Fernet.generate_key()<\/p>\n<p>cipher = Fernet(key)<\/p>\n<p>encrypted_data = cipher.encrypt(b&quot;secret data&quot;)<\/p>\n<p>decrypted_data = cipher.decrypt(encrypted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b89\u5168\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u7a0b\u5e8f\u548c\u670d\u52a1\u5668\u7684\u5b89\u5168\u914d\u7f6e\uff0c\u907f\u514d\u66b4\u9732\u654f\u611f\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528HTTPS\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\u3002<\/li>\n<li>\u914d\u7f6e\u9632\u706b\u5899\u548c\u5b89\u5168\u7ec4\uff0c\u9650\u5236\u8bbf\u95ee\u6743\u9650\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0\u4f9d\u8d56\u5305\u548c\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4fee\u590d\u5df2\u77e5\u6f0f\u6d1e\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u56db\u3001Python\u7a0b\u5e8f\u7ef4\u62a4<\/h3>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u53d1\u5e03\u540e\uff0c\u7ef4\u62a4\u548c\u66f4\u65b0\u662f\u4fdd\u6301\u7a0b\u5e8f\u8fd0\u884c\u7a33\u5b9a\u7684\u91cd\u8981\u73af\u8282\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7ef4\u62a4\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u7248\u672c\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u7ba1\u7406\u4ee3\u7801\u7248\u672c\uff0c\u8bb0\u5f55\u4ee3\u7801\u53d8\u66f4\u5386\u53f2\uff0c\u65b9\u4fbf\u56de\u6eda\u548c\u534f\u4f5c\u5f00\u53d1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">git init<\/p>\n<p>git add .<\/p>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u52a8\u5316\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u914d\u7f6e<a href=\"https:\/\/docs.pingcode.com\/blog\/devops\/52691.html\" target=\"_blank\">CI\/CD<\/a>\uff08\u6301\u7eed\u96c6\u6210\/\u6301\u7eed\u90e8\u7f72\uff09\u5de5\u5177\uff08\u5982GitHub Actions\u3001Travis CI\uff09\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u90e8\u7f72\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\"># GitHub Actions\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>name: Python application<\/p>\n<p>on: [push]<\/p>\n<p>jobs:<\/p>\n<p>  build:<\/p>\n<p>    runs-on: ubuntu-latest<\/p>\n<p>    steps:<\/p>\n<p>    - uses: actions\/checkout@v2<\/p>\n<p>    - name: Set up Python<\/p>\n<p>      uses: actions\/setup-python@v2<\/p>\n<p>      with:<\/p>\n<p>        python-version: 3.8<\/p>\n<p>    - name: Install dependencies<\/p>\n<p>      run: |<\/p>\n<p>        python -m pip install --upgrade pip<\/p>\n<p>        pip install -r requirements.txt<\/p>\n<p>    - name: Run tests<\/p>\n<p>      run: |<\/p>\n<p>        pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u76d1\u63a7\u548c\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u914d\u7f6e\u76d1\u63a7\u548c\u65e5\u5fd7\u7cfb\u7edf\uff0c\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\uff0c\u4fdd\u5b58\u8fd0\u884c\u4fe1\u606f\u548c\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528\u76d1\u63a7\u5de5\u5177\uff08\u5982Prometheus\u3001Grafana\uff09\u76d1\u63a7\u670d\u52a1\u5668\u548c\u5e94\u7528\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>logging.info(&#39;This is an info message&#39;)<\/p>\n<p>logging.error(&#39;This is an error message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001Python\u793e\u533a\u548c\u8d44\u6e90<\/h3>\n<\/p>\n<p><p>Python\u793e\u533a\u6d3b\u8dc3\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8d44\u6e90\u548c\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u793e\u533a\u548c\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h4>1. Python<\/h4>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u672c\u5730\u73af\u5883\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5728\u672c\u5730\u73af\u5883\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Python\u89e3\u91ca\u5668\u3002\u53ef\u4ee5\u8bbf\u95eePython\u5b98\u7f51\uff08python.org\uff09\u4e0b\u8f7d\u9002\u5408\u60a8\u64cd\u4f5c\u7cfb\u7edf\u7684\u7248\u672c\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u8fdb\u5165\u5305\u542b\u7a0b\u5e8f\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u4f7f\u7528\u547d\u4ee4 <code>python \u6587\u4ef6\u540d.py<\/code> \u6765\u8fd0\u884c\u7a0b\u5e8f\u3002\u786e\u4fdd\u6587\u4ef6\u540d\u6b63\u786e\u4e14\u540e\u7f00\u4e3a.py\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u9047\u5230\u9519\u8bef\uff0c\u5982\u4f55\u8c03\u8bd5\u6211\u7684Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5185\u7f6e\u5de5\u5177\u548c\u5e93\uff0c\u4f8b\u5982<code>pdb<\/code>\uff08Python Debugger\uff09\uff0c\u5b83\u5141\u8bb8\u60a8\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u503c\u3002\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u8c03\u8bd5\u65b9\u6cd5\u3002IDE\u5982PyCharm\u6216VSCode\u4e5f\u63d0\u4f9b\u4e86\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\uff0c\u4fbf\u4e8e\u67e5\u627e\u95ee\u9898\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728\u4e91\u5e73\u53f0\u4e0a\u8fd0\u884cPython\u7a0b\u5e8f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8bb8\u591a\u4e91\u5e73\u53f0\u63d0\u4f9b\u4e86\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u73af\u5883\u3002\u4f8b\u5982\uff0cGoogle Colab\u548cJupyter Notebook\u662f\u70ed\u95e8\u9009\u62e9\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u3002\u60a8\u53ea\u9700\u5728\u8fd9\u4e9b\u5e73\u53f0\u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7b14\u8bb0\u672c\uff0c\u7c98\u8d34\u60a8\u7684Python\u4ee3\u7801\u5373\u53ef\u8fd0\u884c\u3002\u4e91\u670d\u52a1\u7684\u597d\u5904\u5728\u4e8e\u4e0d\u9700\u8981\u5b89\u88c5\u4efb\u4f55\u8f6f\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e\u4ed6\u4eba\u5206\u4eab\u60a8\u7684\u4ee3\u7801\u548c\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8fd0\u884c\u7528Python\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Python\u89e3\u91ca\u5668\u3001\u4f7f\u7528IDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1167050,"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\/1167045"}],"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=1167045"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167045\/revisions"}],"predecessor-version":[{"id":1167051,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167045\/revisions\/1167051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167050"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}