{"id":945110,"date":"2024-12-26T23:20:47","date_gmt":"2024-12-26T15:20:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/945110.html"},"modified":"2024-12-26T23:20:49","modified_gmt":"2024-12-26T15:20:49","slug":"linux-%e5%a6%82%e4%bd%95%e5%8d%87%e7%ba%a7python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/945110.html","title":{"rendered":"linux \u5982\u4f55\u5347\u7ea7python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081824\/47107ec5-094b-4ada-8051-b7318450a429.webp\" alt=\"linux \u5982\u4f55\u5347\u7ea7python\" \/><\/p>\n<p><p> <strong>\u5728Linux\u7cfb\u7edf\u4e0a\u5347\u7ea7Python\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u3001\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u3001\u4f7f\u7528Python\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u5982Pyenv\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u800c\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u548c\u4f7f\u7528Pyenv\u5219\u9002\u5408\u9700\u8981\u7279\u5b9aPython\u7248\u672c\u7684\u7528\u6237\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5347\u7ea7Python\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u8fdb\u884c\u5347\u7ea7<\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u90fd\u5e26\u6709\u81ea\u5df1\u7684\u5305\u7ba1\u7406\u5668\uff0c\u5982Ubuntu\u7684APT\u3001Fedora\u7684DNF\u3001CentOS\u7684YUM\u7b49\u3002\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u662f\u5347\u7ea7Python\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>Ubuntu\u7cfb\u7edf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Ubuntu\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528APT\u5305\u7ba1\u7406\u5668\u5347\u7ea7Python\u3002\u9996\u5148\uff0c\u66f4\u65b0\u5305\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt update<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5347\u7ea7Python\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u5c06\u7cfb\u7edf\u4e2d\u7684Python\u5347\u7ea7\u5230\u4ed3\u5e93\u4e2d\u6700\u65b0\u7684\u7a33\u5b9a\u7248\u672c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>Fedora\u7cfb\u7edf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Fedora\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528DNF\u5305\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo dnf install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>CentOS\u7cfb\u7edf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728CentOS\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528YUM\u5305\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u81ea\u52a8\u4ece\u5b98\u65b9\u4ed3\u5e93\u4e2d\u83b7\u53d6\u6700\u65b0\u7684Python\u7248\u672c\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u7279\u5b9a\u7248\u672c\u7684Python\uff0c\u6216\u8005\u5b98\u65b9\u4ed3\u5e93\u4e2d\u7684\u7248\u672c\u8fc7\u65e7\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e90\u7801\u7f16\u8bd1\u7684\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0b\u8f7d\u6e90\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u8bbf\u95eePython\u5b98\u65b9<a href=\"https:\/\/www.python.org\/downloads\/\">\u4e0b\u8f7d\u9875\u9762<\/a>\uff0c\u9009\u62e9\u9700\u8981\u7684\u7248\u672c\u5e76\u4e0b\u8f7d\u5176\u6e90\u7801\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">wget https:\/\/www.python.org\/ftp\/python\/3.x.x\/Python-3.x.x.tgz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\u5c06\u201c3.x.x\u201d\u66ff\u6362\u4e3a\u6240\u9700\u7684Python\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u538b\u6e90\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u89e3\u538b\u4e0b\u8f7d\u7684\u6e90\u7801\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">tar -xvf Python-3.x.x.tgz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u914d\u7f6e\u548c\u7f16\u8bd1<\/strong><\/li>\n<\/ol>\n<p><p>\u8fdb\u5165\u89e3\u538b\u540e\u7684\u76ee\u5f55\uff0c\u8fd0\u884c\u914d\u7f6e\u811a\u672c\u4ee5\u51c6\u5907\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd Python-3.x.x<\/p>\n<p>.\/configure --enable-optimizations<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8fdb\u884c\u7f16\u8bd1\u548c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">make<\/p>\n<p>sudo make altinstall<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>make altinstall<\/code>\u800c\u4e0d\u662f<code>make install<\/code>\u53ef\u4ee5\u907f\u514d\u8986\u76d6\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u9a8c\u8bc1\u5b89\u88c5<\/strong><\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u5b89\u88c5\u540e\uff0c\u9a8c\u8bc1Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3.x --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u201c3.x\u201d\u66ff\u6362\u4e3a\u5b89\u88c5\u7684Python\u6b21\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pyenv\u8fdb\u884c\u7ba1\u7406<\/p>\n<\/p>\n<p><p>Pyenv\u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u7684\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u5728\u540c\u4e00\u7cfb\u7edf\u4e0a\u8fd0\u884c\u4e0d\u540cPython\u7248\u672c\u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Pyenv<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Pyenv\u7684\u4f9d\u8d56\u9879\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 \\<\/p>\n<p>libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \\<\/p>\n<p>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\u7528Git\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>\u6dfb\u52a0Pyenv\u5230shell\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">echo &#39;export PYENV_ROOT=&quot;$HOME\/.pyenv&quot;&#39; &gt;&gt; ~\/.bashrc<\/p>\n<p>echo &#39;export PATH=&quot;$PYENV_ROOT\/bin:$PATH&quot;&#39; &gt;&gt; ~\/.bashrc<\/p>\n<p>echo &#39;eval &quot;$(pyenv init --path)&quot;&#39; &gt;&gt; ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u91cd\u65b0\u52a0\u8f7dshell\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b89\u88c5Python\u7248\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Pyenv\u5b89\u88c5\u6240\u9700\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv install 3.x.x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u201c3.x.x\u201d\u66ff\u6362\u4e3a\u6240\u9700\u7684Python\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8bbe\u7f6e\u5168\u5c40\u6216\u672c\u5730Python\u7248\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u8bbe\u7f6e\u7cfb\u7edf\u5168\u5c40\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv global 3.x.x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4ec5\u4e3a\u5f53\u524d\u76ee\u5f55\u8bbe\u7f6e\u672c\u5730Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv local 3.x.x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u9a8c\u8bc1\u5b89\u88c5<\/strong><\/li>\n<\/ol>\n<p><p>\u9a8c\u8bc1\u5b89\u88c5\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u786e\u4fdd\u5907\u4efd\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4efb\u4f55\u5347\u7ea7\u64cd\u4f5c\u4e4b\u524d\uff0c\u786e\u4fdd\u5907\u4efd\u91cd\u8981\u6570\u636e\u548c\u914d\u7f6e\u6587\u4ef6\uff0c\u4ee5\u9632\u610f\u5916\u60c5\u51b5\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u517c\u5bb9\u6027\u6d4b\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5347\u7ea7Python\u524d\uff0c\u8fdb\u884c\u517c\u5bb9\u6027\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u548c\u5e93\u4e0e\u65b0\u7248\u672c\u517c\u5bb9\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u73af\u5883\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u901a\u8fc7\u6e90\u7801\u7f16\u8bd1\u6216Pyenv\u5b89\u88c5Python\uff0c\u786e\u4fdd\u6b63\u786e\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u907f\u514d\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Linux\u7cfb\u7edf\u4e0a\u987a\u5229\u5347\u7ea7Python\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u7528\u6237\u5e94\u6839\u636e\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5\u5f53\u524dLinux\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u5f53\u524d\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>python --version<\/code> \u6216 <code>python3 --version<\/code>\u3002\u8fd9\u5c06\u663e\u793a\u5b89\u88c5\u7684Python\u7248\u672c\u4fe1\u606f\u3002\u5982\u679c\u7cfb\u7edf\u4e2d\u540c\u65f6\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>which python<\/code>\u6216<code>which python3<\/code>\u547d\u4ee4\u6765\u786e\u8ba4Python\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Linux\u4e0a\u5347\u7ea7Python\u4f1a\u5f71\u54cd\u7cfb\u7edf\u4e2d\u7684\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u5417\uff1f<\/strong><br \/>\u5347\u7ea7Python\u53ef\u80fd\u4f1a\u5f71\u54cd\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7248\u672cPython\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u8bb8\u591aLinux\u53d1\u884c\u7248\u4f7f\u7528Python\u4f5c\u4e3a\u7cfb\u7edf\u5de5\u5177\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u76f4\u63a5\u5347\u7ea7\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u7248\u672c\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u7cfb\u7edf\u5de5\u5177\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u6216\u5de5\u5177\u5982<code>pyenv<\/code>\u6765\u7ba1\u7406\u4e0d\u540c\u7684Python\u7248\u672c\uff0c\u907f\u514d\u5bf9\u7cfb\u7edf\u7684\u5f71\u54cd\u3002<\/p>\n<p><strong>\u5728Linux\u4e0a\u4f7f\u7528pip\u6765\u5347\u7ea7Python\u5305\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528pip\u5347\u7ea7Python\u5305\u65f6\uff0c\u786e\u4fdd\u60a8\u6b63\u5728\u4f7f\u7528\u6b63\u786e\u7684pip\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip --version<\/code>\u6216<code>pip3 --version<\/code>\u6765\u786e\u8ba4\u3002\u5bf9\u4e8e\u7279\u5b9a\u7684Python\u7248\u672c\uff0c\u5e94\u4f7f\u7528\u5bf9\u5e94\u7684pip\u547d\u4ee4\uff0c\u4f8b\u5982<code>pip3<\/code>\u7528\u4e8ePython 3.x\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pip install --upgrade package_name<\/code>\u547d\u4ee4\u6765\u5347\u7ea7\u6307\u5b9a\u7684\u5305\uff0c\u786e\u4fdd\u5728\u865a\u62df\u73af\u5883\u4e2d\u6267\u884c\uff0c\u4ee5\u907f\u514d\u5f71\u54cd\u5168\u5c40\u73af\u5883\u4e2d\u7684\u5305\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Linux\u7cfb\u7edf\u4e0a\u5347\u7ea7Python\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u3001\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u3001\u4f7f\u7528Python\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u5982Py [&hellip;]","protected":false},"author":3,"featured_media":945115,"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\/945110"}],"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=945110"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945110\/revisions"}],"predecessor-version":[{"id":945117,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945110\/revisions\/945117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/945115"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=945110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=945110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=945110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}