{"id":1084655,"date":"2025-01-08T13:09:14","date_gmt":"2025-01-08T05:09:14","guid":{"rendered":""},"modified":"2025-01-08T13:09:17","modified_gmt":"2025-01-08T05:09:17","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8a%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8cpython%e8%84%9a%e6%9c%ac-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084655.html","title":{"rendered":"\u670d\u52a1\u5668\u4e0a\u5982\u4f55\u8fd0\u884cpython\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194917\/caa34ee9-170b-44da-92cd-20a7a364926c.webp\" alt=\"\u670d\u52a1\u5668\u4e0a\u5982\u4f55\u8fd0\u884cpython\u811a\u672c\" \/><\/p>\n<p><p> \u4e00\u3001\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u8fde\u63a5\u670d\u52a1\u5668\u3001\u5b89\u88c5Python\u73af\u5883\u3001\u4e0a\u4f20\u811a\u672c\u6587\u4ef6\u3001\u8fd0\u884c\u811a\u672c\u3001\u76d1\u63a7\u548c\u8c03\u8bd5<\/strong>\u3002\u5176\u4e2d\uff0c\u8fde\u63a5\u670d\u52a1\u5668\u662f\u6700\u57fa\u7840\u7684\u4e00\u6b65\uff0c\u901a\u8fc7SSH\u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u786e\u4fdd\u4f60\u6709\u6743\u9650\u64cd\u4f5c\u670d\u52a1\u5668\u4e0a\u7684\u6587\u4ef6\u548c\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u8fde\u63a5\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528SSH\uff08Secure Shell\uff09\u534f\u8bae\u3002SSH\u662f\u4e00\u79cd\u52a0\u5bc6\u7684\u7f51\u7edc\u534f\u8bae\uff0c\u7528\u4e8e\u5728\u4e0d\u5b89\u5168\u7684\u7f51\u7edc\u4e2d\u5b89\u5168\u5730\u64cd\u4f5c\u8fdc\u7a0b\u8ba1\u7b97\u673a\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u77e5\u9053\u670d\u52a1\u5668\u7684IP\u5730\u5740\u6216\u57df\u540d\uff0c\u4ee5\u53ca\u767b\u5f55\u6240\u9700\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff08\u6216\u8005\u5bc6\u94a5\u6587\u4ef6\uff09\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PuTTY\u7b49SSH\u5ba2\u6237\u7aef\u8f6f\u4ef6\u6765\u8fdb\u884c\u8fde\u63a5\uff0c\u800c\u5728Unix\/Linux\u548cMacOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7ec8\u7aef\uff08Terminal\uff09\u4e2d\u7684<code>ssh<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8fde\u63a5\u670d\u52a1\u5668<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528SSH\u8fde\u63a5<\/li>\n<\/ol>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PuTTY\u7b49SSH\u5ba2\u6237\u7aef\u8f6f\u4ef6\u6765\u8fde\u63a5\u670d\u52a1\u5668\u3002\u9996\u5148\uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5PuTTY\uff0c\u7136\u540e\u6253\u5f00PuTTY\uff0c\u8f93\u5165\u670d\u52a1\u5668\u7684IP\u5730\u5740\u6216\u57df\u540d\uff0c\u70b9\u51fb\u201cOpen\u201d\u6309\u94ae\uff0c\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5373\u53ef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002\u5728Unix\/Linux\u548cMacOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7ec8\u7aef\uff08Terminal\uff09\u4e2d\u7684<code>ssh<\/code>\u547d\u4ee4\u8fdb\u884c\u8fde\u63a5\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ssh username@server_ip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u5165\u5bc6\u7801\u540e\uff0c\u5373\u53ef\u6210\u529f\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5bc6\u94a5\u6587\u4ef6\u8fde\u63a5<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u670d\u52a1\u5668\u4f7f\u7528SSH\u5bc6\u94a5\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u5bc6\u94a5\u6587\u4ef6\u8fdb\u884c\u8fde\u63a5\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5728PuTTY\u7684\u201cSSH\u201d\u9009\u9879\u4e2d\u9009\u62e9\u201cAuth\u201d\uff0c\u7136\u540e\u5728\u201cPrivate key file for authentication\u201d\u4e2d\u9009\u62e9\u5bc6\u94a5\u6587\u4ef6\u3002\u5728Unix\/Linux\u548cMacOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>-i<\/code>\u9009\u9879\u6307\u5b9a\u5bc6\u94a5\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ssh -i \/path\/to\/keyfile username@server_ip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b89\u88c5Python\u73af\u5883<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5Python\u7248\u672c<\/li>\n<\/ol>\n<p><p>\u8fde\u63a5\u5230\u670d\u52a1\u5668\u540e\uff0c\u9996\u5148\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u4ee5\u53ca\u5b89\u88c5\u7684\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u4e14\u7248\u672c\u7b26\u5408\u8981\u6c42\uff0c\u53ef\u4ee5\u8df3\u8fc7\u5b89\u88c5\u6b65\u9aa4\uff0c\u76f4\u63a5\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002\u5982\u679c\u672a\u5b89\u88c5Python\uff0c\u6216\u8005\u7248\u672c\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u5219\u9700\u8981\u5b89\u88c5\u6216\u5347\u7ea7Python\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5Python<\/li>\n<\/ol>\n<p><p>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982apt\u3001yum\u7b49\uff09\u5b89\u88c5Python\u3002\u4f8b\u5982\uff0c\u5728Debian\/Ubuntu\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Python 3\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<p><p>\u5728CentOS\/RHEL\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Python 3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5b89\u88c5pip<\/li>\n<\/ol>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u5927\u591a\u6570Python\u5b89\u88c5\u5305\u5df2\u7ecf\u5305\u542bpip\uff0c\u5982\u679c\u6ca1\u6709\u5b89\u88c5pip\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt install python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum install python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4e0a\u4f20\u811a\u672c\u6587\u4ef6<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528SCP\u4e0a\u4f20\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528SCP\uff08Secure Copy Protocol\uff09\u5c06\u672c\u5730\u7684Python\u811a\u672c\u6587\u4ef6\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u3002\u5728Unix\/Linux\u548cMacOS\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7ec8\u7aef\u4e2d\u7684<code>scp<\/code>\u547d\u4ee4\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scp \/path\/to\/local_script.py username@server_ip:\/path\/to\/remote_directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528WinSCP\u7b49\u5de5\u5177\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528SFTP\u4e0a\u4f20\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>SFTP\uff08SSH File Transfer Protocol\uff09\u662f\u4e00\u79cd\u901a\u8fc7SSH\u4f20\u8f93\u6587\u4ef6\u7684\u534f\u8bae\u3002\u53ef\u4ee5\u4f7f\u7528SFTP\u5ba2\u6237\u7aef\u8f6f\u4ef6\uff08\u5982WinSCP\u3001FileZilla\u7b49\uff09\u5c06\u672c\u5730\u7684Python\u811a\u672c\u6587\u4ef6\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8fd0\u884c\u811a\u672c<\/p>\n<\/p>\n<ol>\n<li>\u8fdb\u5165\u811a\u672c\u76ee\u5f55<\/li>\n<\/ol>\n<p><p>\u5728\u8fde\u63a5\u5230\u670d\u52a1\u5668\u5e76\u4e0a\u4f20\u811a\u672c\u6587\u4ef6\u540e\uff0c\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u8fdb\u5165\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd \/path\/to\/remote_directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8fd0\u884cPython\u811a\u672c<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cPython\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>\u5982\u679c\u811a\u672c\u9700\u8981\u4f20\u9012\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u540e\u6dfb\u52a0\u53c2\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 script.py arg1 arg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u76d1\u63a7\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<ol>\n<li>\u67e5\u770b\u811a\u672c\u8f93\u51fa<\/li>\n<\/ol>\n<p><p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u7ec8\u7aef\u4e2d\u67e5\u770b\u811a\u672c\u7684\u8f93\u51fa\u3002\u5982\u679c\u811a\u672c\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u5efa\u8bae\u4f7f\u7528<code>nohup<\/code>\u547d\u4ee4\u6216<code>screen<\/code>\u5de5\u5177\u6765\u4fdd\u6301\u811a\u672c\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u5e76\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nohup python3 script.py &gt; output.log 2&gt;&amp;1 &amp;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8c03\u8bd5\u811a\u672c<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u811a\u672c\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u53ef\u4ee5\u67e5\u770b\u8f93\u51fa\u65e5\u5fd7\u6216\u9519\u8bef\u4fe1\u606f\uff0c\u5b9a\u4f4d\u95ee\u9898\u5e76\u8fdb\u884c\u8c03\u8bd5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u8c03\u8bd5\u5de5\u5177\uff08\u5982pdb\uff09\u8fdb\u884c\u8c03\u8bd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 -m pdb script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/li>\n<\/ol>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff0c\u65b9\u4fbf\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO, filename=&#39;script.log&#39;, filemode=&#39;w&#39;, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>logging.info(&#39;This is an info message&#39;)<\/p>\n<p>logging.error(&#39;This is an error message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bb0\u5f55\u65e5\u5fd7\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u76d1\u63a7\u811a\u672c\u8fd0\u884c\u60c5\u51b5\uff0c\u5b9a\u4f4d\u95ee\u9898\u5e76\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f18\u5316\u811a\u672c\u6027\u80fd<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u865a\u62df\u73af\u5883<\/li>\n<\/ol>\n<p><p>\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08virtual environment\uff09\u7ba1\u7406\u9879\u76ee\u4f9d\u8d56\u3002\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\uff0c\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 -m venv myenv<\/p>\n<p>source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u6240\u9700\u4f9d\u8d56\u5305\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f18\u5316\u4ee3\u7801\u6027\u80fd<\/li>\n<\/ol>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u4ee3\u7801\u6027\u80fd\uff0c\u63d0\u9ad8\u811a\u672c\u8fd0\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u548c\u8ba1\u7b97\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u7f13\u5b58<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u91cd\u590d\u8ba1\u7b97\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6280\u672f\u63d0\u9ad8\u811a\u672c\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\u7f13\u5b58\u51fd\u6570\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li>\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b\u63d0\u9ad8\u811a\u672c\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u5b9e\u73b0\u5f02\u6b65\u4efb\u52a1\u8c03\u5ea6\uff0c\u63d0\u9ad8I\/O\u64cd\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u81ea\u52a8\u5316\u8fd0\u884c<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Cron\u5b9a\u65f6\u4efb\u52a1<\/li>\n<\/ol>\n<p><p>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Cron\u5b9a\u65f6\u4efb\u52a1\u81ea\u52a8\u5316\u8fd0\u884cPython\u811a\u672c\u3002\u53ef\u4ee5\u7f16\u8f91Crontab\u6587\u4ef6\uff0c\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff0c\u8868\u793a\u6bcf\u96941\u5c0f\u65f6\u8fd0\u884c\u4e00\u6b21\u811a\u672c\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<ol start=\"2\">\n<li>\u4f7f\u7528Systemd\u670d\u52a1<\/li>\n<\/ol>\n<p><p>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Systemd\u670d\u52a1\u7ba1\u7406Python\u811a\u672c\u7684\u81ea\u52a8\u5316\u8fd0\u884c\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aSystemd\u670d\u52a1\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Script<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/script.py<\/p>\n<p>Restart=always<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=multi-user.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u670d\u52a1\u6587\u4ef6\u4fdd\u5b58\u5230<code>\/etc\/systemd\/system\/<\/code>\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u5e76\u542f\u7528\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl start myscript.service<\/p>\n<p>sudo systemctl enable myscript.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f<\/li>\n<\/ol>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\uff08Task Scheduler\uff09\u81ea\u52a8\u5316\u8fd0\u884cPython\u811a\u672c\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4efb\u52a1\uff0c\u8bbe\u7f6e\u89e6\u53d1\u5668\u548c\u64cd\u4f5c\uff0c\u6307\u5b9aPython\u53ef\u6267\u884c\u6587\u4ef6\u548c\u811a\u672c\u6587\u4ef6\u8def\u5f84\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u4e0a\u6210\u529f\u8fd0\u884cPython\u811a\u672c\uff0c\u5e76\u5b9e\u73b0\u81ea\u52a8\u5316\u7ba1\u7406\u548c\u4f18\u5316\u811a\u672c\u6027\u80fd\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\uff0c\u786e\u4fdd\u811a\u672c\u8fd0\u884c\u7a33\u5b9a\u3001\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5Python\u73af\u5883\uff1f<\/strong><br \/>\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u7b2c\u4e00\u6b65\u662f\u786e\u4fdd\u5b89\u88c5\u4e86Python\u73af\u5883\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5305\u7ba1\u7406\u5668\uff08\u5982apt\u3001yum\u7b49\uff09\u6765\u5b89\u88c5Python\u3002\u5bf9\u4e8eUbuntu\u7528\u6237\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>sudo apt-get install python3<\/code>\u6765\u5b89\u88c5Python3\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python3 --version<\/code>\u547d\u4ee4\u786e\u8ba4\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7SSH\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u5e76\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u9700\u8981\u901a\u8fc7SSH\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002\u4f7f\u7528\u547d\u4ee4<code>ssh username@server_ip<\/code>\u8fdb\u884c\u8fde\u63a5\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5bfc\u822a\u5230\u5b58\u653ePython\u811a\u672c\u7684\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7\u547d\u4ee4<code>python3 script.py<\/code>\u6765\u8fd0\u884c\u811a\u672c\uff0c\u66ff\u6362\u201cscript.py\u201d\u4e3a\u4f60\u7684\u811a\u672c\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u65f6\u5982\u4f55\u5904\u7406\u4f9d\u8d56\u5e93\uff1f<\/strong><br \/>\u5728\u8fd0\u884cPython\u811a\u672c\u4e4b\u524d\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u5e93\u90fd\u5df2\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56\uff0c\u4f7f\u7528\u547d\u4ee4<code>python3 -m venv myenv<\/code>\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u7136\u540e\u4f7f\u7528<code>source myenv\/bin\/activate<\/code>\u6fc0\u6d3b\u5b83\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>pip install -r requirements.txt<\/code>\u547d\u4ee4\u6839\u636e\u9700\u6c42\u6587\u4ef6\u5b89\u88c5\u6240\u9700\u5e93\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4e0e\u5176\u4ed6\u9879\u76ee\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8fde\u63a5\u670d\u52a1\u5668\u3001\u5b89\u88c5Python\u73af\u5883\u3001\u4e0a\u4f20\u811a\u672c\u6587\u4ef6\u3001\u8fd0\u884c\u811a\u672c\u3001\u76d1\u63a7\u548c\u8c03 [&hellip;]","protected":false},"author":3,"featured_media":1084662,"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\/1084655"}],"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=1084655"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084655\/revisions"}],"predecessor-version":[{"id":1084663,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084655\/revisions\/1084663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084662"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}