{"id":1072245,"date":"2025-01-08T11:14:30","date_gmt":"2025-01-08T03:14:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072245.html"},"modified":"2025-01-08T11:14:33","modified_gmt":"2025-01-08T03:14:33","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8c%e7%a8%8b%e5%ba%8f%e5%90%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072245.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u6267\u884c\u7a0b\u5e8f\u5417"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102534\/7ab012e7-81dc-44c6-8be2-8c4dcd9fcfef.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u6267\u884c\u7a0b\u5e8f\u5417\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3001\u4f7f\u7528\u811a\u672c\u6a21\u5f0f\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fdb\u884c\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u6267\u884cPython\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u6267\u884cPython\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6267\u884cPython\u7a0b\u5e8f\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u65e0\u8bba\u662f\u5728Windows\u3001macOS\u8fd8\u662fLinux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u79cd\u65b9\u6cd5\u90fd\u975e\u5e38\u65b9\u4fbf\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efaPython\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Notepad++\u3001Sublime Text\u3001VS Code\u7b49\uff09\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u53ef\u4ee5\u662f\u4efb\u610f\u540d\u79f0\uff0c\u4f46\u9700\u8981\u4ee5<code>.py<\/code>\u4e3a\u6269\u5c55\u540d\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>hello.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6253\u5f00\u547d\u4ee4\u884c<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\u3002\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u6309\u4e0b<code>Win + R<\/code>\u6253\u5f00\u201c\u8fd0\u884c\u201d\u7a97\u53e3\uff0c\u8f93\u5165<code>cmd<\/code>\u5e76\u6309\u4e0b<code>Enter<\/code>\u952e\uff1b\u5728macOS\u548cLinux\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Terminal\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>3. \u5bfc\u822a\u5230\u6587\u4ef6\u6240\u5728\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u7a97\u53e3\u4e2d\uff0c\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5bfc\u822a\u5230Python\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>hello.py<\/code>\u6587\u4ef6\u4fdd\u5b58\u5728<code>C:\\Users\\YourName\\Documents<\/code>\u76ee\u5f55\u4e0b\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd C:\\Users\\YourName\\Documents<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u6267\u884cPython\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5bfc\u822a\u5230\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u6267\u884cPython\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5177\u4f53\u4f7f\u7528<code>python<\/code>\u8fd8\u662f<code>python3<\/code>\u53d6\u51b3\u4e8e\u4f60\u7684Python\u5b89\u88c5\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/p>\n<\/p>\n<p><p>IDE\u63d0\u4f9b\u4e86\u4e00\u4e2a\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u4f7f\u5f97\u7f16\u5199\u3001\u8c03\u8bd5\u548c\u6267\u884cPython\u7a0b\u5e8f\u66f4\u52a0\u4fbf\u6377\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u7684IDE\u53ca\u5176\u4f7f\u7528\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>1. PyCharm<\/h3>\n<\/p>\n<p><p>PyCharm\u662f\u7531JetBr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns\u5f00\u53d1\u7684\u4e00\u6b3e\u4e13\u4e1a\u7684Python IDE\uff0c\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5PyCharm\u3002<\/li>\n<li>\u6253\u5f00PyCharm\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002<\/li>\n<li>\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u3002<\/li>\n<li>\u53f3\u952e\u70b9\u51fbPython\u6587\u4ef6\uff0c\u9009\u62e9\u201cRun &#39;filename&#39;\u201d\u6765\u6267\u884c\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><h3>2. Visual Studio Code (VS Code)<\/h3>\n<\/p>\n<p><p>VS Code\u662f\u5fae\u8f6f\u5f00\u53d1\u7684\u4e00\u6b3e\u514d\u8d39\u5f00\u6e90\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5VS Code\u3002<\/li>\n<li>\u6253\u5f00VS Code\uff0c\u5b89\u88c5Python\u6269\u5c55\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee3\u7801\u3002<\/li>\n<li>\u5728VS Code\u4e2d\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>python filename.py<\/code>\u6765\u6267\u884c\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528\u811a\u672c\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u811a\u672c\u6a21\u5f0f\u9002\u7528\u4e8e\u7f16\u5199\u8f83\u957f\u7684Python\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216IDE\u6765\u6267\u884c\u8fd9\u4e9b\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>1. \u7f16\u5199\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u7f16\u5199Python\u811a\u672c\uff0c\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u7f16\u5199\u4e00\u4e2a\u540d\u4e3a<code>script.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    name = input(&quot;Enter your name: &quot;)<\/p>\n<p>    print(greet(name))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6267\u884c\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u547d\u4ee4\u884c\u6216IDE\u6267\u884c\u8be5\u811a\u672c\uff0c\u8f93\u5165<code>python script.py<\/code>\u6765\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>Python\u7684\u4ea4\u4e92\u6a21\u5f0f\u9002\u7528\u4e8e\u7f16\u5199\u548c\u6d4b\u8bd5\u5c0f\u7247\u6bb5\u4ee3\u7801\uff0c\u5feb\u901f\u9a8c\u8bc1\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u6253\u5f00\u4ea4\u4e92\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f\uff0c\u63d0\u793a\u7b26\u53d8\u4e3a<code>&gt;&gt;&gt;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2. \u7f16\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b\uff0c\u53ef\u4ee5\u9010\u884c\u8f93\u5165Python\u4ee3\u7801\uff0c\u5e76\u7acb\u5373\u770b\u5230\u6267\u884c\u7ed3\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&gt;&gt;&gt; print(&quot;Hello, World!&quot;)<\/p>\n<p>Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u9000\u51fa\u4ea4\u4e92\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u8f93\u5165<code>exit()<\/code>\u6216\u6309\u4e0b<code>Ctrl + Z<\/code>\uff08Windows\uff09\u6216<code>Ctrl + D<\/code>\uff08macOS\/Linux\uff09\u9000\u51fa\u4ea4\u4e92\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u3001IDE\u3001\u811a\u672c\u6a21\u5f0f\u8fd8\u662f\u4ea4\u4e92\u6a21\u5f0f\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u591a\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u6267\u884c\u7a0b\u5e8f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5de5\u4f5c\u73af\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4fbf\u6377\u6027\u3002<strong>\u4e86\u89e3\u5e76\u719f\u6089\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u638c\u63e1Python\u7f16\u7a0b\u6280\u5de7\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982VS Code\u3001PyCharm\u6216Notepad++\uff09\u7f16\u5199\u4ee3\u7801\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u4ee5<code>.py<\/code>\u4e3a\u6269\u5c55\u540d\u7684\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u5bfc\u822a\u5230\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8f93\u5165<code>python \u6587\u4ef6\u540d.py<\/code>\u5373\u53ef\u6267\u884c\u7a0b\u5e8f\u3002\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5Python\u5e76\u914d\u7f6e\u4e86\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8c03\u8bd5\u7a0b\u5e8f\u65f6\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8c03\u8bd5Python\u7a0b\u5e8f\u65f6\uff0c\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u7f29\u8fdb\u9519\u8bef\u548c\u7c7b\u578b\u9519\u8bef\u3002\u8bed\u6cd5\u9519\u8bef\u901a\u5e38\u662f\u56e0\u4e3a\u4ee3\u7801\u4e0d\u7b26\u5408Python\u8bed\u6cd5\u89c4\u8303\uff0c\u7f29\u8fdb\u9519\u8bef\u5219\u662f\u56e0\u4e3a\u4ee3\u7801\u5757\u7684\u7f29\u8fdb\u4e0d\u4e00\u81f4\u3002\u7c7b\u578b\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u5c1d\u8bd5\u5bf9\u4e0d\u517c\u5bb9\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u64cd\u4f5c\u65f6\u3002\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u5b9a\u4f4d\u548c\u4fee\u590d\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5728\u7a0b\u5e8f\u4e2d\u5bfc\u5165<code>sys<\/code>\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys.argv<\/code>\u8bbf\u95ee\u547d\u4ee4\u884c\u53c2\u6570\u5217\u8868\u3002<code>sys.argv[0]<\/code>\u662f\u7a0b\u5e8f\u7684\u540d\u79f0\uff0c\u540e\u7eed\u7684\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\u52a8\u6001\u5730\u4f20\u9012\u6570\u636e\uff0c\u589e\u5f3a\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6267\u884c\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3001\u4f7f\u7528\u811a\u672c\u6a21\u5f0f\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1072250,"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\/1072245"}],"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=1072245"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072245\/revisions"}],"predecessor-version":[{"id":1072254,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072245\/revisions\/1072254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}