{"id":1008825,"date":"2024-12-27T11:06:12","date_gmt":"2024-12-27T03:06:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008825.html"},"modified":"2024-12-27T11:06:14","modified_gmt":"2024-12-27T03:06:14","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e6%96%87%e4%bb%b6%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008825.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u6587\u4ef6\u6267\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084005\/4762d3ee-def0-440a-a180-09eb00acef78.webp\" alt=\"\u5982\u4f55\u8ba9python\u6587\u4ef6\u6267\u884c\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Python\u6587\u4ef6\u6267\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3001\u53cc\u51fb\u6587\u4ef6\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7b49\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u989d\u5916\u7684\u5de5\u5177\uff0c\u4e14\u9002\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u6267\u884cPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u547d\u4ee4\u884c\u6267\u884c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u6309\u4e0bWin+R\u952e\uff0c\u7136\u540e\u8f93\u5165\u201ccmd\u201d\u6765\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u3002\u5728MacOS\u548cLinux\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u201c\u7ec8\u7aef\u201d\u5e94\u7528\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u822a\u5230Python\u6587\u4ef6\u6240\u5728\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u201ccd\u201d\u547d\u4ee4\u6765\u5bfc\u822a\u5230\u5b58\u50a8Python\u6587\u4ef6\u7684\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6587\u4ef6\u4f4d\u4e8e\u684c\u9762\u7684\u201cPythonScripts\u201d\u6587\u4ef6\u5939\u4e2d\uff0c\u53ef\u4ee5\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd Desktop\/PythonScripts<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Python\u89e3\u91ca\u5668\u8fd0\u884c\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fd0\u884cPython\u6587\u4ef6\uff0c\u5176\u4e2d\u201cscript.py\u201d\u662f\u4f60\u8981\u6267\u884c\u7684Python\u6587\u4ef6\u7684\u540d\u79f0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u540c\u65f6\u5b89\u88c5\u4e86Python 2.x\u548c3.x\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u201cpython3\u201d\u547d\u4ee4\u6765\u660e\u786e\u6307\u5b9a\u4f7f\u7528Python 3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684IDE<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u8bb8\u591aIDE\u53ef\u4f9b\u9009\u62e9\uff0c\u5982PyCharm\u3001Visual Studio Code\u3001Jupyter Notebook\u548cSpyder\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u53cb\u597d\u7684\u7528\u6237\u754c\u9762\uff0c\u4fbf\u4e8e\u7f16\u5199\u548c\u8c03\u8bd5Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5f00Python\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728IDE\u4e2d\u6253\u5f00\u4f60\u60f3\u8981\u8fd0\u884c\u7684Python\u6587\u4ef6\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u201c\u6587\u4ef6\u201d\u83dc\u5355\u4e2d\u7684\u201c\u6253\u5f00\u201d\u9009\u9879\u6765\u627e\u5230\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570IDE\u90fd\u6709\u4e00\u4e2a\u201c\u8fd0\u884c\u201d\u6309\u94ae\uff0c\u901a\u5e38\u6807\u8bb0\u4e3a\u7eff\u8272\u7684\u201c\u64ad\u653e\u201d\u56fe\u6807\u3002\u70b9\u51fb\u8fd9\u4e2a\u6309\u94ae\u5373\u53ef\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u53cc\u51fb\u6587\u4ef6\u6267\u884c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fdd\u6587\u4ef6\u5173\u8054<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u5982\u679c\u5df2\u6b63\u786e\u5b89\u88c5Python\uff0cPython\u6587\u4ef6\uff08.py\uff09\u5e94\u8be5\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u8054\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u53cc\u51fb.py\u6587\u4ef6\u6765\u8fd0\u884c\u5b83\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u811a\u672c\uff0c\u4f46\u5982\u679c\u811a\u672c\u9700\u8981\u8f93\u5165\u6216\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7a97\u53e3\u53ef\u80fd\u4f1a\u7acb\u5373\u5173\u95ed\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u7ed3\u5408\u4f7f\u7528\u201cinput()\u201d\u51fd\u6570\u6765\u6682\u505c\u7a0b\u5e8f\uff0c\u76f4\u5230\u7528\u6237\u6309\u4e0b\u4efb\u610f\u952e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08Linux\/MacOS\uff09<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0shebang\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff0c\u4ee5\u6307\u793a\u7cfb\u7edf\u4f7f\u7528Python\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u8be5\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fee\u6539\u6587\u4ef6\u6743\u9650<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u6587\u4ef6\u6210\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f4\u63a5\u6267\u884c\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5b9a\u65f6\u4efb\u52a1\u6216\u811a\u672c<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528cron\uff08Linux\/MacOS\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ecron\u4efb\u52a1\u6765\u5b9a\u671f\u6267\u884cPython\u811a\u672c\u3002\u9996\u5148\uff0c\u901a\u8fc7\u547d\u4ee4\u201ccrontab -e\u201d\u6253\u5f00cron\u7f16\u8f91\u5668\uff0c\u7136\u540e\u6dfb\u52a0\u4efb\u52a1\uff0c\u4f8b\u5982\u6bcf\u5c0f\u65f6\u8fd0\u884c\u4e00\u6b21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 * * * * \/usr\/bin\/python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff08Windows\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u6765\u5b89\u6392\u811a\u672c\u7684\u6267\u884c\u3002\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u57fa\u672c\u4efb\u52a1\uff0c\u6309\u7167\u5411\u5bfc\u8bbe\u7f6e\u65f6\u95f4\u548c\u811a\u672c\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6267\u884cPython\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u73af\u5883\u800c\u5b9a\u3002\u65e0\u8bba\u662f\u5f00\u53d1\u3001\u6d4b\u8bd5\u8fd8\u662f\u90e8\u7f72\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u90fd\u80fd\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u5f00\u53d1\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884cPython\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u8fd0\u884cPython\u6587\u4ef6\u3002\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u5bfc\u822a\u5230\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c\u8f93\u5165<code>python \u6587\u4ef6\u540d.py<\/code>\u5373\u53ef\u6267\u884c\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fMac\u6216Linux\uff0c\u53ea\u9700\u6253\u5f00\u7ec8\u7aef\uff0c\u8fdb\u5165\u6587\u4ef6\u5939\u5e76\u8f93\u5165<code>python3 \u6587\u4ef6\u540d.py<\/code>\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u73af\u5883\u5e76\u6b63\u786e\u8bbe\u7f6e\u4e86\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p><strong>\u8fd0\u884cPython\u6587\u4ef6\u9700\u8981\u4ec0\u4e48\u6837\u7684\u73af\u5883\u914d\u7f6e\uff1f<\/strong><br \/>\u4e3a\u4e86\u987a\u5229\u8fd0\u884cPython\u6587\u4ef6\uff0c\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86Python\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eePython\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\u6765\u786e\u8ba4\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216VSCode\u6765\u7b80\u5316\u5f00\u53d1\u548c\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n<p><strong>Python\u6587\u4ef6\u4e2d\u5982\u4f55\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u7ed3\u679c\u3002\u901a\u8fc7\u8fd9\u4e9b\u57fa\u7840\u7684\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\uff0c\u5904\u7406\u6570\u636e\u3002\u5728\u6587\u4ef6\u4e2d\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u4ee5\u4fbf\u4e8e\u7528\u6237\u7406\u89e3\u8f93\u51fa\u5185\u5bb9\uff0c\u6bd4\u5982\u4f7f\u7528f-string\u6216<code>format()<\/code>\u65b9\u6cd5\u63d0\u5347\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Python\u6587\u4ef6\u6267\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u3001\u53cc\u51fb\u6587\u4ef6\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7b49\u65b9\u6cd5\u3002\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1008834,"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\/1008825"}],"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=1008825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008825\/revisions"}],"predecessor-version":[{"id":1008837,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008825\/revisions\/1008837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008834"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}