{"id":1043754,"date":"2024-12-31T13:08:48","date_gmt":"2024-12-31T05:08:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043754.html"},"modified":"2024-12-31T13:08:51","modified_gmt":"2024-12-31T05:08:51","slug":"%e5%a6%82%e4%bd%95%e5%91%bd%e4%bb%a4%e8%a1%8c%e4%b8%ad%e4%bd%bf%e7%94%a8python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043754.html","title":{"rendered":"\u5982\u4f55\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528python\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0f2d8228-d334-452b-b8da-0a82f085ca24.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528python\u811a\u672c\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528python\u811a\u672c<\/strong>,\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b89\u88c5Python\u3001\u7f16\u5199Python\u811a\u672c\u3001\u4fdd\u5b58\u811a\u672c\u3001\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884c\u811a\u672c\u3002<strong>\u5b89\u88c5Python\u3001\u7f16\u5199Python\u811a\u672c\u3001\u4fdd\u5b58\u811a\u672c\u3001\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884c\u811a\u672c<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5b89\u88c5Python<\/strong>\u662f\u7b2c\u4e00\u6b65\uff0c\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u6ca1\u6709Python\u73af\u5883\u7684\u8bdd\uff0c\u540e\u7eed\u64cd\u4f5c\u5c31\u65e0\u4ece\u8c08\u8d77\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7dPython\u5b89\u88c5\u5305<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84Python%E5%AE%89%E8%A3%85%E5%8C%85%E3%80%82%E9%80%9A%E5%B8%B8%E5%BB%BA%E8%AE%AE%E4%B8%8B%E8%BD%BD%E7%A8%B3%E5%AE%9A%E7%89%88%E6%9C%AC%E3%80%82\">https:\/\/www.python.org\/\uff09\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Python\u5b89\u88c5\u5305\u3002\u901a\u5e38\u5efa\u8bae\u4e0b\u8f7d\u7a33\u5b9a\u7248\u672c\u3002<\/a><\/li>\n<li>\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u4e0b\u8f7dWindows Installer\uff1b\u5bf9\u4e8eMac\u7528\u6237\uff0c\u4e0b\u8f7dmacOS installer\uff1b\u5bf9\u4e8eLinux\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8fd0\u884c\u4e0b\u8f7d\u7684\u5b89\u88c5\u5305\uff0c\u6309\u7167\u63d0\u793a\u8fdb\u884c\u5b89\u88c5\u3002\u5efa\u8bae\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u4ee5\u4fbf\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528Python\u547d\u4ee4\u3002<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6253\u5f00\u547d\u4ee4\u884c\uff08Windows\u7528\u6237\u53ef\u4ee5\u4f7f\u7528cmd\u6216PowerShell\uff0cMac\u548cLinux\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Terminal\uff09\uff0c\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u68c0\u67e5Python\u662f\u5426\u6b63\u786e\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u7f16\u5199Python\u811a\u672c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u6587\u672c\u7f16\u8f91\u5668<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u4efb\u610f\u6587\u672c\u7f16\u8f91\u5668\u7f16\u5199Python\u811a\u672c\u3002\u5e38\u89c1\u7684\u7f16\u8f91\u5668\u6709\uff1aNotepad++\u3001Sublime Text\u3001VS Code\u3001PyCharm\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u7f16\u5199\u4f60\u6240\u9700\u8981\u7684Python\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c<code>hello.py<\/code>\uff1a\n<pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4fdd\u5b58\u811a\u672c<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4fdd\u5b58\u811a\u672c\u6587\u4ef6<\/strong>\n<ul>\n<li>\u5c06\u7f16\u5199\u597d\u7684Python\u4ee3\u7801\u6587\u4ef6\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6269\u5c55\u540d\u7684\u6587\u4ef6\uff0c\u4f8b\u5982<code>hello.py<\/code>\u3002<\/li>\n<li>\u786e\u4fdd\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84\u5bb9\u6613\u627e\u5230\uff0c\u5efa\u8bae\u4fdd\u5b58\u5230\u684c\u9762\u6216\u6307\u5b9a\u7684\u9879\u76ee\u6587\u4ef6\u5939\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u547d\u4ee4\u884c<\/strong>\n<ul>\n<li>\u5bf9\u4e8eWindows\u7528\u6237\uff1a\u6309\u4e0b<code>Win + R<\/code>\uff0c\u8f93\u5165<code>cmd<\/code>\u6216<code>powershell<\/code>\u540e\u56de\u8f66\u3002<\/li>\n<li>\u5bf9\u4e8eMac\u7528\u6237\uff1a\u6309\u4e0b<code>Command + Space<\/code>\uff0c\u8f93\u5165<code>Terminal<\/code>\u540e\u56de\u8f66\u3002<\/li>\n<li>\u5bf9\u4e8eLinux\u7528\u6237\uff1a\u6309\u4e0b<code>Ctrl + Alt + T<\/code>\uff0c\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884c\u811a\u672c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5bfc\u822a\u5230Python\u811a\u672c\u6240\u5728\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5982\u679c\u811a\u672c\u4fdd\u5b58\u5728\u684c\u9762\u4e0a\uff0c\u8f93\u5165\uff1a\n<pre><code class=\"language-sh\">cd Desktop<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5bf9\u4e8e\u7279\u5b9a\u8def\u5f84\u7684\u5bfc\u822a\uff0c\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u811a\u672c<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cPython\u811a\u672c\uff1a\n<pre><code class=\"language-sh\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5982\u679c\u5b89\u88c5\u7684\u662fPython3\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a\n<pre><code class=\"language-sh\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u811a\u672c<code>greet.py<\/code>\uff1a\n<pre><code class=\"language-python\">import sys<\/p>\n<p>name = sys.argv[1]<\/p>\n<p>print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8fd0\u884c\u811a\u672c\u5e76\u4f20\u9012\u53c2\u6570\uff1a\n<pre><code class=\"language-sh\">python greet.py Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8f93\u51fa\u7ed3\u679c\u4f1a\u662f<code>Hello, Alice!<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-sh\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<ul>\n<li>\u5bf9\u4e8eWindows\uff1a\n<pre><code class=\"language-sh\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5bf9\u4e8eMac\u548cLinux\uff1a\n<pre><code class=\"language-sh\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884c\u811a\u672c<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u518d\u8fd0\u884cPython\u811a\u672c\u65f6\u4f1a\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\u548c\u5305\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u8c03\u8bd5\u548c\u65e5\u5fd7<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u8bd5\u811a\u672c<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u4f7f\u7528<code>pdb<\/code>\u6a21\u5757\u8fdb\u884c\u811a\u672c\u8c03\u8bd5\u3002\u4f8b\u5982\uff0c\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8c03\u8bd5\uff1a\n<pre><code class=\"language-python\">import pdb; pdb.set_trace()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5f53\u8fd0\u884c\u5230\u8be5\u884c\u65f6\uff0c\u811a\u672c\u4f1a\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\uff0c\u5141\u8bb8\u9010\u6b65\u6267\u884c\u548c\u68c0\u67e5\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff0c\u4fbf\u4e8e\u5206\u6790\u548c\u6392\u67e5\u95ee\u9898\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logging.info(&quot;This is an info message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python\u811a\u672c\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u811a\u672c\u8fd0\u884c\uff0c\u8fd8\u662f\u590d\u6742\u7684\u53c2\u6570\u4f20\u9012\u548c\u8c03\u8bd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u5927\u5927\u63d0\u9ad8\u60a8\u7684\u5de5\u4f5c\u6548\u7387\u548c\u5f00\u53d1\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u9700\u8981\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff08Windows\u4e0a\u4e3aCMD\uff0cMac\u548cLinux\u4e0a\u4e3a\u7ec8\u7aef\uff09\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5207\u6362\u5230\u5b58\u653ePython\u811a\u672c\u7684\u76ee\u5f55\u3002\u8f93\u5165<code>python script_name.py<\/code>\uff0c\u5176\u4e2d<code>script_name.py<\/code>\u662f\u4f60\u7684\u811a\u672c\u6587\u4ef6\u540d\u3002\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u540c\u65f6\u5b89\u88c5\u4e86Python 2\u548cPython 3\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528<code>python3<\/code>\u6765\u8fd0\u884cPython 3\u7684\u811a\u672c\u3002<\/p>\n<p><strong>\u5728\u547d\u4ee4\u884c\u4e2d\u5982\u4f55\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff1f<\/strong><br \/>\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u547d\u4ee4\u65f6\u76f4\u63a5\u5728\u811a\u672c\u540d\u540e\u6dfb\u52a0\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8fd0\u884c<code>python script_name.py arg1 arg2<\/code>\u3002\u5728\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.argv<\/code>\u5217\u8868\u6765\u8bbf\u95ee\u8fd9\u4e9b\u53c2\u6570\u3002<code>sys.argv[0]<\/code>\u662f\u811a\u672c\u540d\uff0c\u540e\u7eed\u7684\u7d22\u5f15\u5bf9\u5e94\u4f20\u9012\u7684\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u65f6\u9047\u5230\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u9047\u5230\u9519\u8bef\u65f6\uff0c\u9996\u5148\u68c0\u67e5Python\u662f\u5426\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u786e\u4fdd\u811a\u672c\u8def\u5f84\u6b63\u786e\u65e0\u8bef\u3002\u5982\u679c\u51fa\u73b0\u201c\u627e\u4e0d\u5230\u6587\u4ef6\u201d\u6216\u201c\u547d\u4ee4\u672a\u627e\u5230\u201d\u4e4b\u7c7b\u7684\u9519\u8bef\uff0c\u786e\u8ba4\u662f\u5426\u5728\u6b63\u786e\u7684\u76ee\u5f55\u4e0b\uff0c\u5e76\u786e\u4fdd\u8f93\u5165\u7684\u547d\u4ee4\u683c\u5f0f\u6b63\u786e\u3002\u6b64\u5916\uff0c\u67e5\u770bPython\u811a\u672c\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\u6216\u903b\u8f91\u9519\u8bef\u4e5f\u5f88\u91cd\u8981\uff0c\u4f7f\u7528<code>python -m py_compile script_name.py<\/code>\u53ef\u4ee5\u5e2e\u52a9\u68c0\u67e5\u8bed\u6cd5\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528python\u811a\u672c,\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b89\u88c5Python\u3001\u7f16\u5199Python\u811a\u672c\u3001\u4fdd\u5b58\u811a\u672c\u3001\u6253\u5f00\u547d [&hellip;]","protected":false},"author":3,"featured_media":1043771,"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\/1043754"}],"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=1043754"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043754\/revisions"}],"predecessor-version":[{"id":1043776,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043754\/revisions\/1043776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043771"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}