{"id":939140,"date":"2024-12-26T20:27:42","date_gmt":"2024-12-26T12:27:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939140.html"},"modified":"2024-12-26T20:27:44","modified_gmt":"2024-12-26T12:27:44","slug":"ubuntu%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939140.html","title":{"rendered":"ubuntu\u5982\u4f55\u4fee\u6539python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074321\/9e6dc4c5-8a90-4d36-990b-cc868ea3640f.webp\" alt=\"ubuntu\u5982\u4f55\u4fee\u6539python\" \/><\/p>\n<p><p> <strong>\u5728Ubuntu\u7cfb\u7edf\u4e2d\u4fee\u6539Python\u7248\u672c\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528update-alternatives\u5de5\u5177\u3001\u624b\u52a8\u66f4\u6539\u7b26\u53f7\u94fe\u63a5\uff08symlinks\uff09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08virtual environments\uff09\u3002\u63a8\u8350\u4f7f\u7528\u865a\u62df\u73af\u5883\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684Python\u7248\u672c\uff0c\u5e76\u80fd\u7075\u6d3b\u7ba1\u7406\u591a\u4e2a\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u5728\u5f00\u53d1Python\u9879\u76ee\u65f6\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u4e0d\u5f71\u54cd\u7cfb\u7edfPython\u7248\u672c\u7684\u60c5\u51b5\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684Python\u73af\u5883\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u5e93\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b83\u4eec\u4e4b\u95f4\u7684\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001UPDATE-ALTERNATIVES\u5de5\u5177<\/h3>\n<\/p>\n<p><p>update-alternatives\u662f\u4e00\u4e2aUbuntu\u63d0\u4f9b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u7ba1\u7406\u4e0d\u540c\u8f6f\u4ef6\u7248\u672c\u4e4b\u95f4\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5update-alternatives\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570Ubuntu\u7cfb\u7edf\u4e0a\uff0cupdate-alternatives\u5de5\u5177\u9ed8\u8ba4\u5b89\u88c5\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install update-alternatives<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528update-alternatives\u5207\u6362Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u4e0d\u540c\u7684Python\u7248\u672c\u6dfb\u52a0\u5230update-alternatives\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3.8 1<\/p>\n<p>sudo update-alternatives --install \/usr\/bin\/python python \/usr\/bin\/python3.9 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u901a\u8fc7\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>\u5728\u51fa\u73b0\u7684\u5217\u8868\u4e2d\u9009\u62e9\u6240\u9700\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u624b\u52a8\u66f4\u6539\u7b26\u53f7\u94fe\u63a5<\/h3>\n<\/p>\n<p><p>\u7b26\u53f7\u94fe\u63a5\u662f\u4e00\u79cd\u6587\u4ef6\u7c7b\u578b\uff0c\u5b83\u6307\u5411\u53e6\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u3002\u901a\u8fc7\u66f4\u6539\u7b26\u53f7\u94fe\u63a5\u53ef\u4ee5\u6539\u53d8\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u67e5\u627e\u7cfb\u7edf\u4e2d\u7684Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u67e5\u770b\u7cfb\u7edf\u4e2d\u5df2\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><h4>2.2 \u66f4\u6539\u7b26\u53f7\u94fe\u63a5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u9700\u8981\u5c06\u9ed8\u8ba4\u7684Python\u7248\u672c\u66f4\u6539\u4e3aPython 3.9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo ln -sf \/usr\/bin\/python3.9 \/usr\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4fee\u6539\u7684\u662f\u7cfb\u7edf\u5168\u5c40\u7684Python\u7248\u672c\uff0c\u56e0\u6b64\u9700\u8981\u6ce8\u610f\u5bf9\u7cfb\u7edf\u5176\u4ed6\u8f6f\u4ef6\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u7cfb\u7edfPython\u7248\u672c\u7684\u60c5\u51b5\u4e0b\uff0c\u7075\u6d3b\u7ba1\u7406\u548c\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5\u865a\u62df\u73af\u5883\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Python\u7684virtualenv\u5de5\u5177\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<p><h4>3.2 \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\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.9 -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amyenv\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>3.3 \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u4e0d\u540c\u7684Python\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.4 \u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5Python\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u6fc0\u6d3b\u540e\uff0c\u4f7f\u7528pip\u5b89\u88c5\u6240\u9700\u7684Python\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u4e0d\u5f71\u54cd\u7cfb\u7edf\u7684Python\u73af\u5883\uff0c\u6240\u6709\u7684\u5305\u5b89\u88c5\u548c\u7248\u672c\u7ba1\u7406\u90fd\u53ea\u5728\u865a\u62df\u73af\u5883\u4e2d\u751f\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pyenv\u7ba1\u7406Python\u7248\u672c<\/h3>\n<\/p>\n<p><p>Pyenv\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u5b89\u88c5\u548c\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5Pyenv<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u7cfb\u7edf\u6ee1\u8db3\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt update<\/p>\n<p>sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pyenv\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/pyenv.run | bash<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6839\u636e\u7ec8\u7aef\u8f93\u51fa\uff0c\u66f4\u65b0shell\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>~\/.bashrc<\/code>\u6216<code>~\/.zshrc<\/code>\uff09\u4ee5\u52a0\u8f7dPyenv\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528Pyenv\u5b89\u88c5Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u67d0\u4e2aPython\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv install 3.9.7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u8bbe\u7f6e\u5168\u5c40\u6216\u5c40\u90e8Python\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pyenv\u8bbe\u7f6e\u5168\u5c40Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv global 3.9.7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u5728\u67d0\u4e2a\u9879\u76ee\u76ee\u5f55\u4e0b\u8bbe\u7f6e\u5c40\u90e8Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv local 3.9.7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pyenv\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u8f7b\u677e\u5207\u6362\u548c\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff0c\u975e\u5e38\u9002\u5408\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Ubuntu\u4e0a\u4fee\u6539Python\u7248\u672c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08\u5982venv\u6216virtualenv\uff09\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684Python\u7248\u672c\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u9879\u76ee\u521b\u5efa\u72ec\u7acb\u7684\u73af\u5883\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u5207\u6362Python\u7248\u672c\u7684\u5f00\u53d1\u8005\uff0cPyenv\u4e5f\u662f\u4e00\u4e2a\u503c\u5f97\u8003\u8651\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8Python\u5f00\u53d1\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Ubuntu\u4e0a\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684Python\uff1f<\/strong><br \/>\u5728Ubuntu\u4e0a\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u6765\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684Python\u3002\u9996\u5148\uff0c\u66f4\u65b0\u8f6f\u4ef6\u5305\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7248\u672c\uff1a  <\/p>\n<pre><code class=\"language-bash\">sudo apt update  \nsudo apt install python3.x  \n<\/code><\/pre>\n<p>\u5c06<code>x<\/code>\u66ff\u6362\u4e3a\u60a8\u60f3\u8981\u5b89\u88c5\u7684\u5177\u4f53\u7248\u672c\u53f7\uff0c\u4f8b\u5982<code>python3.9<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>python3.x --version<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Ubuntu\u4e2d\u5207\u6362Python\u7684\u9ed8\u8ba4\u7248\u672c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\u7684Python\uff0c\u53ef\u80fd\u5e0c\u671b\u66f4\u6539\u9ed8\u8ba4\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528<code>update-alternatives<\/code>\u547d\u4ee4\u6765\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-bash\">sudo update-alternatives --install \/usr\/bin\/python3 python3 \/usr\/bin\/python3.x 1  \n<\/code><\/pre>\n<p>\u66ff\u6362<code>3.x<\/code>\u4e3a\u60a8\u5e0c\u671b\u8bbe\u4e3a\u9ed8\u8ba4\u7684\u7248\u672c\u3002\u5b8c\u6210\u540e\uff0c\u8fd0\u884c<code>python3 --version<\/code>\u6765\u68c0\u67e5\u66f4\u6539\u662f\u5426\u751f\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Ubuntu\u4e2d\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406Python\u9879\u76ee\uff1f<\/strong><br \/>\u865a\u62df\u73af\u5883\u662f\u7ba1\u7406Python\u9879\u76ee\u4f9d\u8d56\u7684\u6709\u6548\u65b9\u5f0f\u3002\u5728Ubuntu\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a  <\/p>\n<pre><code class=\"language-bash\">python3 -m venv myprojectenv  \n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myprojectenv<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4e2d\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u5e93\u3002\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u4f9d\u8d56\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u4e2d\u7684\u5168\u5c40Python\u73af\u5883\uff1a  <\/p>\n<pre><code class=\"language-bash\">source myprojectenv\/bin\/activate  \n<\/code><\/pre>\n<p>\u6fc0\u6d3b\u540e\uff0c\u60a8\u53ef\u4ee5\u5b89\u88c5\u4f9d\u8d56\u5e76\u8fd0\u884c\u9879\u76ee\uff0c\u5b8c\u6210\u540e\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>deactivate<\/code>\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Ubuntu\u7cfb\u7edf\u4e2d\u4fee\u6539Python\u7248\u672c\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528update-alternatives\u5de5\u5177\u3001\u624b\u52a8\u66f4 [&hellip;]","protected":false},"author":3,"featured_media":939144,"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\/939140"}],"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=939140"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939140\/revisions"}],"predecessor-version":[{"id":939145,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939140\/revisions\/939145"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/939144"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=939140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=939140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=939140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}