{"id":52039,"date":"2024-10-08T23:24:21","date_gmt":"2024-10-08T15:24:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/52039\/"},"modified":"2024-11-14T10:40:40","modified_gmt":"2024-11-14T02:40:40","slug":"ubuntu-%e5%b0%87-python-%e5%be%9e%e9%bb%98%e8%aa%8d%e7%9a%84-2-7-%e5%8d%87%e7%b4%9a%e5%88%b0-3-%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/52039\/","title":{"rendered":"Ubuntu \u5c07 Python \u5f9e\u9ed8\u8a8d\u7684 2.7 \u5347\u7d1a\u5230 3.* \u7248\u672c"},"content":{"rendered":"<h1 id=\"ubuntu-%e5%b0%87-python-%e5%be%9e%e9%bb%98%e8%aa%8d%e7%9a%84-2-7-%e5%8d%87%e7%b4%9a%e5%88%b0-3-%e7%89%88%e6%9c%ac-CMuzIKURVz\">Ubuntu \u5c07 Python \u5f9e\u9ed8\u8a8d\u7684 2.7 \u5347\u7d1a\u5230 3.* \u7248\u672c<\/h1>\n<p>\u96a8\u8457\u79d1\u6280\u7684\u9032\u6b65\uff0cPython \u8a9e\u8a00\u7684\u4f7f\u7528\u8d8a\u4f86\u8d8a\u5ee3\u6cdb\u3002\u81ea 2020 \u5e74 1 \u6708 1 \u65e5\u8d77\uff0cPython 2.7 \u6b63\u5f0f\u505c\u6b62\u652f\u6301\uff0c\u9019\u4f7f\u5f97\u8a31\u591a\u4f7f\u7528 Ubuntu \u7684\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u9762\u81e8\u8457\u5c07 Python \u5347\u7d1a\u5230 3.* \u7248\u672c\u7684\u5fc5\u8981\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5728 Ubuntu \u7cfb\u7d71\u4e2d\u5982\u4f55\u5c07 Python \u5f9e\u9ed8\u8a8d\u7684 2.7 \u5347\u7d1a\u5230 3.* \u7248\u672c\uff0c\u4ee5\u53ca\u9019\u4e00\u904e\u7a0b\u4e2d\u7684\u6ce8\u610f\u4e8b\u9805\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e8%a6%81%e5%8d%87%e7%b4%9a%e5%88%b0-python-3-%ef%bc%9f-CMuzIKURVz\">\u70ba\u4ec0\u9ebc\u8981\u5347\u7d1a\u5230 Python 3.*\uff1f<\/h2>\n<p>Python 3.* \u7248\u672c\u5f15\u5165\u4e86\u8a31\u591a\u65b0\u7279\u6027\u548c\u6539\u9032\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u66f4\u597d\u7684 Unicode \u652f\u6301\uff1a<\/strong>Python 3 \u5c0d\u5b57\u7b26\u4e32\u7684\u8655\u7406\u66f4\u52a0\u53cb\u597d\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u591a\u8a9e\u8a00\u6587\u672c\u6642\u3002<\/li>\n<li><strong>\u8a9e\u6cd5\u6539\u9032\uff1a<\/strong>\u4f8b\u5982\uff0cprint \u51fd\u6578\u7684\u4f7f\u7528\u8b8a\u5f97\u66f4\u52a0\u4e00\u81f4\uff0c\u4e26\u4e14\u5f15\u5165\u4e86\u65b0\u7684\u8a9e\u6cd5\u7279\u6027\uff0c\u5982 f-string\u3002<\/li>\n<li><strong>\u6a19\u6e96\u5eab\u7684\u6539\u9032\uff1a<\/strong>\u8a31\u591a\u6a19\u6e96\u5eab\u6a21\u584a\u5728 Python 3 \u4e2d\u5f97\u5230\u4e86\u91cd\u69cb\u548c\u512a\u5316\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%aa%a2%e6%9f%a5%e7%95%b6%e5%89%8d-python-%e7%89%88%e6%9c%ac-CMuzIKURVz\">\u6aa2\u67e5\u7576\u524d Python \u7248\u672c<\/h2>\n<p>\u5728\u5347\u7d1a\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u6aa2\u67e5\u7576\u524d\u7cfb\u7d71\u4e2d\u5b89\u88dd\u7684 Python \u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>python --version<\/code><\/pre>\n<p>\u5982\u679c\u986f\u793a\u7684\u662f Python 2.7.x\uff0c\u5247\u9700\u8981\u9032\u884c\u5347\u7d1a\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-python-3-CMuzIKURVz\">\u5b89\u88dd Python 3.*<\/h2>\n<p>\u5728 Ubuntu \u4e2d\u5b89\u88dd Python 3 \u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd\u6700\u65b0\u7248\u672c\u7684 Python 3\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install python3<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u518d\u6b21\u6aa2\u67e5 Python \u7248\u672c\uff1a<\/p>\n<pre><code>python3 --version<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%ae-python-3-%e7%82%ba%e9%bb%98%e8%aa%8d%e7%89%88%e6%9c%ac-CMuzIKURVz\">\u8a2d\u7f6e Python 3 \u70ba\u9ed8\u8a8d\u7248\u672c<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5c07 Python 3 \u8a2d\u7f6e\u70ba\u7cfb\u7d71\u7684\u9ed8\u8a8d Python \u7248\u672c\u3002\u9019\u53ef\u4ee5\u901a\u904e\u66f4\u65b0 alternatives \u7cfb\u7d71\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3 1<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u7576\u60a8\u8f38\u5165 <code>python<\/code> \u547d\u4ee4\u6642\uff0c\u7cfb\u7d71\u5c07\u81ea\u52d5\u4f7f\u7528 Python 3\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-pip3-CMuzIKURVz\">\u5b89\u88dd pip3<\/h2>\n<p>\u70ba\u4e86\u65b9\u4fbf\u7ba1\u7406 Python \u5305\uff0c\u5efa\u8b70\u5b89\u88dd pip3\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt install python3-pip<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5 pip3 \u7684\u7248\u672c\uff1a<\/p>\n<pre><code>pip3 --version<\/code><\/pre>\n<h2 id=\"%e8%bd%89%e6%8f%9b%e7%8f%be%e6%9c%89%e4%bb%a3%e7%a2%bc-CMuzIKURVz\">\u8f49\u63db\u73fe\u6709\u4ee3\u78bc<\/h2>\n<p>\u5982\u679c\u60a8\u6709\u73fe\u6709\u7684 Python 2 \u4ee3\u78bc\uff0c\u5247\u9700\u8981\u5c07\u5176\u8f49\u63db\u70ba Python 3\u3002\u53ef\u4ee5\u4f7f\u7528 <code>2to3<\/code> \u5de5\u5177\u4f86\u81ea\u52d5\u8f49\u63db\u4ee3\u78bc\uff1a<\/p>\n<pre><code>2to3 -w your_script.py<\/code><\/pre>\n<p>\u9019\u5c07\u81ea\u52d5\u4fee\u6539\u60a8\u7684\u4ee3\u78bc\u4e26\u751f\u6210\u5099\u4efd\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-CMuzIKURVz\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u5347\u7d1a\u904e\u7a0b\u4e2d\uff0c\u8acb\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u67d0\u4e9b\u7b2c\u4e09\u65b9\u5eab\u53ef\u80fd\u4e0d\u652f\u6301 Python 3\uff0c\u56e0\u6b64\u5728\u5347\u7d1a\u4e4b\u524d\u8acb\u6aa2\u67e5\u6240\u9700\u5eab\u7684\u517c\u5bb9\u6027\u3002<\/li>\n<li>\u5728\u751f\u7522\u74b0\u5883\u4e2d\u9032\u884c\u5347\u7d1a\u6642\uff0c\u5efa\u8b70\u5148\u5728\u6e2c\u8a66\u74b0\u5883\u4e2d\u9032\u884c\u5145\u5206\u6e2c\u8a66\u3002<\/li>\n<li>\u4fdd\u6301\u7cfb\u7d71\u548c\u6240\u6709\u76f8\u95dc\u5305\u7684\u66f4\u65b0\uff0c\u4ee5\u78ba\u4fdd\u5b89\u5168\u6027\u548c\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CMuzIKURVz\">\u7e3d\u7d50<\/h2>\n<p>\u5c07 Ubuntu \u4e2d\u7684 Python \u5f9e\u9ed8\u8a8d\u7684 2.7 \u5347\u7d1a\u5230 3.* \u7248\u672c\u662f\u4e00\u500b\u5fc5\u8981\u7684\u6b65\u9a5f\uff0c\u4ee5\u78ba\u4fdd\u958b\u767c\u74b0\u5883\u7684\u73fe\u4ee3\u5316\u548c\u5b89\u5168\u6027\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5b8c\u6210\u9019\u4e00\u904e\u7a0b\uff0c\u4e26\u4eab\u53d7 Python 3 \u5e36\u4f86\u7684\u5404\u7a2e\u65b0\u7279\u6027\u548c\u6539\u9032\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u5de5\u4f5c\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\uff0c\u63d0\u4f9b\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u548c <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a> \u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ubuntu \u5347\u7d1a Python \u5f9e\u9ed8\u8a8d\u7684 2.7 \u5230 3.* \u7248\u672c\uff0c\u63d0\u5347\u6027\u80fd\u8207\u5b89\u5168\u6027\uff0c\u652f\u6301\u73fe\u4ee3\u958b\u767c\u9700\u6c42\uff0c\u78ba\u4fdd\u517c\u5bb9\u6027\u8207\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-52039","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=52039"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52039\/revisions"}],"predecessor-version":[{"id":52040,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52039\/revisions\/52040"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=52039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=52039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=52039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}