{"id":52285,"date":"2024-10-09T01:21:13","date_gmt":"2024-10-08T17:21:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/52285\/"},"modified":"2024-11-14T10:39:35","modified_gmt":"2024-11-14T02:39:35","slug":"ubuntu-%e4%b8%8b%e6%9b%b4%e6%96%b0-pip-%e5%be%8c%e5%a0%b1-importerror-cannot-import-name-main-%e9%8c%af%e8%aa%a4%e8%a7%a3%e6%b1%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/52285\/","title":{"rendered":"Ubuntu \u4e0b\u66f4\u65b0 pip \u5f8c\u5831 ImportError: cannot import name &#8216;main&#8217; \u932f\u8aa4\u89e3\u6c7a"},"content":{"rendered":"<h1 id=\"ubuntu-%e4%b8%8b%e6%9b%b4%e6%96%b0-pip-%e5%be%8c%e5%a0%b1-importerror-cannot-import-name-main-%e9%8c%af%e8%aa%a4%e8%a7%a3%e6%b1%ba-RpMJObitns\">Ubuntu \u4e0b\u66f4\u65b0 pip \u5f8c\u5831 ImportError: cannot import name &#8216;main&#8217; \u932f\u8aa4\u89e3\u6c7a<\/h1>\n<p>\u5728\u4f7f\u7528 Python \u7684\u904e\u7a0b\u4e2d\uff0cpip \u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u65bc\u5b89\u88dd\u548c\u7ba1\u7406 Python \u5305\u3002\u7136\u800c\uff0c\u8a31\u591a\u7528\u6236\u5728\u66f4\u65b0 pip \u5f8c\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff1aImportError: cannot import name &#8216;main&#8217;\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u500b\u554f\u984c\u7684\u539f\u56e0\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\uff0c\u5e6b\u52a9\u7528\u6236\u9806\u5229\u4f7f\u7528 pip\u3002<\/p>\n<h2 id=\"%e5%95%8f%e9%a1%8c%e8%83%8c%e6%99%af-RpMJObitns\">\u554f\u984c\u80cc\u666f<\/h2>\n<p>\u5728 Ubuntu \u7cfb\u7d71\u4e2d\uff0cpip \u662f Python \u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u901a\u5e38\u7528\u65bc\u5b89\u88dd\u7b2c\u4e09\u65b9\u5eab\u548c\u6a21\u7d44\u3002\u96a8\u8457 Python \u548c pip \u7684\u7248\u672c\u4e0d\u65b7\u66f4\u65b0\uff0c\u67d0\u4e9b\u820a\u7684\u7528\u6cd5\u53ef\u80fd\u6703\u88ab\u6dd8\u6c70\uff0c\u5c0e\u81f4\u7528\u6236\u5728\u57f7\u884c pip \u547d\u4ee4\u6642\u51fa\u73fe\u932f\u8aa4\u3002\u7279\u5225\u662f\u5728\u66f4\u65b0 pip \u4e4b\u5f8c\uff0c\u4f7f\u7528\u8005\u53ef\u80fd\u6703\u767c\u73fe\u7121\u6cd5\u6b63\u5e38\u5c0e\u5165 pip \u7684\u67d0\u4e9b\u529f\u80fd\uff0c\u5f9e\u800c\u51fa\u73fe ImportError\u3002<\/p>\n<h2 id=\"%e9%8c%af%e8%aa%a4%e5%8e%9f%e5%9b%a0-RpMJObitns\">\u932f\u8aa4\u539f\u56e0<\/h2>\n<p>\u9019\u500b\u932f\u8aa4\u7684\u4e3b\u8981\u539f\u56e0\u662f pip \u7684\u5167\u90e8\u7d50\u69cb\u5728\u4e0d\u540c\u7248\u672c\u4e4b\u9593\u767c\u751f\u4e86\u8b8a\u5316\u3002\u5728\u8f03\u820a\u7684\u7248\u672c\u4e2d\uff0cpip \u7684\u4e3b\u51fd\u6578\u662f\u901a\u904e\u5f9e pip \u6a21\u7d44\u4e2d\u5c0e\u5165 main \u51fd\u6578\u4f86\u57f7\u884c\u7684\u3002\u7136\u800c\uff0c\u5728\u66f4\u65b0\u5230\u8f03\u65b0\u7684\u7248\u672c\u5f8c\uff0c\u9019\u7a2e\u5c0e\u5165\u65b9\u5f0f\u5df2\u7d93\u88ab\u79fb\u9664\uff0c\u5c0e\u81f4\u7528\u6236\u5728\u57f7\u884c pip \u547d\u4ee4\u6642\u51fa\u73fe ImportError\u3002<\/p>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-RpMJObitns\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\u64cd\u4f5c\uff1a<\/p>\n<h3 id=\"1-%e7%a2%ba%e8%aa%8d-python-%e5%92%8c-pip-%e7%9a%84%e7%89%88%e6%9c%ac-RpMJObitns\">1. \u78ba\u8a8d Python \u548c pip \u7684\u7248\u672c<\/h3>\n<pre><code>python --version\npip --version\n<\/code><\/pre>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u78ba\u8a8d\u60a8\u7576\u524d\u4f7f\u7528\u7684 Python \u548c pip \u7684\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u547d\u4ee4\u4f86\u6aa2\u67e5\u7248\u672c\u865f\u3002<\/p>\n<h3 id=\"2-%e6%9b%b4%e6%96%b0-pip-RpMJObitns\">2. \u66f4\u65b0 pip<\/h3>\n<p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u8f03\u820a\u7248\u672c\u7684 pip\uff0c\u5efa\u8b70\u60a8\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u66f4\u65b0\uff1a<\/p>\n<pre><code>python -m pip install --upgrade pip\n<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u6703\u4f7f\u7528 Python \u7684\u6a21\u7d44\u65b9\u5f0f\u4f86\u66f4\u65b0 pip\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d ImportError \u7684\u554f\u984c\u3002<\/p>\n<h3 id=\"3-%e6%aa%a2%e6%9f%a5%e7%92%b0%e5%a2%83%e8%ae%8a%e6%95%b8-RpMJObitns\">3. \u6aa2\u67e5\u74b0\u5883\u8b8a\u6578<\/h3>\n<p>\u6709\u6642\u5019\uff0c\u74b0\u5883\u8b8a\u6578\u7684\u8a2d\u7f6e\u4e5f\u53ef\u80fd\u5c0e\u81f4 pip \u51fa\u73fe\u554f\u984c\u3002\u78ba\u4fdd\u60a8\u7684 PATH \u74b0\u5883\u8b8a\u6578\u4e2d\u5305\u542b\u4e86\u6b63\u78ba\u7684 Python \u548c pip \u8def\u5f91\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u6aa2\u67e5\uff1a<\/p>\n<pre><code>echo $PATH\n<\/code><\/pre>\n<p>\u5982\u679c\u767c\u73fe\u8def\u5f91\u4e0d\u6b63\u78ba\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u7de8\u8f2f ~\/.bashrc \u6216 ~\/.bash_profile \u6587\u4ef6\u4f86\u9032\u884c\u4fee\u6b63\u3002<\/p>\n<h3 id=\"4-%e9%87%8d%e6%96%b0%e5%ae%89%e8%a3%9d-pip-RpMJObitns\">4. \u91cd\u65b0\u5b89\u88dd pip<\/h3>\n<p>\u5982\u679c\u4ee5\u4e0a\u65b9\u6cd5\u7121\u6cd5\u89e3\u6c7a\u554f\u984c\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u91cd\u65b0\u5b89\u88dd pip\u3002\u9996\u5148\uff0c\u5378\u8f09\u7576\u524d\u7684 pip\uff1a<\/p>\n<pre><code>python -m pip uninstall pip\n<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u5b89\u88dd pip\uff1a<\/p>\n<pre><code>curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py\npython get-pip.py\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-RpMJObitns\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Ubuntu \u7cfb\u7d71\u4e2d\uff0c\u66f4\u65b0 pip \u5f8c\u51fa\u73fe ImportError: cannot import name &#8216;main&#8217; \u7684\u554f\u984c\uff0c\u4e3b\u8981\u662f\u7531\u65bc pip \u7684\u5167\u90e8\u7d50\u69cb\u8b8a\u5316\u6240\u81f4\u3002\u901a\u904e\u78ba\u8a8d\u7248\u672c\u3001\u66f4\u65b0 pip\u3001\u6aa2\u67e5\u74b0\u5883\u8b8a\u6578\u4ee5\u53ca\u91cd\u65b0\u5b89\u88dd pip\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u9019\u4e00\u554f\u984c\u3002\u9019\u6a23\uff0c\u60a8\u5c31\u80fd\u5920\u9806\u5229\u4f7f\u7528 pip \u4f86\u7ba1\u7406\u60a8\u7684 Python \u5305\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u670d\u52d9\u4f86\u652f\u6301\u60a8\u7684\u958b\u767c\u5de5\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7a Ubuntu \u66f4\u65b0 pip \u5f8c\u51fa\u73fe\u7684 ImportError: cannot import name &#8216;main&#8217; \u932f\u8aa4\uff0c\u63d0\u4f9b\u6709\u6548\u7684\u6b65\u9a5f\u8207\u89e3\u6c7a\u65b9\u6848\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-52285","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52285","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=52285"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52285\/revisions"}],"predecessor-version":[{"id":52286,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52285\/revisions\/52286"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=52285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=52285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=52285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}