{"id":1008235,"date":"2024-12-27T11:00:52","date_gmt":"2024-12-27T03:00:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008235.html"},"modified":"2024-12-27T11:00:54","modified_gmt":"2024-12-27T03:00:54","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e5%ae%89%e8%a3%85%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008235.html","title":{"rendered":"python\u5982\u4f55\u770b\u5b89\u88c5\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083626\/628d1271-f0f2-4bd7-8853-0b48666e19d2.webp\" alt=\"python\u5982\u4f55\u770b\u5b89\u88c5\u8def\u5f84\" \/><\/p>\n<p><p> \u8981\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b\u3001\u4f7f\u7528Python\u811a\u672c\u67e5\u770b\u3001\u901a\u8fc7IDE\u67e5\u770b\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u67e5\u627e<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u76f8\u5173\u547d\u4ee4\u5373\u53ef\u83b7\u53d6Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b<\/h3>\n<\/p>\n<p><h4>1.1 Windows\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\uff08cmd\uff09\u6765\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff1a\u6309\u4e0b<code>Win + R<\/code>\u952e\uff0c\u8f93\u5165<code>cmd<\/code>\u540e\u6309\u56de\u8f66\u3002<\/li>\n<li>\u8f93\u5165\u547d\u4ee4\uff1a\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>where python<\/code>\u5e76\u6309\u56de\u8f66\u3002<\/li>\n<li>\u67e5\u770b\u7ed3\u679c\uff1a\u547d\u4ee4\u4f1a\u8fd4\u56dePython\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\uff0c\u8fd9\u5c31\u662fPython\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u5feb\u6377\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h4>1.2 Mac\u548cLinux\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Mac\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u627e\u5230\u7ec8\u7aef\u5e76\u6253\u5f00\u3002<\/li>\n<li>\u8f93\u5165\u547d\u4ee4\uff1a\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165<code>which python<\/code>\u6216<code>which python3<\/code>\uff0c\u6839\u636e\u4f60\u5b89\u88c5\u7684Python\u7248\u672c\u9009\u62e9\u3002<\/li>\n<li>\u67e5\u770b\u7ed3\u679c\uff1a\u7ec8\u7aef\u4f1a\u8fd4\u56dePython\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython\u5df2\u7ecf\u5b89\u88c5\u5728\u7cfb\u7edf\u4e2d\uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u5b9e\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u811a\u672c\u67e5\u770b<\/h3>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528<code>sys<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86<code>sys<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.executable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0a\u811a\u672c\uff0c\u4f1a\u8f93\u51faPython\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528<code>os<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>os<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6Python\u7684\u5b89\u88c5\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(os.path.dirname(os.__file__))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u4f1a\u8f93\u51fa<code>os<\/code>\u6a21\u5757\u6240\u5728\u7684\u76ee\u5f55\u8def\u5f84\uff0c\u901a\u5e38\u5b83\u4f4d\u4e8ePython\u7684\u5b89\u88c5\u76ee\u5f55\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7IDE\u67e5\u770b<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u63d0\u4f9b\u4e86\u67e5\u770bPython\u5b89\u88c5\u8def\u5f84\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3.1 PyCharm<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770bPython\u8def\u5f84\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00PyCharm\u5e76\u52a0\u8f7d\u9879\u76ee\u3002<\/li>\n<li>\u8fdb\u5165<code>File &gt; Settings &gt; Project: [Your Project Name] &gt; Python Interpreter<\/code>\u3002<\/li>\n<li>\u5728\u53f3\u4fa7\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5f53\u524d\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\u53ca\u5176\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h4>3.2 Visual Studio Code<\/h4>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u9762\u677f\u67e5\u770bPython\u8def\u5f84\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00VS Code\u5e76\u52a0\u8f7d\u9879\u76ee\u3002<\/li>\n<li>\u6309\u4e0b<code>Ctrl + Shift + P<\/code>\uff0c\u8f93\u5165<code>Python: Select Interpreter<\/code>\u3002<\/li>\n<li>\u5728\u5217\u8868\u4e2d\u9009\u62e9\u5f53\u524d\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\uff0c\u67e5\u770b\u5176\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u67e5\u627e<\/h3>\n<\/p>\n<p><h4>4.1 Windows\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u4e2d\uff0cPython\u7684\u5b89\u88c5\u8def\u5f84\u901a\u5e38\u4f1a\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u53f3\u952e\u70b9\u51fb<code>\u6b64\u7535\u8111<\/code>\u6216<code>\u6211\u7684\u7535\u8111<\/code>\uff0c\u9009\u62e9<code>\u5c5e\u6027<\/code>\u3002<\/li>\n<li>\u9009\u62e9<code>\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e<\/code>\uff0c\u7136\u540e\u70b9\u51fb<code>\u73af\u5883\u53d8\u91cf<\/code>\u3002<\/li>\n<li>\u5728<code>\u7cfb\u7edf\u53d8\u91cf<\/code>\u4e2d\u627e\u5230<code>Path<\/code>\uff0c\u53cc\u51fb\u67e5\u770b\u5176\u4e2d\u662f\u5426\u5305\u542bPython\u7684\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h4>4.2 Mac\u548cLinux\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Mac\u548cLinux\u4e2d\uff0cPython\u7684\u8def\u5f84\u901a\u5e38\u4fdd\u5b58\u5728<code>.bashrc<\/code>\u3001<code>.bash_profile<\/code>\u6216<code>.zshrc<\/code>\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u8f93\u5165\u547d\u4ee4<code>cat ~\/.bashrc<\/code>\u6216<code>cat ~\/.bash_profile<\/code>\uff0c\u6839\u636e\u4f60\u7684shell\u7c7b\u578b\u9009\u62e9\u3002<\/li>\n<li>\u67e5\u770b\u6587\u4ef6\u5185\u5bb9\uff0c\u5bfb\u627e\u4e0ePython\u76f8\u5173\u7684\u8def\u5f84\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4e86\u89e3\u5982\u4f55\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\u5bf9\u4e8e\u5f00\u53d1\u8005\u6765\u8bf4\u662f\u4e00\u4e2a\u57fa\u672c\u6280\u80fd\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u73af\u5883\u914d\u7f6e\u548c\u8c03\u8bd5\u65f6\u3002<strong>\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u6765\u786e\u8ba4\u8def\u5f84\u7684\u51c6\u786e\u6027\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u548c\u914d\u7f6ePython\u73af\u5883\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5Python\u7684\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>which python<\/code>\uff08\u5728Linux\u548cmacOS\u4e0a\uff09\u6216<code>where python<\/code>\uff08\u5728Windows\u4e0a\uff09\u3002\u8fd9\u5c06\u663e\u793aPython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002\u6b64\u5916\uff0c\u8fdb\u5165Python\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>import sys<\/code>\u548c<code>print(sys.executable)<\/code>\u6765\u83b7\u53d6Python\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Windows\u4e0a\u5982\u4f55\u627e\u5230Python\u7684\u5b89\u88c5\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u8fd0\u884c<code>where python<\/code>\u6765\u67e5\u627ePython\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u53ef\u4ee5\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\uff0c\u786e\u4fddPython\u7684\u5b89\u88c5\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u53d8\u91cf\u4e2d\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u65f6\u65e0\u6cd5\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u5982\u679c\u5728\u547d\u4ee4\u884c\u4e2d\u627e\u4e0d\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u80fd\u662f\u56e0\u4e3aPython\u6ca1\u6709\u6b63\u786e\u5b89\u88c5\uff0c\u6216\u8005\u5176\u8def\u5f84\u6ca1\u6709\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5Python\uff0c\u5e76\u786e\u4fdd\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u4ee5\u4fbf\u81ea\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b\u3001\u4f7f\u7528Python\u811a\u672c\u67e5\u770b\u3001\u901a\u8fc7IDE\u67e5\u770b\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1008243,"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\/1008235"}],"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=1008235"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008235\/revisions"}],"predecessor-version":[{"id":1008246,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008235\/revisions\/1008246"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008243"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}