{"id":981441,"date":"2024-12-27T07:02:37","date_gmt":"2024-12-26T23:02:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981441.html"},"modified":"2024-12-27T07:02:40","modified_gmt":"2024-12-26T23:02:40","slug":"ubuntu-%e5%a6%82%e4%bd%95%e5%88%87%e6%8d%a2python%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981441.html","title":{"rendered":"ubuntu \u5982\u4f55\u5207\u6362python\u7248\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210402\/49a8beab-92fa-4f31-b9fc-8f6852c74d25.webp\" alt=\"ubuntu \u5982\u4f55\u5207\u6362python\u7248\u672c\" \/><\/p>\n<p><p> <strong>\u5728Ubuntu\u4e2d\u5207\u6362Python\u7248\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>update-alternatives<\/code>\u5de5\u5177\u3001\u76f4\u63a5\u4fee\u6539\u7b26\u53f7\u94fe\u63a5\u6216\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>update-alternatives<\/code>\u5de5\u5177\u662f\u6bd4\u8f83\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7cfb\u7edf\u5316\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u591a\u4e2a\u7248\u672c\u7684\u8f6f\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u4ee5<code>update-alternatives<\/code>\u5de5\u5177\u4e3a\u4f8b\u8fdb\u884c\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>update-alternatives<\/code>\u5de5\u5177<\/p>\n<\/p>\n<p><p><code>update-alternatives<\/code>\u662fUbuntu\u7528\u4e8e\u7ba1\u7406\u4e0d\u540c\u7248\u672c\u8f6f\u4ef6\u7684\u5de5\u5177\u3002\u4f7f\u7528\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u7cfb\u7edf\u4e2d\u5207\u6362\u4e0d\u540c\u7248\u672c\u7684Python\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u4f60\u9700\u8981\u7684\u6240\u6709Python\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ls \/usr\/bin\/python*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u7f3a\u5c11\u67d0\u4e2a\u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3.x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d<code>3.x<\/code>\u8868\u793a\u4f60\u9700\u8981\u7684\u5177\u4f53Python\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e<code>update-alternatives<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e3a\u6bcf\u4e2aPython\u7248\u672c\u914d\u7f6e<code>update-alternatives<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3.x 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u6b21\u6dfb\u52a0\u65b0\u7248\u672c\u65f6\uff0c\u5c06\u8def\u5f84<code>\/usr\/bin\/python3.x<\/code>\u66ff\u6362\u4e3a\u5b9e\u9645\u7684Python\u7248\u672c\u8def\u5f84\uff0c\u5e76\u786e\u4fdd\u6700\u540e\u7684\u4f18\u5148\u7ea7\u6570\u5b57\u662f\u552f\u4e00\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5207\u6362Python\u7248\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5207\u6362Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo update-alternatives --config python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7cfb\u7edf\u4f1a\u663e\u793a\u4e00\u4e2a\u83dc\u5355\uff0c\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684Python\u7248\u672c\u3002\u8f93\u5165\u5bf9\u5e94\u7684\u6570\u5b57\u5373\u53ef\u5207\u6362\u5230\u76f8\u5e94\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u76f4\u63a5\u4fee\u6539\u7b26\u53f7\u94fe\u63a5<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u624b\u52a8\u5207\u6362Python\u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u7b26\u53f7\u94fe\u63a5\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u5f53\u524dPython\u7b26\u53f7\u94fe\u63a5<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ls -l \/usr\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u663e\u793a\u5f53\u524d<code>python<\/code>\u6307\u5411\u7684\u5177\u4f53\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fee\u6539\u7b26\u53f7\u94fe\u63a5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4fee\u6539\u7b26\u53f7\u94fe\u63a5\u4ee5\u6307\u5411\u65b0\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo ln -sf \/usr\/bin\/python3.x \/usr\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d<code>3.x<\/code>\u662f\u4f60\u5e0c\u671b\u5207\u6362\u5230\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/p>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u5728\u9879\u76ee\u7ea7\u522b\u7ba1\u7406Python\u7248\u672c\u548c\u5e93\u7684\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5<code>venv<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u8fd8\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3.x -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u66ff\u6362<code>3.x<\/code>\u4e3a\u4f60\u9700\u8981\u7684Python\u7248\u672c\uff0c\u5e76\u5c06<code>myenv<\/code>\u66ff\u6362\u4e3a\u4f60\u7684\u865a\u62df\u73af\u5883\u540d\u79f0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u6240\u6709\u7684Python\u547d\u4ee4\u90fd\u5c06\u5728\u8fd9\u4e2a\u73af\u5883\u4e2d\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9000\u51fa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cUbuntu\u4e2d\u5207\u6362Python\u7248\u672c\u7684\u65b9\u6cd5\u591a\u6837\u5316\uff0c\u6839\u636e\u9700\u8981\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>update-alternatives<\/code>\u5de5\u5177\u8fdb\u884c\u7ba1\u7406\u662f\u6bd4\u8f83\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7cfb\u7edf\u5316\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u548c\u5207\u6362\u8f6f\u4ef6\u7248\u672c\u3002\u800c\u865a\u62df\u73af\u5883\u5219\u66f4\u9002\u5408\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u7248\u672c\u63a7\u5236\u548c\u9694\u79bb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Ubuntu\u4e0a\u68c0\u67e5\u5f53\u524dPython\u7248\u672c\uff1f<\/strong><br \/>\u5728Ubuntu\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u6765\u68c0\u67e5\u5f53\u524d\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u8fd9\u5c06\u663e\u793a\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u7248\u672c\u53f7\u3002\u5982\u679c\u9700\u8981\u67e5\u770b\u6240\u6709\u5df2\u5b89\u88c5\u7684Python\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ls \/usr\/bin\/python*<\/code>\u547d\u4ee4\u5217\u51fa\u6240\u6709\u7684Python\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Ubuntu\u4e2d\u5207\u6362Python\u7248\u672c\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5207\u6362Python\u7248\u672c\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u4e8e\u7279\u5b9aPython\u7248\u672c\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u5e93\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u4e00\u4e9b\u7cfb\u7edf\u5de5\u5177\u53ef\u80fd\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u7248\u672c\uff0c\u6539\u52a8\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u7a33\u5b9a\u3002\u5efa\u8bae\u5728\u5207\u6362\u7248\u672c\u4e4b\u524d\u5907\u4efd\u91cd\u8981\u6570\u636e\uff0c\u5e76\u8003\u8651\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4e0d\u540c\u9879\u76ee\u6240\u9700\u7684Python\u7248\u672c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528update-alternatives\u547d\u4ee4\u6765\u7ba1\u7406Python\u7248\u672c\uff1f<\/strong><br \/>\u5728Ubuntu\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>update-alternatives<\/code>\u547d\u4ee4\u6765\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002\u9996\u5148\uff0c\u4f7f\u7528\u547d\u4ee4<code>sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3.x 1<\/code>\uff08\u5c06<code>3.x<\/code>\u66ff\u6362\u4e3a\u5e0c\u671b\u8bbe\u7f6e\u7684\u7248\u672c\u53f7\uff09\u6765\u6dfb\u52a0\u65b0\u7684\u7248\u672c\u3002\u63a5\u7740\uff0c\u6267\u884c<code>sudo update-alternatives --config python<\/code>\u547d\u4ee4\uff0c\u7cfb\u7edf\u5c06\u63d0\u793a\u9009\u62e9\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002\u6309\u7167\u63d0\u793a\u8f93\u5165\u76f8\u5e94\u7684\u6570\u5b57\u5373\u53ef\u5b8c\u6210\u7248\u672c\u5207\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Ubuntu\u4e2d\u5207\u6362Python\u7248\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528update-alternatives\u5de5\u5177\u3001\u76f4\u63a5\u4fee\u6539\u7b26\u53f7\u94fe\u63a5\u6216 [&hellip;]","protected":false},"author":3,"featured_media":981448,"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\/981441"}],"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=981441"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981441\/revisions"}],"predecessor-version":[{"id":981450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981441\/revisions\/981450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981448"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}