{"id":1166440,"date":"2025-01-15T15:32:13","date_gmt":"2025-01-15T07:32:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1166440.html"},"modified":"2025-01-15T15:32:16","modified_gmt":"2025-01-15T07:32:16","slug":"%e4%b9%8c%e7%8f%ad%e5%9b%be%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1166440.html","title":{"rendered":"\u4e4c\u73ed\u56fe\u5982\u4f55\u67e5\u770bpython\u7248\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210551\/a6df205d-4c89-44cf-ba21-dbb609380857.webp\" alt=\"\u4e4c\u73ed\u56fe\u5982\u4f55\u67e5\u770bpython\u7248\u672c\" \/><\/p>\n<p><p> <strong>\u4e4c\u73ed\u56fe\u67e5\u770bPython\u7248\u672c\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u811a\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u5de5\u5177\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u67e5\u770bPython\u7248\u672c\u3002\u4f60\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\uff0c\u5e76\u8f93\u5165\u5982\u4e0b\u547d\u4ee4\uff1a<code>python --version<\/code> \u6216\u8005 <code>python3 --version<\/code>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u4ed6\u76f8\u5173\u7684\u6280\u672f\u7ec6\u8282\u548c\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u67e5\u770bPython\u7248\u672c<\/h3>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u5de5\u5177\u662f\u67e5\u770bPython\u7248\u672c\u6700\u76f4\u63a5\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\uff0c\u5e76\u8f93\u5165\u76f8\u5e94\u7684\u547d\u4ee4\uff0c\u5373\u53ef\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528python\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u663e\u793a\u51fa\u4f60\u5f53\u524d\u7cfb\u7edf\u4e2d\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002\u5982\u679c\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684\u662fPython 2.x\u7248\u672c\uff0c\u90a3\u4e48\u8fd9\u4e2a\u547d\u4ee4\u5c06\u4f1a\u8fd4\u56de\u7c7b\u4f3c\u4e8e <code>Python 2.7.17<\/code> \u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528python3\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5927\u591a\u6570\u73b0\u4ee3\u7cfb\u7edf\u5df2\u7ecf\u9ed8\u8ba4\u5b89\u88c5\u4e86Python 3.x\u7248\u672c\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u67e5\u770bPython 3\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u8fd4\u56de\u7c7b\u4f3c\u4e8e <code>Python 3.8.5<\/code> \u7684\u7ed3\u679c\uff0c\u8868\u793a\u4f60\u5f53\u524d\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python 3\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7Python\u811a\u672c\u67e5\u770bPython\u7248\u672c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u52a8\u6001\u83b7\u53d6Python\u7684\u7248\u672c\u4fe1\u606f\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7Python\u5185\u7f6e\u7684 <code>sys<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Python version&quot;)<\/p>\n<p>print(sys.version)<\/p>\n<p>print(&quot;Version info.&quot;)<\/p>\n<p>print(sys.version_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u4ee3\u7801\u4fdd\u5b58\u4e3a <code>check_version.py<\/code> \u6587\u4ef6\uff0c\u7136\u540e\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u8be5\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python check_version.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python3 check_version.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u8f93\u51fa\u8be6\u7ec6\u7684Python\u7248\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u7248\u672c\u53f7\u3001\u7f16\u8bd1\u65e5\u671f\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u5de5\u5177\u67e5\u770bPython\u7248\u672c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08Virtual Environment\uff09\u662f\u73b0\u4ee3Python\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u6280\u672f\u3002\u865a\u62df\u73af\u5883\u5141\u8bb8\u4f60\u5728\u540c\u4e00\u7cfb\u7edf\u4e2d\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684Python\u73af\u5883\uff0c\u6bcf\u4e2a\u73af\u5883\u53ef\u4ee5\u6709\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u5305\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>virtualenv<\/code> \u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sudo apt-get install python3-venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python3 -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>myenv<\/code> \u7684\u6587\u4ef6\u5939\uff0c\u5305\u542b\u72ec\u7acb\u7684Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff0c\u4f7f\u5176\u6210\u4e3a\u5f53\u524d\u7ec8\u7aef\u4f1a\u8bdd\u7684\u9ed8\u8ba4Python\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\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u865a\u62df\u73af\u5883\u4e2d\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9000\u51fa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u6280\u672f\u7ec6\u8282<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u6280\u672f\u7ec6\u8282\u548c\u5b9e\u7528\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u548c\u67e5\u770bPython\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528which\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>which<\/code> \u547d\u4ee4\u6765\u67e5\u627ePython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">which python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">which python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u8fd4\u56dePython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u5e2e\u52a9\u4f60\u786e\u8ba4\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u66f4\u65b0\u66ff\u4ee3\u65b9\u6848<\/h4>\n<\/p>\n<p><p>\u5728Ubuntu\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>update-alternatives<\/code> \u547d\u4ee4\u6765\u7ba1\u7406\u4e0d\u540c\u7248\u672c\u7684Python\u3002\u9996\u5148\uff0c\u6dfb\u52a0\u4e0d\u540c\u7248\u672c\u7684Python\u5230\u66ff\u4ee3\u65b9\u6848\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python2.7 1<\/p>\n<p>sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3.8 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u8fdb\u884c\u5207\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sudo update-alternatives --config python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u663e\u793a\u4e00\u4e2a\u83dc\u5355\uff0c\u5141\u8bb8\u4f60\u9009\u62e9\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Ubuntu\u7cfb\u7edf\u4e2d\u67e5\u770b\u548c\u7ba1\u7406Python\u7248\u672c\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001Python\u811a\u672c\uff0c\u8fd8\u662f\u865a\u62df\u73af\u5883\u5de5\u5177\uff0c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6\u6240\u9700\u7684Python\u7248\u672c\u4fe1\u606f\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u672f\u7ec6\u8282\uff0c\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4f60\u7684\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7ef4\u62a4\u4f60\u7684Python\u73af\u5883\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5c06\u4f7f\u4f60\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u4e4c\u73ed\u56fe\u4e2d\u68c0\u67e5\u5df2\u5b89\u88c5\u7684Python\u7248\u672c\uff1f<\/strong><br \/>\u5728\u4e4c\u73ed\u56fe\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u68c0\u67e5\u5df2\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165\u547d\u4ee4<code>python --version<\/code>\u6216\u8005<code>python3 --version<\/code>\uff0c\u5373\u53ef\u67e5\u770bPython 2\u6216Python 3\u7684\u7248\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u4e4c\u73ed\u56fe\u4e2d\u6ca1\u6709\u5b89\u88c5Python\uff0c\u5e94\u8be5\u5982\u4f55\u5b89\u88c5\uff1f<\/strong><br \/>\u5982\u679c\u7cfb\u7edf\u4e2d\u6ca1\u6709\u5b89\u88c5Python\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u4f7f\u7528\u547d\u4ee4<code>sudo apt update<\/code>\u66f4\u65b0\u8f6f\u4ef6\u5305\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>sudo apt install python3<\/code>\u6765\u5b89\u88c5Python 3\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u547d\u4ee4\u6765\u786e\u8ba4\u5b89\u88c5\u7684\u7248\u672c\u3002<\/p>\n<p><strong>\u5728\u4e4c\u73ed\u56fe\u4e2d\uff0c\u5982\u4f55\u540c\u65f6\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>pyenv<\/code>\u5de5\u5177\u6765\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002\u5b89\u88c5<code>pyenv<\/code>\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pyenv install &lt;version&gt;<\/code>\u6765\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684Python\uff0c\u5e76\u4f7f\u7528<code>pyenv global &lt;version&gt;<\/code>\u8bbe\u7f6e\u9ed8\u8ba4\u7248\u672c\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u754c\u9762\u67e5\u770bPython\u7248\u672c\uff1f<\/strong><br \/>\u5728\u4e4c\u73ed\u56fe\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u56fe\u5f62\u754c\u9762\u67e5\u770bPython\u7248\u672c\u3002\u6253\u5f00\u201c\u7ec8\u7aef\u201d\u5e94\u7528\u7a0b\u5e8f\uff0c\u7136\u540e\u5728\u83dc\u5355\u680f\u4e2d\u9009\u62e9\u201c\u5e2e\u52a9\u201d\u6216\u201c\u5173\u4e8e\u201d\uff0c\u5728\u6b64\u5904\u53ef\u80fd\u4f1a\u663e\u793a\u7cfb\u7edf\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ecPython\u7248\u672c\u3002\u5982\u679c\u6ca1\u6709\u663e\u793a\uff0c\u53ef\u4ee5\u4f9d\u7136\u4f7f\u7528\u7ec8\u7aef\u547d\u4ee4\u6765\u786e\u8ba4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e4c\u73ed\u56fe\u67e5\u770bPython\u7248\u672c\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u811a\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u5de5\u5177\u3002 \u5176\u4e2d\uff0c\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1166445,"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\/1166440"}],"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=1166440"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166440\/revisions"}],"predecessor-version":[{"id":1166446,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1166440\/revisions\/1166446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1166445"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1166440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1166440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1166440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}