{"id":1011891,"date":"2024-12-27T11:34:01","date_gmt":"2024-12-27T03:34:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011891.html"},"modified":"2024-12-27T11:34:04","modified_gmt":"2024-12-27T03:34:04","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8bpython%e5%ae%89%e8%a3%85%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011891.html","title":{"rendered":"\u5982\u4f55\u68c0\u6d4bpython\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\/25085851\/3f7b4111-8c0b-4e6a-afe3-a59488a9d9cf.webp\" alt=\"\u5982\u4f55\u68c0\u6d4bpython\u5b89\u88c5\u8def\u5f84\" \/><\/p>\n<p><p> <strong>\u8981\u68c0\u6d4bPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u6216\u8005\u5728Python\u73af\u5883\u4e2d\u67e5\u8be2\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>which python<\/code>\uff08Linux\u548cMac\uff09\u6216<code>where python<\/code>\uff08Windows\uff09\u547d\u4ee4\u6765\u67e5\u770bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u5728Python\u4ea4\u4e92\u73af\u5883\u4e2d\u4f7f\u7528<code>sys.executable<\/code>\u6765\u83b7\u53d6Python\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u73af\u5883\u53d8\u91cf\u68c0\u67e5<\/p>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0cPython\u7684\u5b89\u88c5\u8def\u5f84\u901a\u5e38\u4f1a\u88ab\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u8fd9\u4f7f\u5f97\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u53ef\u4ee5\u76f4\u63a5\u6267\u884cPython\u547d\u4ee4\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u68c0\u67e5Python\u8def\u5f84\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u201c\u63a7\u5236\u9762\u677f\u201d\u3002<\/li>\n<li>\u9009\u62e9\u201c\u7cfb\u7edf\u548c\u5b89\u5168\u201d\u3002<\/li>\n<li>\u9009\u62e9\u201c\u7cfb\u7edf\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u4e2d\uff0c\u627e\u5230\u540d\u4e3a\u201cPath\u201d\u7684\u53d8\u91cf\uff0c\u53cc\u51fb\u67e5\u770b\u5176\u4e2d\u662f\u5426\u5305\u542bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Linux\u548cMac<\/strong><\/p>\n<\/p>\n<p><p>\u5728Linux\u548cMac\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u73af\u5883\u53d8\u91cf\u4e2dPython\u7684\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">echo $PATH<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8be5\u547d\u4ee4\u53ef\u4ee5\u770b\u5230\u73af\u5883\u53d8\u91cf<code>PATH<\/code>\u7684\u503c\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\u3002\u5982\u679cPython\u5df2\u7ecf\u5b89\u88c5\uff0c\u5176\u8def\u5f84\u5e94\u8be5\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u5de5\u5177\u63d0\u4f9b\u4e86\u5feb\u901f\u68c0\u67e5Python\u5b89\u88c5\u8def\u5f84\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Linux\u548cMac<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>which<\/code>\u547d\u4ee4\u6765\u67e5\u627ePython\u7684\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">which python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff0c\u5982\u679c\u4f7f\u7528\u7684\u662fPython 3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">which python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u4f1a\u8fd4\u56dePython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Windows<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>where<\/code>\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">where python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u540c\u6837\u4f1a\u8fd4\u56dePython\u7684\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7Python\u5185\u7f6e\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Python\u73af\u5883\u4e2d\u83b7\u53d6\u5b89\u88c5\u8def\u5f84\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>sys<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u7684<code>executable<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524dPython\u89e3\u91ca\u5668\u7684\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>\u8fd9\u884c\u4ee3\u7801\u5c06\u5728Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u51faPython\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>os<\/code>\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u95f4\u63a5\u83b7\u53d6Python\u7684\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\u5c06\u8fd4\u56dePython\u6807\u51c6\u5e93\u6240\u5728\u7684\u8def\u5f84\uff0c\u4ece\u4e2d\u53ef\u4ee5\u63a8\u65ad\u51faPython\u7684\u5b89\u88c5\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u68c0\u67e5\u7cfb\u7edf\u6ce8\u518c\u8868\uff08\u4ec5\u9650Windows\uff09<\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0cPython\u7684\u5b89\u88c5\u4fe1\u606f\u53ef\u80fd\u5b58\u50a8\u5728\u6ce8\u518c\u8868\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u6ce8\u518c\u8868\u7f16\u8f91\u5668\u6765\u67e5\u770b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00\u6ce8\u518c\u8868\u7f16\u8f91\u5668\uff08\u6309\u4e0b<code>Windows + R<\/code>\u952e\uff0c\u8f93\u5165<code>regedit<\/code>\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u822a\u5230\u4ee5\u4e0b\u8def\u5f84\u4e4b\u4e00\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\{version}\\InstallPath<\/code><\/li>\n<li><code>HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\{version}\\InstallPath<\/code><\/li>\n<\/ul>\n<p><p><code>{version}<\/code>\u662fPython\u7684\u7248\u672c\u53f7\uff0c\u4f8b\u59823.9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u8def\u5f84\u4e0b\uff0c\u53ef\u4ee5\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001Visual Studio Code\u7b49\uff0c\u63d0\u4f9b\u4e86\u914d\u7f6ePython\u89e3\u91ca\u5668\u7684\u529f\u80fd\u3002\u5728\u8fd9\u4e9bIDE\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6216\u914d\u7f6e\u754c\u9762\u67e5\u770b\u548c\u9009\u62e9Python\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyCharm<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00PyCharm\u5e76\u8fdb\u5165\u9879\u76ee\u3002<\/li>\n<li>\u70b9\u51fb\u83dc\u5355\u680f\u7684\u201cFile\u201d\u9009\u9879\uff0c\u7136\u540e\u9009\u62e9\u201cSettings\u201d\u3002<\/li>\n<li>\u5728\u8bbe\u7f6e\u7a97\u53e3\u4e2d\uff0c\u5bfc\u822a\u5230\u201cProject: <project name> -&gt; Python Interpreter\u201d\u3002<\/li>\n<li>\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u5f53\u524d\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\u53ca\u5176\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Visual Studio Code<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00Visual Studio Code\u5e76\u8fdb\u5165\u9879\u76ee\u3002<\/li>\n<li>\u6309\u4e0b<code>Ctrl + Shift + P<\/code>\uff0c\u8f93\u5165\u201cPython: Select Interpreter\u201d\u3002<\/li>\n<li>\u8fd9\u5c06\u663e\u793a\u53ef\u7528\u7684Python\u89e3\u91ca\u5668\u53ca\u5176\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u9009\u62e9\u6700\u9002\u5408\u60a8\u7684\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u5b9a\u4f4dPython\u7684\u5b89\u88c5\u76ee\u5f55\uff0c\u4ece\u800c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5f00\u53d1\u548c\u914d\u7f6e\u5de5\u4f5c\u3002\u786e\u4fdd\u5728\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\u65f6\u6ce8\u610f\u7cfb\u7edf\u7684\u6743\u9650\u548c\u5b89\u5168\u6027\uff0c\u4ee5\u514d\u8bef\u64cd\u4f5c\u5bfc\u81f4\u7cfb\u7edf\u6545\u969c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u8ba4\u6211\u7684\u8ba1\u7b97\u673a\u4e0a\u662f\u5426\u5b89\u88c5\u4e86Python\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u8ba1\u7b97\u673a\u4e0a\u662f\u5426\u5b89\u88c5\u4e86Python\uff0c\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u3002\u5982\u679cPython\u5df2\u5b89\u88c5\uff0c\u7cfb\u7edf\u5c06\u663e\u793a\u5176\u7248\u672c\u53f7\u3002\u5982\u679c\u51fa\u73b0\u201c\u672a\u627e\u5230\u547d\u4ee4\u201d\u6216\u7c7b\u4f3c\u63d0\u793a\uff0c\u8868\u660ePython\u5c1a\u672a\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u6211\u8be5\u5982\u4f55\u67e5\u627ePython\u7684\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>where python<\/code>\u6216<code>where python3<\/code>\u6765\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u8fd9\u5c06\u663e\u793aPython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u786e\u5207\u4f4d\u7f6e\u3002\u60a8\u4e5f\u53ef\u4ee5\u5728\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e2d\u67e5\u770bPATH\u53d8\u91cf\uff0c\u901a\u5e38\u5305\u542bPython\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Mac\u6216Linux\u7cfb\u7edf\u4e0a\u67e5\u627ePython\u7684\u5b89\u88c5\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5728Mac\u6216Linux\u7cfb\u7edf\u4e2d\uff0c\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165<code>which python<\/code>\u6216<code>which python3<\/code>\uff0c\u7cfb\u7edf\u4f1a\u8fd4\u56dePython\u7684\u5b89\u88c5\u8def\u5f84\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>echo $PATH<\/code>\u547d\u4ee4\u67e5\u770b\u6240\u6709\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbf\u627e\u5230\u76f8\u5173\u7684Python\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u68c0\u6d4bPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u6216\u8005\u5728Python\u73af\u5883\u4e2d\u67e5\u8be2\u7b49\u65b9\u6cd5\u6765\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1011901,"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\/1011891"}],"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=1011891"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011891\/revisions"}],"predecessor-version":[{"id":1011903,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011891\/revisions\/1011903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011901"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}