{"id":177061,"date":"2024-05-08T19:22:58","date_gmt":"2024-05-08T11:22:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177061.html"},"modified":"2024-05-08T19:23:03","modified_gmt":"2024-05-08T11:23:03","slug":"python%e5%8d%87%e7%ba%a7%e5%90%8e%ef%bc%8ccentos-%e4%b8%8a%e7%9a%84yum%e4%b8%8d%e8%83%bd%e7%94%a8%e4%ba%86%ef%bc%8c%e6%80%8e%e4%b9%88%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177061.html","title":{"rendered":"python\u5347\u7ea7\u540e\uff0ccentOS \u4e0a\u7684yum\u4e0d\u80fd\u7528\u4e86\uff0c\u600e\u4e48\u529e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060916\/3ce19761-79bd-4747-93ad-a7106c968fb5.webp\" alt=\"python\u5347\u7ea7\u540e\uff0ccentOS \u4e0a\u7684yum\u4e0d\u80fd\u7528\u4e86\uff0c\u600e\u4e48\u529e\" \/><\/p>\n<p><p>\u89e3\u51b3\u5728CentOS\u4e0a\u56e0Python\u5347\u7ea7\u540e\u5bfc\u81f4yum\u4e0d\u53ef\u7528\u7684\u60c5\u51b5\uff0c\u4e3b\u8981\u624b\u6bb5\u5305\u62ec<strong>\u91cd\u65b0\u6307\u5b9aPython\u7248\u672c\u3001\u4fee\u590dyum\u914d\u7f6e\u6587\u4ef6\u3001\u5b89\u88c5\u517c\u5bb9\u7684Python\u7248\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong>\u3002\u6700\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u662f<strong>\u91cd\u65b0\u6307\u5b9aPython\u7248\u672c<\/strong>\u3002CentOS\u7cfb\u7edf\u4e2dyum\u4f9d\u8d56\u4e8ePython 2.x\u7248\u672c\u6765\u8fd0\u884c\u3002\u5f53\u7528\u6237\u5347\u7ea7\u5230Python 3.x\u540e\uff0cyum\u4f1a\u56e0\u7248\u672c\u4e0d\u517c\u5bb9\u800c\u4ea7\u751f\u6545\u969c\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efaPython\u7684\u8f6f\u94fe\u63a5\u6307\u5411\u539f\u6765\u7684Python 2.x\u7248\u672c\u6216\u4fee\u590dyum\u7684Python\u73af\u5883\u8def\u5f84\u6765\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u91cd\u65b0\u6307\u5b9aPython\u7248\u672c<\/h2>\n<\/p>\n<p><p>\u91cd\u6307\u5b9aPython\u7248\u672c\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u8fd9\u6d89\u53ca\u5230\u4fee\u6539\u7cfb\u7edf\u8f6f\u94fe\u63a5\uff0c\u4f7f\u5f97<code>\/usr\/bin\/python<\/code>\u6307\u5411yum\u517c\u5bb9\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u8ba4\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\u53ca\u8def\u5f84\u3002\u4f7f\u7528\u547d\u4ee4<code>ls -l \/usr\/bin\/python*<\/code>\u67e5\u770b\u3002\u5982\u679c\u7cfb\u7edf\u4e2d\u540c\u65f6\u5b89\u88c5\u4e86Python 2.x\u548cPython 3.x\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u66f4\u65b0\u8f6f\u94fe\u63a5\u8ba9<code>\/usr\/bin\/python<\/code>\u6307\u5411Python 2.x\u7248\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">sudo ln -sfn \/usr\/bin\/python2.7 \/usr\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4e0a\u8ff0\u547d\u4ee4\u540e\uff0c\u901a\u8fc7<code>python --version<\/code>\u547d\u4ee4\u786e\u8ba4\u8f6f\u94fe\u63a5\u66f4\u65b0\u6210\u529f\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u4ee5\u8ba9\u7cfb\u7edf\u7684\u9ed8\u8ba4Python\u7248\u672c\u56de\u9000\u5230yum\u652f\u6301\u7684\u7248\u672c\uff0c\u8fdb\u800c\u8ba9yum\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4fee\u590dyum\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4ec5\u4ec5\u662f\u4fee\u6539\u8f6f\u94fe\u63a5\u4ecd\u7136\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u53ef\u80fd\u662fyum\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u786c\u7f16\u7801\u4e86Python\u8def\u5f84\u3002\u8fd9\u65f6\uff0c\u9700\u8981\u624b\u52a8\u7f16\u8f91\u8fd9\u4e9b\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u67e5\u627e\u542b\u6709<code>#!\/usr\/bin\/python<\/code>\u7684yum\u76f8\u5173\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">grep -rlI &#039;#!\/usr\/bin\/python&#039; \/usr\/bin\/yum*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6839\u636e\u7cfb\u7edf\u4e2dPython 2.x\u7684\u786e\u5207\u8def\u5f84\uff0c\u5c06\u8fd9\u4e9b\u6587\u4ef6\u9876\u90e8\u7684\u89e3\u91ca\u5668\u8def\u5f84\u4fee\u6539\u4e3a\u6b63\u786e\u7684Python 2.x\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5982\u679cPython 2.7\u4f4d\u4e8e<code>\/usr\/bin\/python2.7<\/code>\uff0c\u5219\u5c06<code>#!\/usr\/bin\/python<\/code>\u6539\u4e3a<code>#!\/usr\/bin\/python2.7<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b89\u88c5\u517c\u5bb9\u7684Python\u7248\u672c<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u7cfb\u7edf\u4e0d\u5141\u8bb8\u4fee\u6539\u65e7\u7684Python\u7248\u672c\u94fe\u63a5\uff0c\u6216\u8005\u51fa\u4e8e\u67d0\u4e9b\u539f\u56e0\u9700\u8981\u4fdd\u6301\u7cfb\u7edfPython 3.x\u4f5c\u4e3a\u9ed8\u8ba4\u7248\u672c\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684Python 2.x\u7248\u672c\uff0c\u4e13\u95e8\u7528\u4e8eyum\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8f7dPython 2.x\u7684\u6e90\u4ee3\u7801\uff0c\u7136\u540e\u7f16\u8bd1\u5b89\u88c5\u5230\u4e0d\u540c\u7684\u76ee\u5f55\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">wget https:\/\/www.python.org\/ftp\/python\/2.7.xx\/Python-2.7.xx.tgz<\/p>\n<p>tar xvf Python-2.7.xx.tgz<\/p>\n<p>cd Python-2.7.xx<\/p>\n<p>.\/configure --prefix=\/usr\/local<\/p>\n<p>make &amp;&amp; make altinstall<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u786e\u4fddyum\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684Python\u8def\u5f84\u6307\u5411\u65b0\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u4fee\u6539\u7cfb\u7edf\u9ed8\u8ba4\u7684Python\u7248\u672c\uff0c\u53e6\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528Python\u7684\u865a\u62df\u73af\u5883\u3002\u8fd9\u610f\u5473\u7740\u4e3ayum\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684Python 2.x\u73af\u5883\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5230\u7cfb\u7edf\u4e2d\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>virtualenv<\/code>\u5de5\u5177\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install virtualenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u9002\u5f53\u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\u5e76\u6fc0\u6d3b\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">virtualenv -p \/usr\/bin\/python2.7 yum-env<\/p>\n<p>source yum-env\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u865a\u62df\u73af\u5883\u4e2d\uff0cyum\u5c06\u4f7f\u7528\u6307\u5b9a\u7684Python\u7248\u672c\u8fd0\u884c\uff0c\u800c\u4e0d\u4f1a\u53d7\u5230\u7cfb\u7edfPython\u7248\u672c\u5347\u7ea7\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53<strong>CentOS\u4e0a\u7684yum\u56e0Python\u5347\u7ea7\u540e\u51fa\u73b0\u95ee\u9898<\/strong>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u91cd\u65b0\u6307\u5b9aPython\u7248\u672c\u3001\u4fee\u590dyum\u914d\u7f6e\u6587\u4ef6\u3001\u5b89\u88c5\u517c\u5bb9\u7684Python\u7248\u672c\u6216\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong>\u7b49\u65b9\u6cd5\u6765\u89e3\u51b3\u3002\u8fd9\u4e9b\u65b9\u6cd5\u65e2\u53ef\u4ee5\u6062\u590dyum\u7684\u529f\u80fd\uff0c\u4e5f\u5141\u8bb8\u7528\u6237\u5728\u7cfb\u7edf\u4e2d\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684Python\uff0c\u786e\u4fdd\u7cfb\u7edf\u7684\u7075\u6d3b\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5728Python\u5347\u7ea7\u540e\uff0c\u4e3a\u4ec0\u4e48\u6211\u65e0\u6cd5\u4f7f\u7528CentOS\u4e0a\u7684yum\u5de5\u5177\uff1f<\/strong><\/p>\n<p>A1: \u5f53\u60a8\u5347\u7ea7Python\u540e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4CentOS\u4e0a\u7684yum\u5de5\u5177\u65e0\u6cd5\u4f7f\u7528\u7684\u539f\u56e0\u662f\uff0cyum\u5de5\u5177\u662fCentOS\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5305\u7ba1\u7406\u5668\uff0c\u5b83\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u6240\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u5982\u679c\u60a8\u5347\u7ea7\u4e86Python\u7248\u672c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4yum\u627e\u4e0d\u5230\u6216\u65e0\u6cd5\u4e0e\u65b0\u7248\u672c\u7684Python\u517c\u5bb9\u3002<\/p>\n<p><strong>Q2: \u6211\u5728CentOS\u4e0a\u5347\u7ea7\u4e86Python\u540e\uff0c\u5982\u4f55\u89e3\u51b3yum\u65e0\u6cd5\u4f7f\u7528\u7684\u95ee\u9898\uff1f<\/strong><\/p>\n<p>A2: \u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3CentOS\u4e0ayum\u65e0\u6cd5\u4f7f\u7528\u7684\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python\u7684\u6b63\u786e\u7248\u672c\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528<code>python -V<\/code>\u547d\u4ee4\u68c0\u67e5\u5f53\u524dPython\u7248\u672c\uff0c\u5e76\u786e\u4fdd\u4e0eyum\u517c\u5bb9\u7684\u7248\u672c\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\u68c0\u67e5yum\u7684\u914d\u7f6e\u6587\u4ef6\u662f\u5426\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91<code>\/etc\/yum.conf<\/code>\u6587\u4ef6\u6216<code>\/etc\/yum.repos.d<\/code>\u76ee\u5f55\u4e0b\u7684\u76f8\u5173\u6587\u4ef6\u6765\u68c0\u67e5\u662f\u5426\u6709\u4efb\u4f55Python\u7248\u672c\u76f8\u5173\u7684\u9519\u8bef\u6216\u51b2\u7a81\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\u5982\u679c\u60a8\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8003\u8651\u91cd\u65b0\u5b89\u88c5CentOS\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5e76\u5728\u5b89\u88c5Python\u4e4b\u524d\u5907\u4efd\u91cd\u8981\u7684\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u914d\u7f6e\u4e0d\u4f1a\u53d7\u5230Python\u5347\u7ea7\u7684\u5e72\u6270\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>Q3: \u5982\u4f55\u5728CentOS\u4e0a\u5347\u7ea7Python\u800c\u4e0d\u5f71\u54cdyum\u7684\u4f7f\u7528\uff1f<\/strong><\/p>\n<p>A3: \u5728CentOS\u4e0a\u5347\u7ea7Python\u65f6\uff0c\u6700\u597d\u91c7\u53d6\u4e00\u4e9b\u63aa\u65bd\u4ee5\u786e\u4fddyum\u4ecd\u7136\u53ef\u7528\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>\u5728\u5347\u7ea7Python\u4e4b\u524d\u5907\u4efd\u91cd\u8981\u7684\u7cfb\u7edf\u6587\u4ef6\u548c\u6570\u636e\u3002\u8fd9\u5c06\u6709\u52a9\u4e8e\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u914d\u7f6e\u95ee\u9898\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u865a\u62df\u73af\u5883\u8fdb\u884cPython\u5347\u7ea7\u3002\u901a\u8fc7\u4f7f\u7528\u5de5\u5177\u5982virtualenv\uff0c\u60a8\u53ef\u4ee5\u5728\u72ec\u7acb\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u65b0\u7248\u7684Python\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684\u5176\u4ed6\u90e8\u5206\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\u5728\u5347\u7ea7\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u4e0eyum\u6709\u5173\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u4e0d\u4f1a\u88ab\u66f4\u6539\u6216\u5220\u9664\u3002\u7279\u522b\u662f<code>\/usr\/bin\/python<\/code>\u548c<code>\/usr\/lib\/pythonX.X\/site-packages<\/code>\u7b49\u6587\u4ef6\u548c\u76ee\u5f55\u662f\u4e0eyum\u5de5\u5177\u76f8\u5173\u8054\u7684\uff0c\u907f\u514d\u5bf9\u5b83\u4eec\u8fdb\u884c\u8bef\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u4efb\u4f55\u7cfb\u7edf\u914d\u7f6e\u7684\u66f4\u6539\uff0c\u7279\u522b\u662f\u6d89\u53ca\u5230\u5305\u7ba1\u7406\u5668\u7684\u66f4\u6539\uff0c\u90fd\u5e94\u5c0f\u5fc3\u64cd\u4f5c\uff0c\u5e76\u5728\u8fdb\u884c\u91cd\u5927\u66f4\u6539\u4e4b\u524d\u5907\u4efd\u60a8\u7684\u6570\u636e\u548c\u7cfb\u7edf\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u89e3\u51b3\u5728CentOS\u4e0a\u56e0Python\u5347\u7ea7\u540e\u5bfc\u81f4yum\u4e0d\u53ef\u7528\u7684\u60c5\u51b5\uff0c\u4e3b\u8981\u624b\u6bb5\u5305\u62ec\u91cd\u65b0\u6307\u5b9aPython\u7248\u672c\u3001\u4fee\u590dyu [&hellip;]","protected":false},"author":3,"featured_media":177068,"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\/177061"}],"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=177061"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177061\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177068"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}