{"id":945328,"date":"2024-12-26T23:24:46","date_gmt":"2024-12-26T15:24:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/945328.html"},"modified":"2024-12-26T23:24:48","modified_gmt":"2024-12-26T15:24:48","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5python%e4%bd%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/945328.html","title":{"rendered":"\u5982\u4f55\u67e5python\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081952\/ea4e9963-e78c-471e-8f78-a00d06ea71a3.webp\" alt=\"\u5982\u4f55\u67e5python\u4f4d\u7f6e\" \/><\/p>\n<p><p> <strong>\u67e5\u627ePython\u7684\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u547d\u4ee4\u884c\u547d\u4ee4\u3001Python\u7684\u5185\u7f6e\u6a21\u5757\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u641c\u7d22\u529f\u80fd\u6765\u5b9e\u73b0\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u4ee5\u4fbf\u8fdb\u884c\u914d\u7f6e\u3001\u8c03\u8bd5\u548c\u5176\u4ed6\u5f00\u53d1\u5de5\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u547d\u4ee4<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4\u884c\u662f\u67e5\u627ePython\u4f4d\u7f6e\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u9002\u7528\u4e8eWindows\u3001macOS\u548cLinux\u7b49\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>Windows\u547d\u4ee4\u63d0\u793a\u7b26<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u6765\u67e5\u627ePython\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">where python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8fd4\u56de\u7cfb\u7edf\u4e2d\u53ef\u6267\u884cPython\u7684\u8def\u5f84\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b89\u88c5\u4e86\u591a\u4e2aPython\u7248\u672c\uff0c\u53ef\u80fd\u4f1a\u8fd4\u56de\u591a\u4e2a\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>macOS\u548cLinux\u7ec8\u7aef<\/strong><\/li>\n<\/ol>\n<p><p>\u5728macOS\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">which python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">which python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u8f93\u51faPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002\u5982\u679c\u6709\u591a\u4e2a\u7248\u672c\uff0c\u901a\u5e38\u201cpython\u201d\u6307\u5411Python 2.x\uff0c\u800c\u201cpython3\u201d\u6307\u5411Python 3.x\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u672c\u8eab\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u5176\u5b89\u88c5\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>sys<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u6765\u83b7\u53d6Python\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>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u6253\u5370\u51faPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>os<\/code>\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p><p><code>os<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u627e\u5230Python\u7684\u8def\u5f84\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(os.environ[&#39;PATH&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u4e0d\u4f1a\u76f4\u63a5\u7ed9\u51faPython\u7684\u4f4d\u7f6e\uff0c\u4f46\u53ef\u4ee5\u67e5\u770b\u201cPATH\u201d\u73af\u5883\u53d8\u91cf\u4e2dPython\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u5f00\u53d1\u8005\u4f7f\u7528IDE\u8fdb\u884cPython\u5f00\u53d1\uff0c\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u4f1a\u663e\u793aPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>PyCharm<\/strong><\/li>\n<\/ol>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770bPython\u89e3\u91ca\u5668\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u9879\u76ee\u540e\uff0c\u8fdb\u5165\u201cFile\u201d -&gt; \u201cSettings\u201d\uff08\u6216\u201cPreferences\u201d\uff09 -&gt; \u201cProject: [Project Name]\u201d -&gt; \u201cPython Interpreter\u201d\u3002<\/li>\n<li>\u5728\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u5f53\u524d\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>Visual Studio Code<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Visual Studio Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u201csettings.json\u201d\u6587\u4ef6\u6216\u4f7f\u7528\u547d\u4ee4\u9762\u677f\u6765\u627e\u5230Python\u8def\u5f84\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u9762\u677f\uff08Ctrl+Shift+P\uff09\uff0c\u8f93\u5165\u201cPython: Select Interpreter\u201d\uff0c\u7136\u540e\u9009\u62e9\u89e3\u91ca\u5668\u3002<\/li>\n<li>\u89e3\u91ca\u5668\u8def\u5f84\u4f1a\u5728\u5e95\u90e8\u72b6\u6001\u680f\u663e\u793a\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u641c\u7d22\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u641c\u7d22\u529f\u80fd\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>Windows<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Windows\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u5668\u4e2d\uff0c\u53ef\u4ee5\u641c\u7d22\u201cpython.exe\u201d\u6765\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>macOS<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Finder\u4e2d\uff0c\u4f7f\u7528\u641c\u7d22\u529f\u80fd\uff0c\u8f93\u5165\u201cpython\u201d\uff0c\u53ef\u4ee5\u627e\u5230\u5b89\u88c5\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>Linux<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Linux\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u641c\u7d22\u5de5\u5177\uff0c\u5982\u201clocate\u201d\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">locate python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5217\u51fa\u6240\u6709\u4e0ePython\u76f8\u5173\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b89\u88c5\u591a\u4e2aPython\u7248\u672c\u7684\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u4f1a\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u5b89\u88c5\u591a\u4e2a\u7248\u672c\u7684Python\u3002\u8fd9\u65f6\uff0c\u627e\u5230\u5177\u4f53\u7248\u672c\u7684\u8def\u5f84\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>pyenv<\/code>\u7ba1\u7406\u7248\u672c<\/strong><\/li>\n<\/ol>\n<p><p><code>pyenv<\/code>\u662f\u4e00\u79cd\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\u7684\u5de5\u5177\uff0c\u5b89\u88c5\u540e\u53ef\u4ee5\u7b80\u5355\u5207\u6362\u548c\u67e5\u770bPython\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyenv versions<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5217\u51fa\u6240\u6709\u5b89\u88c5\u7684\u7248\u672c\uff0c\u4ee5\u53ca\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>virtualenv<\/code>\u548c<code>venv<\/code><\/strong><\/li>\n<\/ol>\n<p><p>\u865a\u62df\u73af\u5883\u5de5\u5177<code>virtualenv<\/code>\u548cPython\u5185\u7f6e\u7684<code>venv<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u7ba1\u7406\u4e0d\u540c\u9879\u76eePython\u7248\u672c\u7684\u65b9\u6cd5\u3002\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys.executable<\/code>\u6765\u67e5\u770b\u4f7f\u7528\u7684Python\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u67e5\u627ePython\u7684\u4f4d\u7f6e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u73af\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u3001Python\u6a21\u5757\u3001IDE\u8fd8\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u641c\u7d22\u529f\u80fd\uff0c\u90fd\u80fd\u591f\u6709\u6548\u5730\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5bf9Python\u5f00\u53d1\u8005\u7ba1\u7406\u5f00\u53d1\u73af\u5883\u548c\u89e3\u51b3\u95ee\u9898\u975e\u5e38\u6709\u5e2e\u52a9\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\u5b89\u88c5\u4e86Python\uff1f<\/strong><br \/>\u8981\u786e\u8ba4\u60a8\u7684\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\u4f1a\u8fd4\u56de\u5f53\u524d\u7684\u7248\u672c\u53f7\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u60a8\u5c06\u6536\u5230\u4e00\u4e2a\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p><strong>\u5728Windows\u4e0a\u5982\u4f55\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u529f\u80fd\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u6253\u5f00\u201c\u5f00\u59cb\u201d\u83dc\u5355\uff0c\u641c\u7d22\u201cPython\u201d\uff0c\u53f3\u952e\u70b9\u51fbPython\u5e94\u7528\u7a0b\u5e8f\uff0c\u9009\u62e9\u201c\u6253\u5f00\u6587\u4ef6\u4f4d\u7f6e\u201d\u3002\u5728\u6253\u5f00\u7684\u6587\u4ef6\u5939\u4e2d\uff0c\u60a8\u5c06\u770b\u5230Python\u7684\u53ef\u6267\u884c\u6587\u4ef6\u53ca\u5176\u8def\u5f84\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Mac\u6216Linux\u7cfb\u7edf\u4e2d\u67e5\u627ePython\u7684\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5728Mac\u6216Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528<code>which python<\/code>\u6216<code>which python3<\/code>\u547d\u4ee4\u6765\u67e5\u627ePython\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002\u8be5\u547d\u4ee4\u5c06\u8fd4\u56dePython\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u5e2e\u52a9\u60a8\u786e\u8ba4\u5176\u5b89\u88c5\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u83b7\u53d6\u5176\u5b89\u88c5\u8def\u5f84\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528<code>import sys<\/code>\u548c<code>print(sys.executable)<\/code>\u6765\u83b7\u53d6Python\u7684\u5b89\u88c5\u8def\u5f84\u3002\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51faPython\u89e3\u91ca\u5668\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u65b9\u4fbf\u60a8\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u627ePython\u7684\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u547d\u4ee4\u884c\u547d\u4ee4\u3001Python\u7684\u5185\u7f6e\u6a21\u5757\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u641c [&hellip;]","protected":false},"author":3,"featured_media":945330,"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\/945328"}],"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=945328"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945328\/revisions"}],"predecessor-version":[{"id":945332,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/945328\/revisions\/945332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/945330"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=945328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=945328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=945328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}