{"id":976603,"date":"2024-12-27T06:22:59","date_gmt":"2024-12-26T22:22:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/976603.html"},"modified":"2024-12-27T06:23:01","modified_gmt":"2024-12-26T22:23:01","slug":"mac-python%e5%a6%82%e4%bd%95%e8%a3%85%e6%88%90%e5%8a%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/976603.html","title":{"rendered":"mac python\u5982\u4f55\u88c5\u6210\u529f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202234\/9434cad4-0325-4f8c-965b-978067c939ab.webp\" alt=\"mac python\u5982\u4f55\u88c5\u6210\u529f\" \/><\/p>\n<p><p> <strong>\u5728macOS\u4e0a\u6210\u529f\u5b89\u88c5Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u4f7f\u7528Homebrew\u5b89\u88c5Python\u3001\u4f7f\u7528pyenv\u7ba1\u7406Python\u7248\u672c\u3001\u5728\u5b89\u88c5\u540e\u9a8c\u8bc1Python\u7248\u672c\u3002<\/strong>\u8fd9\u4e09\u4e2a\u6b65\u9aa4\u786e\u4fdd\u4f60\u5728macOS\u4e0a\u987a\u5229\u5b89\u88c5\u5e76\u8fd0\u884cPython\u3002\u5728\u6b64\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b8c\u6210\u8fd9\u4e09\u6b65\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528HOMEBREW\u5b89\u88c5PYTHON<\/p>\n<\/p>\n<p><p>Homebrew\u662fmacOS\u4e0a\u7684\u4e00\u4e2a\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5\u548c\u7ba1\u7406\u8f6f\u4ef6\u5305\u3002\u4f7f\u7528Homebrew\u5b89\u88c5Python\u662f\u975e\u5e38\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Homebrew<\/strong><br \/>\u9996\u5148\uff0c\u5982\u679c\u4f60\u7684Mac\u4e0a\u8fd8\u6ca1\u6709\u5b89\u88c5Homebrew\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">\/bin\/bash -c &quot;$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/HEAD\/install.sh)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4e0b\u8f7d\u5e76\u5b89\u88c5Homebrew\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>brew -v<\/code>\u6765\u9a8c\u8bc1Homebrew\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Homebrew\u5b89\u88c5Python<\/strong><br \/>\u4e00\u65e6Homebrew\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5Python\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">brew install python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b89\u88c5Python\u7684\u6700\u65b0\u7248\u672c\uff0c\u540c\u65f6\u4e5f\u4f1a\u5b89\u88c5<code>pip<\/code>\uff0cPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>python3 --version<\/code>\u6765\u9a8c\u8bc1Python\u7684\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><br \/>\u5728\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4ee5\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4f7f\u7528\u6b63\u786e\u7684Python\u7248\u672c\u3002\u4f60\u53ef\u4ee5\u7f16\u8f91<code>.bash_profile<\/code>\u6216<code>.zshrc<\/code>\u6587\u4ef6\uff08\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684shell\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=&quot;\/usr\/local\/opt\/python\/libexec\/bin:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c<code>source ~\/.bash_profile<\/code>\u6216<code>source ~\/.zshrc<\/code>\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528PYENV\u7ba1\u7406PYTHON\u7248\u672c<\/p>\n<\/p>\n<p><p>Pyenv\u662f\u4e00\u4e2aPython\u7248\u672c\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u540c\u4e00\u7cfb\u7edf\u4e0a\u5b89\u88c5\u548c\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Pyenv<\/strong><br \/>\u4f7f\u7528Homebrew\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5Pyenv\u3002\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">brew install pyenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684shell\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u51e0\u884c\uff0c\u4ee5\u542f\u7528pyenv\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">echo &#39;export PYENV_ROOT=&quot;$HOME\/.pyenv&quot;&#39; &gt;&gt; ~\/.bash_profile<\/p>\n<p>echo &#39;export PATH=&quot;$PYENV_ROOT\/bin:$PATH&quot;&#39; &gt;&gt; ~\/.bash_profile<\/p>\n<p>echo &#39;eval &quot;$(pyenv init --path)&quot;&#39; &gt;&gt; ~\/.bash_profile<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884c<code>source ~\/.bash_profile<\/code>\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u7ba1\u7406Python\u7248\u672c<\/strong><br \/>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pyenv\u6765\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684Python\u3002\u4f8b\u5982\uff0c\u5b89\u88c5Python 3.9.0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv install 3.9.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u8bbe\u7f6e\u5168\u5c40\u6216\u5c40\u90e8Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv global 3.9.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv local 3.9.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8bbe\u7f6ePython\u7684\u5168\u5c40\u6216\u5f53\u524d\u76ee\u5f55\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5728\u5b89\u88c5\u540e\u9a8c\u8bc1PYTHON\u7248\u672c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9a8c\u8bc1Python\u7248\u672c<\/strong><br \/>\u65e0\u8bba\u4f60\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u5b89\u88c5Python\uff0c\u6700\u540e\u4e00\u6b65\u90fd\u662f\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770bPython\u7684\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u663e\u793a\u5f53\u524d\u4f7f\u7528\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1pip\u5b89\u88c5<\/strong><br \/>\u540c\u6837\u91cd\u8981\u7684\u662f\u9a8c\u8bc1pip\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5pip\u7684\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fddpip\u5de5\u4f5c\u6b63\u5e38\uff0c\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728macOS\u4e0a\u6210\u529f\u5b89\u88c5\u5e76\u914d\u7f6ePython\u3002\u4f7f\u7528Homebrew\u548cpyenv\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u7ba1\u7406Python\u73af\u5883\uff0c\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4fdd\u6301\u6700\u65b0\u548c\u4e00\u81f4\u3002\u65e0\u8bba\u662f\u5f00\u53d1\u65b0\u7684\u9879\u76ee\u8fd8\u662f\u7ef4\u62a4\u73b0\u6709\u7684\u4ee3\u7801\uff0c\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u90fd\u5c06\u4f7f\u4f60\u7684\u5de5\u4f5c\u66f4\u52a0\u9ad8\u6548\u548c\u987a\u7545\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Mac\u4e0a\u5b89\u88c5Python\uff1f<\/strong><br \/>\u5728Mac\u4e0a\u5b89\u88c5Python\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08python.org\uff09\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684\u5b89\u88c5\u5305\uff0c\u6216\u8005\u4f7f\u7528Homebrew\u5305\u7ba1\u7406\u5de5\u5177\u6765\u5b89\u88c5\u3002\u82e5\u9009\u62e9\u4f7f\u7528Homebrew\uff0c\u60a8\u53ea\u9700\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>brew install python<\/code>\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5b89\u88c5\u540e\u901a\u8fc7\u5728\u7ec8\u7aef\u8f93\u5165<code>python3 --version<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<p><strong>\u5728Mac\u4e0a\u5b89\u88c5\u7684Python\u4e0e\u7cfb\u7edf\u81ea\u5e26\u7684Python\u6709\u4f55\u4e0d\u540c\uff1f<\/strong><br \/>Mac\u7cfb\u7edf\u901a\u5e38\u9884\u88c5\u6709Python 2.x\u7248\u672c\uff0c\u800c\u8bb8\u591a\u73b0\u4ee3\u5f00\u53d1\u9700\u6c42\u5219\u9700\u8981Python 3.x\u7248\u672c\u3002\u56e0\u6b64\uff0c\u5b89\u88c5\u65b0\u7248\u672c\u7684Python\u53ef\u4ee5\u786e\u4fdd\u60a8\u4f7f\u7528\u7684\u662f\u6700\u65b0\u7684\u529f\u80fd\u4e0e\u5e93\u3002\u6b64\u5916\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4e0d\u540c\u9879\u76ee\u6240\u9700\u7684Python\u7248\u672c\u548c\u4f9d\u8d56\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0e\u7cfb\u7edf\u81ea\u5e26\u7248\u672c\u4ea7\u751f\u51b2\u7a81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Mac\u4e0a\u8bbe\u7f6ePython\u5f00\u53d1\u73af\u5883\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u5b89\u88c5\u4e86Python\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u4e00\u4e9b\u5e38\u7528\u7684\u5f00\u53d1\u5de5\u5177\u6765\u8bbe\u7f6e\u73af\u5883\u3002\u63a8\u8350\u4f7f\u7528<code>pip<\/code>\u6765\u7ba1\u7406Python\u5e93\uff0c\u8fd0\u884c<code>pip install package_name<\/code>\u53ef\u5b89\u88c5\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216Visual Studio Code\uff0c\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u529f\u80fd\u548c\u7248\u672c\u63a7\u5236\u7b49\u4e30\u5bcc\u529f\u80fd\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728macOS\u4e0a\u6210\u529f\u5b89\u88c5Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u4f7f\u7528Homebrew\u5b89\u88c5Python\u3001\u4f7f\u7528pyenv\u7ba1 [&hellip;]","protected":false},"author":3,"featured_media":976613,"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\/976603"}],"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=976603"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976603\/revisions"}],"predecessor-version":[{"id":976619,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976603\/revisions\/976619"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/976613"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=976603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=976603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=976603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}