{"id":1154972,"date":"2025-01-13T17:57:38","date_gmt":"2025-01-13T09:57:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154972.html"},"modified":"2025-01-13T17:57:41","modified_gmt":"2025-01-13T09:57:41","slug":"%e5%ae%89%e8%a3%85python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e8%b7%af%e5%be%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154972.html","title":{"rendered":"\u5b89\u88c5python\u5982\u4f55\u8bbe\u7f6e\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184421\/adb8fb9f-12bf-4972-bc42-5e290ada6333.webp\" alt=\"\u5b89\u88c5python\u5982\u4f55\u8bbe\u7f6e\u8def\u5f84\" \/><\/p>\n<p><p> \u8981\u5b89\u88c5Python\u5e76\u8bbe\u7f6e\u8def\u5f84\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<strong>\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3001\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7d\u5e76\u5b89\u88c5Python<\/strong>\uff1a\u9996\u5148\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08python.org\uff09\u4e0b\u8f7d\u9002\u7528\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684Python\u5b89\u88c5\u5305\uff0c\u5b89\u88c5\u65f6\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u81ea\u52a8\u5c06Python\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf<\/strong>\uff1a\u5982\u679c\u5728\u5b89\u88c5\u65f6\u6ca1\u6709\u52fe\u9009\u201cAdd Python to PATH\u201d\uff0c\u4f60\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u3002\u53f3\u952e\u201c\u6b64\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\uff0c\u8fdb\u5165\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\uff0c\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\u627e\u5230\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\uff0c\u6dfb\u52a0Python\u5b89\u88c5\u8def\u5f84\u53ca\u5176Scripts\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/strong>\uff1a\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff08cmd\uff09\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>python --version<\/code>\u548c<code>pip --version<\/code>\uff0c\u5982\u679c\u80fd\u591f\u6b63\u786e\u663e\u793a\u7248\u672c\u53f7\uff0c\u8bf4\u660ePython\u5b89\u88c5\u5e76\u8def\u5f84\u8bbe\u7f6e\u6210\u529f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u4e0b\u8f7d\u5e76\u5b89\u88c5Python<\/h3>\n<\/p>\n<p><p>\u5728\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u65f6\uff0c\u4f60\u9700\u8981\u9009\u62e9\u9002\u5408\u81ea\u5df1\u64cd\u4f5c\u7cfb\u7edf\u7684\u7248\u672c\u3002\u65e0\u8bba\u4f60\u4f7f\u7528\u7684\u662fWindows\u3001macOS\u8fd8\u662fLinux\uff0c\u5b98\u65b9\u7f51\u7ad9\u90fd\u4f1a\u63d0\u4f9b\u76f8\u5e94\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1. Windows\u7cfb\u7edf<\/h4>\n<\/p>\n<ul>\n<li>\u8fdb\u5165Python\u5b98\u65b9\u7f51\u7ad9\uff08python.org\uff09\u3002<\/li>\n<li>\u5728\u201cDownloads\u201d\u9875\u9762\uff0c\u9009\u62e9Windows\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li>\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684\u5b89\u88c5\u5305\uff08\u901a\u5e38\u662f\u4e00\u4e2a.exe\u6587\u4ef6\uff09\u3002<\/li>\n<li>\u8fd0\u884c\u4e0b\u8f7d\u597d\u7684\u5b89\u88c5\u7a0b\u5e8f\uff0c\u5728\u5b89\u88c5\u9009\u9879\u754c\u9762\uff0c\u52a1\u5fc5\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u7136\u540e\u70b9\u51fb\u201cInstall Now\u201d\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p><h4>2. macOS\u7cfb\u7edf<\/h4>\n<\/p>\n<ul>\n<li>\u8fdb\u5165Python\u5b98\u65b9\u7f51\u7ad9\uff0c\u9009\u62e9macOS\u64cd\u4f5c\u7cfb\u7edf\u3002<\/li>\n<li>\u4e0b\u8f7d\u9002\u7528\u4e8emacOS\u7684\u5b89\u88c5\u5305\uff08\u901a\u5e38\u662f\u4e00\u4e2a.pkg\u6587\u4ef6\uff09\u3002<\/li>\n<li>\u8fd0\u884c\u4e0b\u8f7d\u597d\u7684\u5b89\u88c5\u7a0b\u5e8f\uff0c\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p><h4>3. Linux\u7cfb\u7edf<\/h4>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u591a\u6570Linux\u53d1\u884c\u7248\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5Python\u3002\u4ee5Ubuntu\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a\n<pre><code class=\"language-bash\">sudo apt update<\/p>\n<p>sudo apt install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u68c0\u67e5Python\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p><strong>\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf<\/strong>\u662f\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u6b63\u786e\u627e\u5230\u5e76\u6267\u884cPython\u548c\u76f8\u5173\u5de5\u5177\uff08\u5982pip\uff09\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5982\u679c\u5728\u5b89\u88c5\u65f6\u6ca1\u6709\u52fe\u9009\u201cAdd Python to PATH\u201d\uff0c\u4f60\u9700\u8981\u624b\u52a8\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1. Windows\u7cfb\u7edf<\/h4>\n<\/p>\n<ul>\n<li>\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u6211\u7684\u7535\u8111\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002<\/li>\n<li>\u5728\u5de6\u4fa7\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\uff0c\u627e\u5230\u5e76\u9009\u62e9\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002<\/li>\n<li>\u5728\u201c\u7f16\u8f91\u73af\u5883\u53d8\u91cf\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\uff0c\u7136\u540e\u6dfb\u52a0Python\u7684\u5b89\u88c5\u8def\u5f84\uff08\u4f8b\u5982\uff1a<code>C:\\Python39<\/code>\uff09\u548cScripts\u76ee\u5f55\uff08\u4f8b\u5982\uff1a<code>C:\\Python39\\Scripts<\/code>\uff09\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u6240\u6709\u66f4\u6539\uff0c\u7136\u540e\u91cd\u542f\u8ba1\u7b97\u673a\u4f7f\u8bbe\u7f6e\u751f\u6548\u3002<\/li>\n<\/ul>\n<p><h4>2. macOS\u548cLinux\u7cfb\u7edf<\/h4>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u7f16\u8f91shell\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>.bashrc<\/code>\u6216<code>.zshrc<\/code>\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a\n<pre><code class=\"language-bash\">export PATH=&quot;\/usr\/local\/bin\/python3:$PATH&quot;<\/p>\n<p>export PATH=&quot;\/usr\/local\/bin\/python3\/Scripts:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u66f4\u6539\u751f\u6548\uff1a\n<pre><code class=\"language-bash\">source ~\/.bashrc  # \u5982\u679c\u4f7f\u7528\u7684\u662fBash<\/p>\n<p>source ~\/.zshrc   # \u5982\u679c\u4f7f\u7528\u7684\u662fZsh<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u4f60\u9700\u8981\u786e\u8ba4Python\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u5e76\u4e14\u8def\u5f84\u8bbe\u7f6e\u6210\u529f\u3002\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff08cmd\uff09\u6216\u7ec8\u7aef\uff0c\u5206\u522b\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p>pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u80fd\u591f\u6b63\u786e\u663e\u793aPython\u548cpip\u7684\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001Python\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5Python\u5e76\u8bbe\u7f6e\u8def\u5f84\u540e\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h4>1. \u547d\u4ee4\u672a\u627e\u5230<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>pip<\/code>\u65f6\u63d0\u793a\u201c\u547d\u4ee4\u672a\u627e\u5230\u201d\uff0c\u53ef\u80fd\u662f\u8def\u5f84\u672a\u6b63\u786e\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u3002\u8bf7\u68c0\u67e5\u5e76\u786e\u4fdd\u8def\u5f84\u8bbe\u7f6e\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h4>2. \u7248\u672c\u51b2\u7a81<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u7cfb\u7edf\u4e0a\u53ef\u80fd\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\u7684Python\uff0c\u5bfc\u81f4\u7248\u672c\u51b2\u7a81\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5b8c\u5168\u8def\u5f84\u6765\u8fd0\u884c\u7279\u5b9a\u7248\u672c\u7684Python\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">C:\\Python39\\python.exe --version  # Windows<\/p>\n<p>\/usr\/bin\/python3.9 --version      # macOS\/Linux<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6743\u9650\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728Linux\u548cmacOS\u7cfb\u7edf\u4e2d\uff0c\u5b89\u88c5\u6216\u8fd0\u884cPython\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>sudo<\/code>\u547d\u4ee4\u63d0\u5347\u6743\u9650\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt install python3<\/p>\n<p>sudo pip install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Python\u7684\u73af\u5883\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406Python\u73af\u5883\uff0c\u63a8\u8350\u4f7f\u7528\u865a\u62df\u73af\u5883\u5de5\u5177\uff0c\u5982<code>venv<\/code>\u548c<code>virtualenv<\/code>\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u521b\u5efa\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u907f\u514d\u5305\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>venv<\/code><\/h4>\n<\/p>\n<p><p>Python 3.3\u53ca\u4ee5\u4e0a\u7248\u672c\u5185\u7f6e\u4e86<code>venv<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # Linux\u548cmacOS<\/p>\n<p>myenv\\Scripts\\activate     # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>virtualenv<\/code><\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u65e9\u7248\u672c\u7684Python\uff0c\u53ef\u4ee5\u4f7f\u7528<code>virtualenv<\/code>\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u9996\u5148\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<p><p>\u7136\u540e\u521b\u5efa\u5e76\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv myenv<\/p>\n<p>source myenv\/bin\/activate  # Linux\u548cmacOS<\/p>\n<p>myenv\\Scripts\\activate     # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001Python\u5305\u7ba1\u7406\u5de5\u5177pip<\/h3>\n<\/p>\n<p><p><code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528<code>pip<\/code>\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5Python\u5305\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install package_name<\/p>\n<p>pip install package_name==version<\/p>\n<p>pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5378\u8f7d\u5305<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5378\u8f7dPython\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5217\u51fa\u5df2\u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u8981\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001Python\u5f00\u53d1\u73af\u5883\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u4f60\u53ef\u4ee5\u914d\u7f6e\u4e00\u4e2a\u9002\u5408\u81ea\u5df1\u7684Python\u5f00\u53d1\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684\u5de5\u5177\u548c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1. \u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684IDE\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6d41\u884c\u7684Python IDE\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyCharm<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\u7684\u4e13\u4e1aIDE\uff0c\u9002\u5408\u4e2d\u5927\u578b\u9879\u76ee\u5f00\u53d1\u3002<\/li>\n<li><strong>VSCode<\/strong>\uff1a\u8f7b\u91cf\u7ea7\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u9002\u5408\u4e2a\u4eba\u548c\u56e2\u961f\u5f00\u53d1\u3002<\/li>\n<li><strong>Jupyter Notebook<\/strong>\uff1a\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u6301\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\uff0c\u63a8\u8350\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\uff0c\u5982<code>black<\/code>\u548c<code>autopep8<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install black<\/p>\n<p>black myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u9759\u6001\u4ee3\u7801\u68c0\u67e5\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u73b0\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5de5\u5177\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>pylint<\/strong>\uff1a\u5168\u9762\u7684Python\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u3002<\/li>\n<li><strong>flake8<\/strong>\uff1a\u8f7b\u91cf\u7ea7\u7684\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\uff0c\u9002\u5408\u5feb\u901f\u68c0\u67e5\u3002<\/li>\n<li><strong>mypy<\/strong>\uff1aPython\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\uff0c\u9002\u5408\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001Python\u7248\u672c\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Python\u7248\u672c\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. pyenv<\/h4>\n<\/p>\n<p><p><code>pyenv<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8emacOS\u548cLinux\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u5b89\u88c5pyenv<\/p>\n<p>curl https:\/\/pyenv.run | bash<\/p>\n<h2><strong>\u5b89\u88c5Python\u7248\u672c<\/strong><\/h2>\n<p>pyenv install 3.9.7<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40Python\u7248\u672c<\/strong><\/h2>\n<p>pyenv global 3.9.7<\/p>\n<h2><strong>\u8bbe\u7f6e\u672c\u5730Python\u7248\u672c<\/strong><\/h2>\n<p>pyenv local 3.9.7<\/p>\n<h2><strong>\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u7248\u672c<\/strong><\/h2>\n<p>pyenv versions<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Anaconda<\/h4>\n<\/p>\n<p><p><code>Anaconda<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u79d1\u5b66\u7684Python\u53d1\u884c\u7248\uff0c\u5185\u7f6e\u4e86\u591a\u4e2aPython\u7248\u672c\u548c\u5927\u91cf\u6570\u636e\u79d1\u5b66\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u4e0b\u8f7d\u5e76\u5b89\u88c5Anaconda<\/p>\n<h2><strong>\u8bbf\u95eeAnaconda\u5b98\u65b9\u7f51\u7ad9\uff08anaconda.com\uff09\uff0c\u4e0b\u8f7d\u9002\u7528\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\uff0c\u5e76\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5<\/strong><\/h2>\n<h2><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p>conda create -n myenv python=3.9<\/p>\n<h2><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p>conda activate myenv<\/p>\n<h2><strong>\u5b89\u88c5\u5305<\/strong><\/h2>\n<p>conda install numpy pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001Python\u5f00\u53d1\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1. \u9075\u5faaPEP 8\u7f16\u7801\u89c4\u8303<\/h4>\n<\/p>\n<p><p>PEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u63a8\u8350\u4f7f\u7528\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u548c\u9759\u6001\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u6765\u786e\u4fdd\u4ee3\u7801\u7b26\u5408PEP 8\u89c4\u8303\u3002<\/p>\n<\/p>\n<p><h4>2. \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u6709\u52a9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002\u63a8\u8350\u4f7f\u7528<code>unittest<\/code>\u3001<code>pytest<\/code>\u7b49\u6d4b\u8bd5\u6846\u67b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528unittest\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/p>\n<p>import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestMathFunctions(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/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><h4>3. \u4f7f\u7528\u7248\u672c\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\uff08\u5982Git\uff09\u7ba1\u7406\u4ee3\u7801\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u4ee3\u7801\u5907\u4efd\u3001\u7248\u672c\u7ba1\u7406\u548c\u56e2\u961f\u534f\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u521d\u59cb\u5316Git\u4ed3\u5e93<\/p>\n<p>git init<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u4ef6\u5230Git\u4ed3\u5e93<\/strong><\/h2>\n<p>git add .<\/p>\n<h2><strong>\u63d0\u4ea4\u6587\u4ef6\u5230Git\u4ed3\u5e93<\/strong><\/h2>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<h2><strong>\u63a8\u9001\u4ee3\u7801\u5230\u8fdc\u7a0b\u4ed3\u5e93<\/strong><\/h2>\n<p>git remote add origin https:\/\/github.com\/username\/repo.git<\/p>\n<p>git push -u origin master<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001Python\u5b66\u4e60\u8d44\u6e90<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5b66\u4e60\u548c\u638c\u63e1Python\uff0c\u63a8\u8350\u4e00\u4e9b\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b98\u65b9\u6587\u6863<\/h4>\n<\/p>\n<p><p>Python\u7684\u5b98\u65b9\u6587\u6863\uff08docs.python.org\uff09\u662f\u5b66\u4e60Python\u6700\u6743\u5a01\u7684\u8d44\u6e90\uff0c\u5305\u542b\u8be6\u7ec6\u7684\u8bed\u8a00\u53c2\u8003\u548c\u5e93\u53c2\u8003\u3002<\/p>\n<\/p>\n<p><h4>2. \u5728\u7ebf\u6559\u7a0b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u8d28\u7684\u5728\u7ebf\u6559\u7a0b\u548c\u8bfe\u7a0b\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Codecademy<\/strong>\uff1a\u4ea4\u4e92\u5f0f\u7684Python\u6559\u7a0b\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>Coursera<\/strong>\uff1a\u63d0\u4f9b\u591a\u95e8Python\u8bfe\u7a0b\uff0c\u9002\u5408\u7cfb\u7edf\u5b66\u4e60\u3002<\/li>\n<li><strong>Udemy<\/strong>\uff1a\u4e30\u5bcc\u7684Python\u8bfe\u7a0b\uff0c\u9002\u5408\u4e0d\u540c\u6c34\u5e73\u7684\u5b66\u4e60\u8005\u3002<\/li>\n<\/ul>\n<p><h4>3. \u4e66\u7c4d<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684Python\u4e66\u7c4d\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u300aPython\u7f16\u7a0b\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b<\/strong>\uff1a\u9002\u5408\u521d\u5b66\u8005\u7684\u5165\u95e8\u4e66\u7c4d\u3002<\/li>\n<li><strong>\u300a\u6d41\u7545\u7684Python\u300b<\/strong>\uff1a\u6df1\u5165\u8bb2\u89e3Python\u9ad8\u7ea7\u7279\u6027\uff0c\u9002\u5408\u6709\u4e00\u5b9a\u57fa\u7840\u7684\u5f00\u53d1\u8005\u3002<\/li>\n<li><strong>\u300aPython Cookbook\u300b<\/strong>\uff1a\u5b9e\u7528\u7684Python\u7f16\u7a0b\u6307\u5357\uff0c\u9002\u5408\u4e2d\u9ad8\u7ea7\u5f00\u53d1\u8005\u3002<\/li>\n<\/ul>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u4e86\u89e3\u4e86\u5982\u4f55\u5b89\u88c5Python\u5e76\u8bbe\u7f6e\u8def\u5f84\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5e38\u89c1\u95ee\u9898\u548c\u4f18\u5316\u5f00\u53d1\u73af\u5883\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u795d\u4f60\u5728Python\u5b66\u4e60\u548c\u5f00\u53d1\u4e2d\u53d6\u5f97\u6210\u529f\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Windows\u4e0a\u5b89\u88c5Python\u5e76\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\u5b89\u88c5Python\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a  <\/p>\n<ol>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\uff0c\u786e\u4fdd\u5728\u5b89\u88c5\u754c\u9762\u4e2d\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\u3002  <\/li>\n<li>\u5982\u679c\u672a\u52fe\u9009\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e\u8def\u5f84\u3002\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u8ba1\u7b97\u673a\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002  <\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002  <\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\uff0c\u627e\u5230\u5e76\u9009\u62e9\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002  <\/li>\n<li>\u6dfb\u52a0Python\u7684\u5b89\u88c5\u8def\u5f84\uff08\u5982C:\\Python39\uff09\u548cScripts\u76ee\u5f55\uff08\u5982C:\\Python39\\Scripts\uff09\uff0c\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u66f4\u6539\u3002<br \/>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528Python\u547d\u4ee4\u3002<\/li>\n<\/ol>\n<p><strong>\u5728Mac\u7cfb\u7edf\u4e0a\u5982\u4f55\u914d\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Mac\u4e0a\u914d\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\u76f8\u5bf9\u7b80\u5355\u3002\u901a\u5e38\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u8bbe\u7f6e\uff1a  <\/p>\n<ol>\n<li>\u5b89\u88c5Python\u540e\uff0c\u6253\u5f00\u7ec8\u7aef\u3002  <\/li>\n<li>\u8f93\u5165\u547d\u4ee4<code>nano ~\/.bash_profile<\/code>\u6216<code>nano ~\/.zshrc<\/code>\uff08\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684Shell\uff09\u3002  <\/li>\n<li>\u5728\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a\n<pre><code class=\"language-bash\">export PATH=&quot;\/usr\/local\/bin:$PATH&quot;\nexport PATH=&quot;\/usr\/local\/bin\/python3:$PATH&quot;\n<\/code><\/pre>\n<\/li>\n<li>\u4fdd\u5b58\u5e76\u9000\u51fa\u7f16\u8f91\u5668\u540e\uff0c\u8fd0\u884c<code>source ~\/.bash_profile<\/code>\u6216<code>source ~\/.zshrc<\/code>\u547d\u4ee4\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002<br \/>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528Python\u4e86\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1Python\u7684\u5b89\u88c5\u53ca\u8def\u5f84\u8bbe\u7f6e\u662f\u5426\u6210\u529f\uff1f<\/strong><br \/>\u5b89\u88c5\u548c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u9a8c\u8bc1Python\u662f\u5426\u6b63\u786e\u5b89\u88c5\uff1a  <\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u3002  <\/li>\n<li>\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u5982\u679c\u6b63\u786e\u5b89\u88c5\uff0c\u60a8\u5c06\u770b\u5230Python\u7684\u7248\u672c\u4fe1\u606f\u3002  <\/li>\n<li>\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u8fd0\u884c<code>pip --version<\/code>\u6765\u68c0\u67e5pip\u7684\u5b89\u88c5\u60c5\u51b5\u3002<br \/>\u5982\u679c\u4e0a\u8ff0\u547d\u4ee4\u90fd\u8fd4\u56de\u4e86\u7248\u672c\u4fe1\u606f\uff0c\u8bf4\u660ePython\u53ca\u5176\u8def\u5f84\u8bbe\u7f6e\u6210\u529f\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b89\u88c5Python\u5e76\u8bbe\u7f6e\u8def\u5f84\uff0c\u4f60\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3001\u6dfb\u52a0Python\u8def\u5f84\u5230\u73af\u5883\u53d8\u91cf\u3001\u9a8c\u8bc1 [&hellip;]","protected":false},"author":3,"featured_media":1154978,"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\/1154972"}],"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=1154972"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154972\/revisions"}],"predecessor-version":[{"id":1154979,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154972\/revisions\/1154979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154978"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}