{"id":945896,"date":"2024-12-26T23:30:19","date_gmt":"2024-12-26T15:30:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/945896.html"},"modified":"2024-12-26T23:30:21","modified_gmt":"2024-12-26T15:30:21","slug":"python%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8cmac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/945896.html","title":{"rendered":"python\u5982\u4f55\u8fd0\u884cmac"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082305\/695dea5f-6221-4512-9a6d-3208c3b82e6e.webp\" alt=\"python\u5982\u4f55\u8fd0\u884cmac\" \/><\/p>\n<p><p> <strong>\u5728Mac\u4e0a\u8fd0\u884cPython\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684\u7ec8\u7aef\u3001\u5b89\u88c5Python\u7684IDE\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Mac\u4e0a\u8fd0\u884cPython\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5728Mac\u4e0a\u8fd0\u884cPython\u7684\u57fa\u7840\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Mac\u7cfb\u7edf\u81ea\u5e26Python\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u8f7b\u677e\u8fd0\u884cPython\u7a0b\u5e8f\u3002\u6253\u5f00\u7ec8\u7aef\u540e\uff0c\u8f93\u5165<code>python3<\/code>\u547d\u4ee4\u5373\u53ef\u542f\u52a8Python 3\u7684\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u3002\u8981\u8fd0\u884c\u4e00\u4e2aPython\u811a\u672c\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python3 script.py<\/code>\uff0c\u5176\u4e2d<code>script.py<\/code>\u662f\u4f60\u7684Python\u6587\u4ef6\u540d\u3002<strong>\u786e\u4fdd\u4f7f\u7528Python 3\uff0c\u56e0\u4e3aPython 2\u5df2\u88ab\u6dd8\u6c70<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Python IDE<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u7ec8\u7aef\uff0cMac\u7528\u6237\u8fd8\u53ef\u4ee5\u5b89\u88c5Python\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6765\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801\u3002\u5e38\u89c1\u7684Python IDE\u5305\u62ecPyCharm\u3001VSCode\u548cAnaconda\u3002\u4ee5PyCharm\u4e3a\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u5728JetBr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u5b89\u88c5\u5b8c\u6210\u540e\u901a\u8fc7\u8be5\u8f6f\u4ef6\u521b\u5efa\u548c\u8fd0\u884cPython\u9879\u76ee\u3002IDE\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u4ee3\u7801\u7f16\u8f91\u3001\u8c03\u8bd5\u548c\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u548c\u7ba1\u7406Python\u7248\u672c<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c\uff0c\u7279\u522b\u662f\u5728\u5f00\u53d1\u548c\u90e8\u7f72\u4e0d\u540c\u7684\u9879\u76ee\u65f6\u3002\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Homebrew\u6765\u7ba1\u7406Python\u7248\u672c\u3002\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>brew install python<\/code>\u6765\u5b89\u88c5Python 3\u3002\u5982\u679c\u9700\u8981\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyenv<\/code>\u3002\u901a\u8fc7<code>pyenv install &lt;version&gt;<\/code>\u547d\u4ee4\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684Python\uff0c\u5e76\u4f7f\u7528<code>pyenv global &lt;version&gt;<\/code>\u6216<code>pyenv local &lt;version&gt;<\/code>\u6765\u5207\u6362\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u9879\u76ee\u65f6\uff0c\u4e3a\u4e86\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u4f9d\u8d56\u51b2\u7a81\uff0c\u63a8\u8350\u4f7f\u7528\u865a\u62df\u73af\u5883\u3002\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u9996\u5148\uff0c\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c<code>python3 -m venv env<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>source env\/bin\/activate<\/code>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u3002\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u7684\u5e93\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u548cPATH<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u7ec8\u7aef\u4e2d\u65b9\u4fbf\u5730\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u7528\u6237\u9700\u8981\u786e\u4fddPython\u7684\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u53ef\u4ee5\u7f16\u8f91<code>.bash_profile<\/code>\u6216<code>.zshrc<\/code>\u6587\u4ef6\uff0c\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0<code>export PATH=&quot;\/usr\/local\/opt\/python\/libexec\/bin:$PATH&quot;<\/code>\u3002\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u8fd0\u884c<code>source ~\/.bash_profile<\/code>\u6216<code>source ~\/.zshrc<\/code>\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Python\u5305\u7ba1\u7406\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u5f00\u53d1Python\u9879\u76ee\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002<code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u8fd0\u884c<code>pip install package_name<\/code>\u6765\u5b89\u88c5\u9700\u8981\u7684\u5e93\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\uff0c\u786e\u4fdd\u5728\u6fc0\u6d3b\u73af\u5883\u540e\u5b89\u88c5\u5e93\uff0c\u4ee5\u907f\u514d\u5168\u5c40\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Docker\u8fd0\u884cPython<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8de8\u5e73\u53f0\u5f00\u53d1\u548c\u90e8\u7f72\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u4f7f\u7528Docker\u6765\u8fd0\u884cPython\u3002\u9996\u5148\uff0c\u5728Mac\u4e0a\u5b89\u88c5Docker Desktop\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a<code>Dockerfile<\/code>\uff0c\u5b9a\u4e49Python\u73af\u5883\u548c\u6240\u9700\u7684\u4f9d\u8d56\u3002\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c<code>docker build -t my-python-app .<\/code>\u6784\u5efa\u955c\u50cf\uff0c\u518d\u901a\u8fc7<code>docker run my-python-app<\/code>\u542f\u52a8\u5bb9\u5668\u3002Docker\u63d0\u4f9b\u4e86\u4e00\u79cd\u9694\u79bb\u7684\u73af\u5883\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u673a\u5668\u4e0a\u8fd0\u884c\u4e00\u81f4\u7684Python\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p>\u7ba1\u7406Python\u9879\u76ee\u7684\u4f9d\u8d56<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684Python\u9879\u76ee\uff0c\u63a8\u8350\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6216<code>Pipfile<\/code>\u6765\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c<code>pip freeze &gt; requirements.txt<\/code>\u5c06\u5f53\u524d\u73af\u5883\u7684\u4f9d\u8d56\u5bfc\u51fa\u4e3a<code>requirements.txt<\/code>\u6587\u4ef6\u3002\u5728\u5176\u4ed6\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install -r requirements.txt<\/code>\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u3002\u4f7f\u7528<code>Pipenv<\/code>\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7b80\u5316\u4f9d\u8d56\u7ba1\u7406\uff0c\u5e76\u4e0e\u865a\u62df\u73af\u5883\u96c6\u6210\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8c03\u8bd5\u548c\u4f18\u5316Python\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u8fd0\u884cPython\u65f6\uff0c\u8c03\u8bd5\u662f\u786e\u4fdd\u4ee3\u7801\u6b63\u786e\u6027\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4f7f\u7528IDE\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u3001\u67e5\u770b\u53d8\u91cf\u503c\u548c\u8ddf\u8e2a\u7a0b\u5e8f\u6267\u884c\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pdb<\/code>\u6a21\u5757\u8fdb\u884c\u547d\u4ee4\u884c\u8c03\u8bd5\u3002\u5bf9\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u5efa\u8bae\u4f7f\u7528<code>cProfile<\/code>\u6a21\u5757\u5206\u6790\u4ee3\u7801\u6027\u80fd\uff0c\u5e76\u6839\u636e\u5206\u6790\u7ed3\u679c\u4f18\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u5b66\u4e60\u548c\u63d0\u9ad8Python\u6280\u80fd<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5728Mac\u4e0a\u8fd0\u884cPython\uff0c\u5b66\u4e60\u548c\u63d0\u9ad8Python\u7f16\u7a0b\u6280\u80fd\u540c\u6837\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u8bfe\u7a0b\u3001\u9605\u8bfb\u4e66\u7c4d\u548c\u53c2\u4e0e\u5f00\u6e90\u9879\u76ee\u6765\u63d0\u5347\u6280\u80fd\u3002\u63a8\u8350\u7684\u5b66\u4e60\u8d44\u6e90\u5305\u62ecCodecademy\u3001Coursera\u3001\u4ee5\u53ca\u300aPython\u7f16\u7a0b\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\uff0c\u7528\u6237\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u7406\u89e3Python\u8bed\u8a00\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Mac\u4e0a\u8fd0\u884cPython\u6d89\u53ca\u591a\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5b89\u88c5\u548c\u7ba1\u7406Python\u7248\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u5b89\u88c5IDE\u3001\u4ee5\u53ca\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u5728Mac\u4e0a\u9ad8\u6548\u5730\u5f00\u53d1\u548c\u8fd0\u884cPython\u7a0b\u5e8f\u3002\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6709\u52a9\u4e8e\u63d0\u5347Python\u7f16\u7a0b\u6280\u80fd\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9879\u76ee\u5f00\u53d1\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\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08python.org\uff09\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\u5b89\u88c5\u5305\u3002\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u53cc\u51fb\u5b89\u88c5\u5305\u5e76\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002\u4e3a\u4e86\u786e\u8ba4\u5b89\u88c5\u6210\u529f\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python3 --version<\/code>\uff0c\u5982\u679c\u80fd\u663e\u793a\u51fa\u7248\u672c\u53f7\uff0c\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>\u5728Mac\u4e0a\u4f7f\u7528Python\u7684\u5e38\u89c1\u5f00\u53d1\u73af\u5883\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Mac\u4e0a\uff0c\u6709\u591a\u79cd\u5f00\u53d1\u73af\u5883\u53ef\u4ee5\u4f7f\u7528Python\u8fdb\u884c\u7f16\u7a0b\u3002\u5e38\u89c1\u7684\u5305\u62ecPyCharm\u3001Visual Studio Code\u548cJupyter Notebook\u3002PyCharm\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684IDE\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u5f00\u53d1\uff1bVisual Studio Code\u5219\u8f7b\u91cf\u4e14\u652f\u6301\u591a\u79cd\u6269\u5c55\uff0c\u9002\u5408\u5404\u79cd\u89c4\u6a21\u7684\u9879\u76ee\uff1b\u800cJupyter Notebook\u5219\u975e\u5e38\u9002\u5408\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u7528\u6237\u4ee5\u4ea4\u4e92\u5f0f\u7684\u65b9\u5f0f\u8fd0\u884c\u4ee3\u7801\u548c\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Mac\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Mac\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u4ee3\u7801\u975e\u5e38\u76f4\u63a5\u3002\u6253\u5f00\u7ec8\u7aef\u5e94\u7528\uff0c\u8f93\u5165<code>python3<\/code>\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f\u3002\u5728\u8fd9\u91cc\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165Python\u4ee3\u7801\u5e76\u6309Enter\u952e\u6267\u884c\u3002\u5982\u679c\u60a8\u6709\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff08\u5982<code>script.py<\/code>\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7<code>python3 script.py<\/code>\u547d\u4ee4\u6765\u8fd0\u884c\u8be5\u6587\u4ef6\u3002\u786e\u4fdd\u7ec8\u7aef\u5f53\u524d\u76ee\u5f55\u662f\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u6216\u8005\u63d0\u4f9b\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Mac\u4e0a\u8fd0\u884cPython\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684\u7ec8\u7aef\u3001\u5b89\u88c5Python\u7684IDE\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":945904,"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\/945896"}],"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=945896"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945896\/revisions"}],"predecessor-version":[{"id":945905,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945896\/revisions\/945905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/945904"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=945896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=945896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=945896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}