{"id":964866,"date":"2024-12-27T04:32:31","date_gmt":"2024-12-26T20:32:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964866.html"},"modified":"2024-12-27T04:32:32","modified_gmt":"2024-12-26T20:32:32","slug":"%e6%a0%91%e8%8e%93%e6%b4%be%e5%a6%82%e4%bd%95python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964866.html","title":{"rendered":"\u6811\u8393\u6d3e\u5982\u4f55python\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181437\/ef4e958b-3153-469e-b0e0-5cd68ac97b4b.webp\" alt=\"\u6811\u8393\u6d3e\u5982\u4f55python\u811a\u672c\" \/><\/p>\n<p><p> <strong>\u6811\u8393\u6d3e\u8fd0\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1\u3001\u901a\u8fc7SSH\u8fdc\u7a0b\u6267\u884c\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884cPython\u811a\u672c\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u8c03\u8bd5\u548c\u5feb\u901f\u6d4b\u8bd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c<\/p>\n<\/p>\n<p><p>\u5728\u6811\u8393\u6d3e\u4e0a\u8fd0\u884cPython\u811a\u672c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u8c03\u8bd5\u548c\u4fee\u6539\u7684\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u6811\u8393\u6d3e\u9ed8\u8ba4\u5b89\u88c5\u4e86Python\u73af\u5883\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u786e\u8ba4\u662f\u5426\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p>python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u82e5\u672a\u5b89\u88c5\u6216\u9700\u8981\u66f4\u65b0\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt update<\/p>\n<p>sudo apt install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Nano\uff09\u7f16\u5199Python\u811a\u672c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>script.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nano script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u5165\u4ee5\u4e0b\u7b80\u5355\u7684Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, Raspberry Pi!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u5e76\u9000\u51fa\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u201cHello, Raspberry Pi!\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7fPython\u811a\u672c\u66f4\u5bb9\u6613\u8fd0\u884c\uff0c\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ece\u800c\u65e0\u9700\u6bcf\u6b21\u90fd\u8f93\u5165\u201cpython3\u201d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0shebang\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0\u4ee5\u4e0bshebang\u884c\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>\u66f4\u6539\u6587\u4ef6\u6743\u9650<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528chmod\u547d\u4ee4\u5c06\u811a\u672c\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\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>\u8fd0\u884c\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u76f4\u63a5\u8fd0\u884c\u811a\u672c\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>\u4e09\u3001\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1<\/p>\n<\/p>\n<p><p>Cron\u662fUnix\/Linux\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u7279\u5b9a\u65f6\u95f4\u81ea\u52a8\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u8f91crontab<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6253\u5f00crontab\u7f16\u8f91\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u4efb\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u5728crontab\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c\u4ee5\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u6bcf\u5929\u51cc\u66681\u70b9\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 1 * * * \/usr\/bin\/python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u5e76\u9000\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u4fdd\u5b58\u66f4\u6539\u5e76\u9000\u51fa\u7f16\u8f91\u5668\uff0ccron\u5c06\u6839\u636e\u8bbe\u5b9a\u7684\u65f6\u95f4\u81ea\u52a8\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u901a\u8fc7SSH\u8fdc\u7a0b\u6267\u884c<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8fdc\u7a0b\u7ba1\u7406\u6811\u8393\u6d3e\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u901a\u8fc7SSH\u8fde\u63a5\u5e76\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u542f\u7528SSH<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6811\u8393\u6d3e\u4e0a\u542f\u7528SSH\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo raspi-config<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bfc\u822a\u5230\u201cInterface Options\u201d &gt; \u201cSSH\u201d\uff0c\u9009\u62e9\u201cEnable\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u6811\u8393\u6d3e<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528SSH\u8fde\u63a5\u6811\u8393\u6d3e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ssh pi@raspberrypi.local<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u5165\u5bc6\u7801\u540e\u5373\u53ef\u767b\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884cPython\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u6811\u8393\u6d3e\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f7f\u7528\u573a\u666f\u548c\u4e2a\u4eba\u4e60\u60ef\u3002\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u76f4\u63a5\u8fd0\u884c\u662f\u6700\u7b80\u4fbf\u7684\u65b9\u5f0f\u3002\u800c\u5728\u9700\u8981\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u65f6\uff0ccron\u5219\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u8bbe\u7f6e\u811a\u672c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u4ee5\u7b80\u5316\u6267\u884c\u8fc7\u7a0b\uff0c\u800c\u901a\u8fc7SSH\u8fdc\u7a0b\u6267\u884c\u5219\u9002\u7528\u4e8e\u8fdc\u7a0b\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fddPython\u73af\u5883\u914d\u7f6e\u6b63\u786e\uff0c\u811a\u672c\u8def\u5f84\u51c6\u786e\u65e0\u8bef\uff0c\u5e76\u6839\u636e\u9700\u8981\u8c03\u6574\u6587\u4ef6\u6743\u9650\u3002\u5bf9\u4e8e\u6d89\u53ca\u5916\u90e8\u786c\u4ef6\u6216\u7f51\u7edc\u7684\u811a\u672c\uff0c\u9700\u8981\u63d0\u524d\u914d\u7f6e\u597d\u76f8\u5173\u73af\u5883\u548c\u6743\u9650\uff0c\u4ee5\u786e\u4fdd\u811a\u672c\u80fd\u591f\u987a\u5229\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u6811\u8393\u6d3e\u4e0a\u5b89\u88c5Python\u73af\u5883\uff1f<\/strong><br \/>\u5728\u6811\u8393\u6d3e\u4e0a\u5b89\u88c5Python\u73af\u5883\u975e\u5e38\u7b80\u5355\u3002\u6811\u8393\u6d3e\u901a\u5e38\u9884\u88c5\u4e86Python\uff0c\u4f46\u5982\u679c\u9700\u8981\u66f4\u65b0\u6216\u5b89\u88c5\u7279\u5b9a\u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-bash\">sudo apt update  \nsudo apt install python3  \n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5b89\u88c5pip\uff0c\u8fd9\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u4f7f\u7528\u547d\u4ee4\uff1a  <\/p>\n<pre><code class=\"language-bash\">sudo apt install python3-pip  \n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>python3 --version<\/code>\u6765\u68c0\u67e5\u7248\u672c\uff0c\u786e\u4fdd\u73af\u5883\u914d\u7f6e\u6210\u529f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6811\u8393\u6d3e\u4e0a\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8fd0\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7ec8\u7aef\u8fdb\u5165\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\uff1a  <\/p>\n<pre><code class=\"language-bash\">python3 your_script.py  \n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>your_script.py<\/code>\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u811a\u672c\u540d\u79f0\u3002\u5982\u679c\u811a\u672c\u9700\u8981\u5b9a\u671f\u6267\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1\u6765\u81ea\u52a8\u5316\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u6811\u8393\u6d3e\u4e0a\u7684Python\u811a\u672c\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u811a\u672c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print()<\/code>\u8bed\u53e5\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u8c03\u8bd5\u5668<code>pdb<\/code>\uff0c\u901a\u8fc7\u5728\u811a\u672c\u4e2d\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\u6765\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u66f4\u52a0\u7ec6\u81f4\u5730\u67e5\u627e\u95ee\u9898\u3002\u5bf9\u4e8e\u56fe\u5f62\u5316\u754c\u9762\u7684\u8c03\u8bd5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Thonny\u7b49IDE\uff0c\u5b83\u63d0\u4f9b\u4e86\u7528\u6237\u53cb\u597d\u7684\u8c03\u8bd5\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6811\u8393\u6d3e\u8fd0\u884cPython\u811a\u672c\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u3001\u8bbe\u7f6e\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1\u3001\u901a\u8fc7SSH\u8fdc [&hellip;]","protected":false},"author":3,"featured_media":964875,"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\/964866"}],"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=964866"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964866\/revisions"}],"predecessor-version":[{"id":964876,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964866\/revisions\/964876"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964875"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}