{"id":1138547,"date":"2025-01-08T22:01:00","date_gmt":"2025-01-08T14:01:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138547.html"},"modified":"2025-01-08T22:01:03","modified_gmt":"2025-01-08T14:01:03","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%ad%e6%89%a7%e8%a1%8cpython%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138547.html","title":{"rendered":"\u5982\u4f55\u5728\u670d\u52a1\u5668\u4e2d\u6267\u884cpython\u6587\u4ef6\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102126\/e49d5142-59d6-4d04-988d-3c02d057c600.webp\" alt=\"\u5982\u4f55\u5728\u670d\u52a1\u5668\u4e2d\u6267\u884cpython\u6587\u4ef6\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>\u5728\u670d\u52a1\u5668\u4e2d\u6267\u884cPython\u6587\u4ef6\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8fde\u63a5\u670d\u52a1\u5668\u3001\u8bbe\u7f6e\u73af\u5883\u3001\u4e0a\u4f20\u6587\u4ef6\u3001\u6267\u884c\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c\u6b63\u786e\u8bbe\u7f6ePython\u73af\u5883\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u4f60\u7684\u4ee3\u7801\u80fd\u5426\u5728\u670d\u52a1\u5668\u4e0a\u6b63\u786e\u8fd0\u884c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u4e2a\u6b65\u9aa4\uff0c\u786e\u4fdd\u4f60\u80fd\u591f\u5728\u670d\u52a1\u5668\u4e2d\u6d41\u7545\u5730\u6267\u884cPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u8fde\u63a5\u670d\u52a1\u5668<\/h2>\n<\/p>\n<p><p>\u5728\u6267\u884cPython\u6587\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8fde\u63a5\u5230\u4f60\u7684\u670d\u52a1\u5668\u3002\u901a\u5e38\u6709\u51e0\u79cd\u65b9\u6cd5\u6765\u8fde\u63a5\uff1a<\/p>\n<\/p>\n<p><h3>1.1 \u4f7f\u7528SSH\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>SSH\uff08Secure Shell\uff09\u662f\u4e00\u79cd\u5b89\u5168\u534f\u8bae\uff0c\u7528\u4e8e\u5728\u4e0d\u5b89\u5168\u7684\u7f51\u7edc\u4e2d\u5b89\u5168\u5730\u8bbf\u95ee\u8fdc\u7a0b\u670d\u52a1\u5668\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fde\u63a5\u5230\u670d\u52a1\u5668\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>\u5728\u8fde\u63a5\u65f6\uff0c\u4f60\u9700\u8981\u8f93\u5165\u7528\u6237\u540d\u548c\u670d\u52a1\u5668\u7684IP\u5730\u5740\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u7aef\u53e3\uff0822\uff09\uff0c\u5219\u65e0\u9700\u6307\u5b9a\u7aef\u53e3\u53f7\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u4f7f\u7528\u56fe\u5f62\u754c\u9762\u8fde\u63a5\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u56fe\u5f62\u754c\u9762\u7684SSH\u8fde\u63a5\u5de5\u5177\uff0c\u5982PuTTY\u3001MobaXterm\u7b49\u3002\u4f60\u53ea\u9700\u8f93\u5165\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7528\u6237\u540d\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>1.3 \u4f7f\u7528\u8fdc\u7a0b\u684c\u9762\u534f\u8bae\uff08RDP\uff09<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u670d\u52a1\u5668\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fdc\u7a0b\u684c\u9762\u534f\u8bae\uff08RDP\uff09\u8fde\u63a5\u3002\u8f93\u5165\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff0c\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5373\u53ef\u8fde\u63a5\u5230\u670d\u52a1\u5668\u684c\u9762\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u8bbe\u7f6e\u73af\u5883<\/h2>\n<\/p>\n<p><p>\u5728\u670d\u52a1\u5668\u4e0a\u6267\u884cPython\u6587\u4ef6\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u6b63\u786e\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u5b89\u88c5Python<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u670d\u52a1\u5668\u4e0a\u6ca1\u6709\u5b89\u88c5Python\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u6765\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Python\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\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum update<\/p>\n<p>sudo yum install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u5b89\u88c5\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\uff0c\u5efa\u8bae\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884cPython\u6587\u4ef6\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt install python3-venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u5e76\u6fc0\u6d3b\u865a\u62df\u73af\u5883\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><h3>2.3 \u5b89\u88c5\u4f9d\u8d56\u5305<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684Python\u6587\u4ef6\u6709\u4f9d\u8d56\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4e0a\u4f20\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5c06Python\u6587\u4ef6\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u6765\u5b8c\u6210\u6587\u4ef6\u4e0a\u4f20\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u4f7f\u7528SCP\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>SCP\uff08Secure Copy\uff09\u662f\u4e00\u4e2a\u57fa\u4e8eSSH\u534f\u8bae\u7684\u6587\u4ef6\u4f20\u8f93\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e0a\u4f20\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scp localfile.py username@server_ip:\/path\/to\/destination<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u4f7f\u7528SFTP\u5de5\u5177<\/h3>\n<\/p>\n<p><p>SFTP\uff08SSH File Transfer Protocol\uff09\u662f\u4e00\u79cd\u5b89\u5168\u7684\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528FileZilla\u7b49\u5de5\u5177\u5c06\u6587\u4ef6\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h3>3.3 \u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528Git\u7b49\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u5c06\u4ee3\u7801\u63a8\u9001\u5230\u670d\u52a1\u5668\u4e0a\u7684\u4ee3\u7801\u4ed3\u5e93\uff0c\u7136\u540e\u5728\u670d\u52a1\u5668\u4e0a\u8fdb\u884c\u514b\u9686\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6267\u884c\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u4e0a\u6267\u884cPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u76f4\u63a5\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u76f4\u63a5\u6267\u884cPython\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 myfile.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528nohup\u547d\u4ee4\u540e\u53f0\u6267\u884c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u7a0b\u5e8f\u5728\u540e\u53f0\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>nohup<\/code>\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nohup python3 myfile.py &amp;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3 \u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u5b9a\u671f\u6267\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>crontab<\/code>\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><p>\u7f16\u8f91<code>crontab<\/code>\u6587\u4ef6\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\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 * * * * \/path\/to\/python3 \/path\/to\/myfile.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u914d\u7f6e\u4f1a\u6bcf\u5c0f\u65f6\u6267\u884c\u4e00\u6b21Python\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>4.4 \u4f7f\u7528\u7cfb\u7edf\u670d\u52a1<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u957f\u671f\u8fd0\u884c\u7684Python\u670d\u52a1\uff0c\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7cfb\u7edf\u670d\u52a1\u3002\u5728<code>\/etc\/systemd\/system\/<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6587\u4ef6\uff0c\u5982<code>myservice.service<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Service<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/path\/to\/python3 \/path\/to\/myfile.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>\u542f\u7528\u5e76\u542f\u52a8\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl enable myservice<\/p>\n<p>sudo systemctl start myservice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u4e2d\u987a\u5229\u5730\u6267\u884cPython\u6587\u4ef6\u3002\u786e\u4fdd\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u6b63\u786e\u5b8c\u6210\uff0c\u7279\u522b\u662f\u5728\u914d\u7f6ePython\u73af\u5883\u65f6\u8981\u6ce8\u610f\u7248\u672c\u548c\u4f9d\u8d56\u5305\u7684\u517c\u5bb9\u6027\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u5728\u670d\u52a1\u5668\u4e2d\u6d41\u7545\u5730\u6267\u884cPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884cPython\u811a\u672c\u7684\u6b65\u9aa4\u4e3b\u8981\u5305\u62ec\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3001\u786e\u4fddPython\u73af\u5883\u5df2\u5b89\u88c5\u3001\u4e0a\u4f20\u6216\u521b\u5efaPython\u6587\u4ef6\uff0c\u4ee5\u53ca\u4f7f\u7528\u547d\u4ee4\u884c\u6267\u884c\u8be5\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7SSH\u8fde\u63a5\u5230\u670d\u52a1\u5668\uff0c\u4f7f\u7528scp\u6216FTP\u5de5\u5177\u4e0a\u4f20\u6587\u4ef6\uff0c\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python your_script.py<\/code>\u6765\u6267\u884c\u3002<\/p>\n<p><strong>\u5728\u670d\u52a1\u5668\u4e2d\u5982\u4f55\u5b89\u88c5Python\uff1f<\/strong><br \/>\u5982\u679c\u670d\u52a1\u5668\u672a\u9884\u88c5Python\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5de5\u5177\u5b89\u88c5\u3002\u5728Ubuntu\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>sudo apt-get install python3<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u5728CentOS\u4e2d\uff0c\u4f7f\u7528<code>sudo yum install python3<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>python3 --version<\/code>\u786e\u8ba4\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u7684Python\u811a\u672c\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528print\u8bed\u53e5\u8f93\u51fa\u53d8\u91cf\u72b6\u6001\uff0c\u6216\u8005\u4f7f\u7528Python\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177pdb\u3002\u901a\u8fc7\u5728\u811a\u672c\u4e2d\u52a0\u5165<code>import pdb; pdb.set_trace()<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\uff0c\u4fbf\u4e8e\u9010\u6b65\u6267\u884c\u548c\u68c0\u67e5\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fdc\u7a0b\u8c03\u8bd5\u5de5\u5177\u5982Visual Studio Code\u7684\u8fdc\u7a0b\u5f00\u53d1\u63d2\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u670d\u52a1\u5668\u4e2d\u6267\u884cPython\u6587\u4ef6\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u8fde\u63a5\u670d\u52a1\u5668\u3001\u8bbe\u7f6e\u73af\u5883\u3001\u4e0a\u4f20\u6587\u4ef6\u3001\u6267\u884c\u6587\u4ef6\u3002 \u5176\u4e2d\uff0c\u6b63\u786e\u8bbe\u7f6ePytho [&hellip;]","protected":false},"author":3,"featured_media":1138554,"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\/1138547"}],"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=1138547"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138547\/revisions"}],"predecessor-version":[{"id":1138555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138547\/revisions\/1138555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}