{"id":971613,"date":"2024-12-27T05:36:38","date_gmt":"2024-12-26T21:36:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971613.html"},"modified":"2024-12-27T05:36:42","modified_gmt":"2024-12-26T21:36:42","slug":"termux%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%bapython%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971613.html","title":{"rendered":"termux\u5982\u4f55\u521b\u5efapython\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194640\/539cdbf1-965b-44cb-a099-e64a7c0c2880.webp\" alt=\"termux\u5982\u4f55\u521b\u5efapython\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Termux\u4e2d\u521b\u5efaPython\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982nano\u3001vim\uff09\u3001Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u4ee5\u53ca\u5916\u90e8\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982\u4f7f\u7528Termux\u4e0ePC\u5171\u4eab\u6587\u4ef6\uff09\u3002\u63a8\u8350\u4f7f\u7528nano\u7f16\u8f91\u5668\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u3001\u76f4\u63a5\u5728Termux\u4e2d\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Termux\u4e2d\uff0c\u521b\u5efaPython\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6839\u636e\u7528\u6237\u7684\u9700\u6c42\u548c\u64cd\u4f5c\u4e60\u60ef\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u5728Termux\u4e2d\uff0c\u547d\u4ee4\u884c\u6587\u672c\u7f16\u8f91\u5668\u662f\u521b\u5efa\u548c\u7f16\u8f91\u6587\u4ef6\u7684\u57fa\u7840\u5de5\u5177\u3002\u5e38\u7528\u7684\u7f16\u8f91\u5668\u5305\u62ecnano\u548cvim\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528nano\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>Nano\u662f\u4e00\u6b3e\u7b80\u5355\u6613\u7528\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5Nano<\/strong>\uff1a\u5728Termux\u4e2d\uff0c\u8f93\u5165\u547d\u4ee4 <code>pkg install nano<\/code> \u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li><strong>\u521b\u5efa\u5e76\u7f16\u8f91\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u547d\u4ee4 <code>nano filename.py<\/code> \u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\u3002\u53ef\u4ee5\u76f4\u63a5\u5728\u7f16\u8f91\u5668\u4e2d\u8f93\u5165Python\u4ee3\u7801\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong>\uff1a\u7f16\u8f91\u5b8c\u6210\u540e\uff0c\u6309 <code>Ctrl + O<\/code> \u4fdd\u5b58\u6587\u4ef6\uff0c\u7136\u540e\u6309 <code>Enter<\/code> \u786e\u8ba4\u3002<\/li>\n<li><strong>\u9000\u51fa\u7f16\u8f91\u5668<\/strong>\uff1a\u6309 <code>Ctrl + X<\/code> \u9000\u51fanano\u7f16\u8f91\u5668\u3002<\/li>\n<\/ul>\n<p><p>Nano\u7f16\u8f91\u5668\u64cd\u4f5c\u7b80\u5355\uff0c\u9002\u5408\u4e0d\u719f\u6089\u547d\u4ee4\u884c\u7684\u7528\u6237\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528vim\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>Vim\u662f\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u8f91\u5668\uff0c\u9002\u5408\u719f\u6089\u547d\u4ee4\u884c\u64cd\u4f5c\u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5Vim<\/strong>\uff1a\u5728Termux\u4e2d\uff0c\u8f93\u5165\u547d\u4ee4 <code>pkg install vim<\/code> \u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li><strong>\u521b\u5efa\u5e76\u7f16\u8f91\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u547d\u4ee4 <code>vim filename.py<\/code> \u521b\u5efa\u5e76\u6253\u5f00\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\u3002\u8fdb\u5165\u7f16\u8f91\u6a21\u5f0f\u540e\uff0c\u53ef\u4ee5\u8f93\u5165Python\u4ee3\u7801\u3002<\/li>\n<li><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong>\uff1a\u6309 <code>Esc<\/code> \u9000\u51fa\u7f16\u8f91\u6a21\u5f0f\uff0c\u7136\u540e\u8f93\u5165 <code>:w<\/code> \u4fdd\u5b58\u6587\u4ef6\u3002<\/li>\n<li><strong>\u9000\u51fa\u7f16\u8f91\u5668<\/strong>\uff1a\u8f93\u5165 <code>:q<\/code> \u9000\u51favim\u7f16\u8f91\u5668\u3002<\/li>\n<\/ul>\n<p><p>Vim\u7f16\u8f91\u5668\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5bf9\u65b0\u7528\u6237\u6765\u8bf4\u53ef\u80fd\u6709\u4e9b\u590d\u6742\uff0c\u5efa\u8bae\u901a\u8fc7\u5b66\u4e60\u57fa\u672c\u547d\u4ee4\u63d0\u9ad8\u4f7f\u7528\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4ea4\u4e92\u5f0fPython\u73af\u5883\u4e2d\u521b\u5efa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00Python\u4ea4\u4e92\u5f0f\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728Termux\u4e2d\uff0c\u8f93\u5165 <code>python<\/code> \u8fdb\u5165Python\u4ea4\u4e92\u5f0f\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528open()\u51fd\u6570\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6<\/p>\n<p>with open(&#39;filename.py&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;# Write your Python code here\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u901a\u8fc7Python\u7a0b\u5e8f\u81ea\u52a8\u751f\u6210\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5916\u90e8\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u66f4\u559c\u6b22\u56fe\u5f62\u5316\u754c\u9762\u7684\u7f16\u8f91\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7Termux\u4e0ePC\u5171\u4eab\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Termux:API\u63d2\u4ef6<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Termux:API\u63d2\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7SSH\u6216FTP\u4e0ePC\u8fdb\u884c\u6587\u4ef6\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h4>2. \u5728PC\u4e0a\u7f16\u8f91\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728PC\u4e0a\u4f7f\u7528\u4f60\u559c\u6b22\u7684\u7f16\u8f91\u5668\u7f16\u8f91Python\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7FTP\u6216SSH\u4e0a\u4f20\u5230Termux\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5728PC\u4e0a\u8fdb\u884c\u590d\u6742\u7f16\u8f91\u5de5\u4f5c\u7684\u7528\u6237\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728Termux\u4e2d\u8fd0\u884cPython\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u521b\u5efaPython\u6587\u4ef6\u540e\uff0c\u901a\u5e38\u9700\u8981\u5728Termux\u4e2d\u8fd0\u884c\u4ee5\u6d4b\u8bd5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u786e\u4fddPython\u5df2\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5728Termux\u4e2d\uff0c\u8f93\u5165 <code>pkg install python<\/code> \u5b89\u88c5Python\u3002<\/p>\n<\/p>\n<p><h4>2. \u8fd0\u884cPython\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4 <code>python filename.py<\/code> \u8fd0\u884cPython\u6587\u4ef6\uff0c\u67e5\u770b\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Termux\u4e2d\u521b\u5efaPython\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u6837\u5316\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u7f16\u8f91\u5668\u3001Python\u5185\u7f6e\u51fd\u6570\u8fd8\u662f\u5916\u90e8\u7f16\u8f91\u5668\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u63a8\u8350\u4f7f\u7528nano\u7f16\u8f91\u5668\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\uff0c\u80fd\u591f\u5feb\u901f\u4e0a\u624b\u3002\u5728\u638c\u63e1\u57fa\u672c\u547d\u4ee4\u540e\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\u7684vim\u7f16\u8f91\u5668\uff0c\u6216\u8005\u901a\u8fc7Termux\u4e0ePC\u8fdb\u884c\u6587\u4ef6\u5171\u4eab\uff0c\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4e0d\u540c\u65b9\u6cd5\uff0c\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u7f16\u7a0b\u9700\u6c42\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u5347\u7f16\u7a0b\u4f53\u9a8c\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Termux\u4e2d\u5b89\u88c5Python\uff1f<\/strong><br \/>\u5728Termux\u4e2d\u521b\u5efaPython\u6587\u4ef6\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u53ef\u4ee5\u901a\u8fc7\u5728Termux\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<code>pkg install python<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>python --version<\/code>\u6765\u786e\u8ba4Python\u662f\u5426\u6210\u529f\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5728Termux\u4e2d\u521b\u5efa\u548c\u7f16\u8f91Python\u6587\u4ef6\u7684\u6700\u4f73\u5de5\u5177\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Termux\u4e2d\u6709\u591a\u79cd\u6587\u672c\u7f16\u8f91\u5668\u53ef\u4f9b\u9009\u62e9\uff0c\u50cf<code>nano<\/code>\u548c<code>vim<\/code>\u90fd\u662f\u5e38\u7528\u7684\u9009\u62e9\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>pkg install nano<\/code>\u6216<code>pkg install vim<\/code>\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u7f16\u8f91\u5668\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u4fee\u6539Python\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Termux\u4e2d\u8fd0\u884c\u521b\u5efa\u7684Python\u6587\u4ef6\uff1f<\/strong><br \/>\u4e00\u65e6\u60a8\u5728Termux\u4e2d\u521b\u5efa\u4e86Python\u6587\u4ef6\uff08\u4f8b\u5982<code>hello.py<\/code>\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u547d\u4ee4<code>python hello.py<\/code>\u6765\u8fd0\u884c\u8be5\u6587\u4ef6\u3002\u786e\u4fdd\u60a8\u5728\u7ec8\u7aef\u4e2d\u5207\u6362\u5230\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u8fd9\u6837\u624d\u80fd\u987a\u5229\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Termux\u4e2d\u521b\u5efaPython\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982nano\u3001vim\uff09\u3001Python\u5185\u7f6e\u7684 [&hellip;]","protected":false},"author":3,"featured_media":971622,"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\/971613"}],"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=971613"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971613\/revisions"}],"predecessor-version":[{"id":971626,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971613\/revisions\/971626"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971622"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}