{"id":981900,"date":"2024-12-27T07:06:23","date_gmt":"2024-12-26T23:06:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981900.html"},"modified":"2024-12-27T07:06:25","modified_gmt":"2024-12-26T23:06:25","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8linux%e7%bc%96%e5%86%99python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981900.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528linux\u7f16\u5199python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210616\/c5442ea2-b9d7-4b36-8b04-80bf6a45c1fd.webp\" alt=\"\u5982\u4f55\u4f7f\u7528linux\u7f16\u5199python\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Linux\u7f16\u5199Python\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5Python\u3001\u9009\u62e9\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u3001\u7f16\u5199\u548c\u8fd0\u884cPython\u811a\u672c\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177pip\u5b89\u88c5\u4f9d\u8d56\u3001\u638c\u63e1\u57fa\u672c\u7684Linux\u547d\u4ee4\u4ee5\u53ca\u5229\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\u7ba1\u7406\u4ee3\u7801\u3002<\/strong>\u5176\u4e2d\uff0c\u5b89\u88c5Python\u662f\u57fa\u7840\uff0c\u56e0\u4e3a\u5927\u90e8\u5206Linux\u53d1\u884c\u7248\u9884\u88c5\u4e86Python\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u4ee5\u4f7f\u7528\u6700\u65b0\u7279\u6027\u548c\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Python\u662f\u4f7f\u7528Linux\u7f16\u5199Python\u7684\u7b2c\u4e00\u6b65\u3002\u5927\u591a\u6570Linux\u53d1\u884c\u7248\uff0c\u5982Ubuntu\u548cFedora\uff0c\u9884\u88c5\u4e86Python\uff0c\u4f46\u8fd9\u4e9b\u7248\u672c\u53ef\u80fd\u4e0d\u662f\u6700\u65b0\u7684\u3002\u4e3a\u4e86\u786e\u4fdd\u80fd\u591f\u4f7f\u7528\u6700\u65b0\u7684Python\u7279\u6027\u548c\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7Linux\u5305\u7ba1\u7406\u5668\uff08\u5982apt\u3001yum\u6216dnf\uff09\u66f4\u65b0Python\u3002\u5bf9\u4e8e\u9700\u8981\u591a\u4e2aPython\u7248\u672c\u7684\u60c5\u51b5\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528pyenv\u7b49\u5de5\u5177\u8fdb\u884c\u7248\u672c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u73af\u5883\u4e2d\u9ad8\u6548\u5730\u7f16\u5199Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python<\/h3>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u5b89\u88c5\u548c\u7ba1\u7406Python\u7248\u672c\u662f\u9996\u5148\u9700\u8981\u89e3\u51b3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u90fd\u81ea\u5e26\u5305\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u7528\u6765\u5b89\u88c5\u548c\u7ba1\u7406\u8f6f\u4ef6\u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684Linux\u53d1\u884c\u7248\u53ca\u5176\u5305\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Ubuntu\/Debian<\/strong>\uff1a\u4f7f\u7528<code>apt<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sudo apt update<\/code>\u548c<code>sudo apt install python3<\/code>\u6765\u5b89\u88c5\u6216\u66f4\u65b0Python\u3002<\/li>\n<li><strong>Fedora<\/strong>\uff1a\u4f7f\u7528<code>dnf<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sudo dnf install python3<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li><strong>CentOS\/RHEL<\/strong>\uff1a\u4f7f\u7528<code>yum<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sudo yum install python3<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>python3 --version<\/code>\u786e\u8ba4\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u7279\u5b9aPython\u7248\u672c\u7684\u7528\u6237\uff0c\u6216\u8005\u5e0c\u671b\u5bf9Python\u8fdb\u884c\u81ea\u5b9a\u4e49\u7f16\u8bd1\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6e90\u7801\u5e76\u7f16\u8bd1\u5b89\u88c5\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u6e90\u7801\uff1a\u901a\u8fc7<code>wget https:\/\/www.python.org\/ftp\/python\/x.x.x\/Python-x.x.x.tgz<\/code>\u4e0b\u8f7d\u5bf9\u5e94\u7248\u672c\u7684\u6e90\u7801\u5305\u3002<\/li>\n<li>\u89e3\u538b\u6e90\u7801\uff1a\u4f7f\u7528<code>tar -xvf Python-x.x.x.tgz<\/code>\u89e3\u538b\u7f29\u6587\u4ef6\u3002<\/li>\n<li>\u7f16\u8bd1\u5b89\u88c5\uff1a\u8fdb\u5165\u89e3\u538b\u540e\u7684\u76ee\u5f55\uff0c\u4f9d\u6b21\u6267\u884c<code>.\/configure<\/code>\u3001<code>make<\/code>\u548c<code>sudo make install<\/code>\u8fdb\u884c\u7f16\u8bd1\u548c\u5b89\u88c5\u3002<\/li>\n<\/ol>\n<p><h4>1.3 \u4f7f\u7528pyenv\u7ba1\u7406\u591a\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5f00\u53d1\u8005\u800c\u8a00\uff0c\u6709\u65f6\u9700\u8981\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684Python\u7248\u672c\u3002\u8fd9\u65f6\uff0c<code>pyenv<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<code>pyenv<\/code>\u5141\u8bb8\u7528\u6237\u5b89\u88c5\u548c\u5207\u6362\u591a\u4e2aPython\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5<code>pyenv<\/code>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/pyenv.run | bash<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7<code>pyenv install x.x.x<\/code>\u5b89\u88c5\u6240\u9700\u7684Python\u7248\u672c\uff0c\u4f7f\u7528<code>pyenv global x.x.x<\/code>\u6216<code>pyenv local x.x.x<\/code>\u5207\u6362\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u5f00\u53d1\u73af\u5883\u53ef\u4ee5\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u8f7b\u91cf\u7ea7\u6587\u672c\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<ul>\n<li><strong>Vim<\/strong>\uff1aVim\u662fLinux\u4e0a\u975e\u5e38\u6d41\u884c\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u63d2\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u5b9a\u5236\u3002<\/li>\n<li><strong>Nano<\/strong>\uff1aNano\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<\/ul>\n<p><h4>2.2 \u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/h4>\n<\/p>\n<ul>\n<li><strong>PyCharm<\/strong>\uff1aPyCharm\u662fJetBr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns\u5f00\u53d1\u7684\u4e00\u6b3e\u5f3a\u5927\u7684Python IDE\uff0c\u652f\u6301\u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u3001\u7248\u672c\u63a7\u5236\u7b49\u529f\u80fd\u3002<\/li>\n<li><strong>Visual Studio Code<\/strong>\uff1aVisual Studio Code\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u4f46\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u8f91\u5668\uff0c\u652f\u6301Python\u6269\u5c55\uff0c\u63d0\u4f9b\u8c03\u8bd5\u3001\u4ee3\u7801\u8865\u5168\u3001\u8bed\u6cd5\u9ad8\u4eae\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u5b89\u88c5\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7Linux\u5305\u7ba1\u7406\u5668\u6216\u8005\u76f4\u63a5\u4ece\u5b98\u7f51\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u548c\u8fd0\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728Linux\u4e0a\uff0c\u7f16\u5199Python\u811a\u672c\u901a\u5e38\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982<code>hello.py<\/code>\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u7b80\u5355\u7684Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u8fd0\u884cPython\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5b8c\u6210\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.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\u52a0\u4e0a\u53c2\u6570\uff0c\u6bd4\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><h3>\u56db\u3001\u4f7f\u7528pip\u7ba1\u7406Python\u5305<\/h3>\n<\/p>\n<p><p>Python\u751f\u6001\u7cfb\u7edf\u4e30\u5bcc\u7684\u5e93\u548c\u6846\u67b6\u662f\u5176\u5f3a\u5927\u4e4b\u5904\u4e4b\u4e00\u3002<code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5pip<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u4e2d\uff0c<code>pip<\/code>\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff0c\u4f8b\u5982\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><h4>4.2 \u4f7f\u7528pip\u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Python\u5305\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip3 install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip3 install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u7ba1\u7406\u5df2\u5b89\u88c5\u7684\u5305<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pip list<\/code>\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305\uff0c\u901a\u8fc7<code>pip uninstall package_name<\/code>\u5378\u8f7d\u4e0d\u518d\u9700\u8981\u7684\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u638c\u63e1\u57fa\u672c\u7684Linux\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u5728Linux\u73af\u5883\u4e0b\u5f00\u53d1Python\u5e94\u7528\uff0c\u638c\u63e1\u4e00\u4e9b\u57fa\u672c\u7684Linux\u547d\u4ee4\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c<\/h4>\n<\/p>\n<ul>\n<li><strong>ls<\/strong>\uff1a\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\u3002<\/li>\n<li><strong>cd<\/strong>\uff1a\u66f4\u6539\u5f53\u524d\u76ee\u5f55\u3002<\/li>\n<li><strong>mkdir<\/strong>\uff1a\u521b\u5efa\u65b0\u76ee\u5f55\u3002<\/li>\n<li><strong>rm<\/strong>\uff1a\u5220\u9664\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p><h4>5.2 \u6743\u9650\u7ba1\u7406<\/h4>\n<\/p>\n<ul>\n<li><strong>chmod<\/strong>\uff1a\u6539\u53d8\u6587\u4ef6\u6743\u9650\u3002<\/li>\n<li><strong>chown<\/strong>\uff1a\u6539\u53d8\u6587\u4ef6\u6240\u6709\u8005\u3002<\/li>\n<\/ul>\n<p><h4>5.3 \u8fdb\u7a0b\u7ba1\u7406<\/h4>\n<\/p>\n<ul>\n<li><strong>ps<\/strong>\uff1a\u67e5\u770b\u5f53\u524d\u8fdb\u7a0b\u3002<\/li>\n<li><strong>kill<\/strong>\uff1a\u7ec8\u6b62\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5229\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\u7ba1\u7406\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7248\u672c\u63a7\u5236\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c<code>Git<\/code>\u662f\u6700\u6d41\u884c\u7684\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u5b89\u88c5Git<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Git\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt install git<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u57fa\u672cGit\u547d\u4ee4<\/h4>\n<\/p>\n<ul>\n<li><strong>git init<\/strong>\uff1a\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684Git\u4ed3\u5e93\u3002<\/li>\n<li><strong>git clone<\/strong>\uff1a\u514b\u9686\u4e00\u4e2a\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/li>\n<li><strong>git add<\/strong>\uff1a\u6dfb\u52a0\u6587\u4ef6\u5230\u6682\u5b58\u533a\u3002<\/li>\n<li><strong>git commit<\/strong>\uff1a\u63d0\u4ea4\u66f4\u6539\u3002<\/li>\n<li><strong>git push<\/strong>\uff1a\u5c06\u672c\u5730\u63d0\u4ea4\u63a8\u9001\u5230\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/li>\n<\/ul>\n<p><h4>6.3 \u4f7f\u7528GitHub\u7b49\u5e73\u53f0<\/h4>\n<\/p>\n<p><p>GitHub\u662f\u4e00\u4e2a\u6258\u7ba1Git\u4ed3\u5e93\u7684\u5728\u7ebf\u5e73\u53f0\uff0c\u53ef\u4ee5\u7528\u6765\u534f\u4f5c\u5f00\u53d1\u548c\u5206\u4eab\u4ee3\u7801\u3002\u5728GitHub\u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u4ed3\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>git remote add origin<\/code>\u547d\u4ee4\u5c06\u672c\u5730\u4ed3\u5e93\u4e0e\u8fdc\u7a0b\u4ed3\u5e93\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8c03\u8bd5\u548c\u4f18\u5316Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h4>\n<\/p>\n<ul>\n<li><strong>pdb<\/strong>\uff1aPython\u5185\u7f6e\u7684\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u72b6\u6001\u3002<\/li>\n<li><strong>PyCharm\/VS Code<\/strong>\uff1a\u8fd9\u4e9bIDE\u63d0\u4f9b\u56fe\u5f62\u5316\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u652f\u6301\u65ad\u70b9\u3001\u53d8\u91cf\u76d1\u89c6\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><h4>7.2 \u4ee3\u7801\u4f18\u5316\u6280\u5de7<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u5728\u9700\u8981\u751f\u6210\u5217\u8868\u7684\u5730\u65b9\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4for\u5faa\u73af\u66f4\u7b80\u6d01\u9ad8\u6548\u3002<\/li>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u7f13\u5b58\u7ed3\u679c<\/strong>\uff1a\u5bf9\u4e8e\u91cd\u590d\u8ba1\u7b97\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u81ea\u52a8\u5316\u4efb\u52a1\u548c\u811a\u672c<\/h3>\n<\/p>\n<p><p>Python\u64c5\u957f\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u81ea\u52a8\u5316\u5904\u7406\u65e5\u5e38\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u4f7f\u7528Crontab<\/h4>\n<\/p>\n<p><p>\u5728Linux\u4e2d\uff0c<code>crontab<\/code>\u53ef\u4ee5\u7528\u6765\u5b9a\u671f\u6267\u884cPython\u811a\u672c\u3002\u4f8b\u5982\uff0c\u6bcf\u5929\u665a\u4e0a2\u70b9\u8fd0\u884c\u4e00\u6b21\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">0 2 * * * \/usr\/bin\/python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u4f7f\u7528Shell\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u7f16\u5199Shell\u811a\u672c\u8c03\u7528Python\u811a\u672c\uff0c\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">#!\/bin\/bash<\/p>\n<p>python3 \/path\/to\/script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fd9\u4e9b\u6b65\u9aa4\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728Linux\u73af\u5883\u4e2d\u9ad8\u6548\u5730\u7f16\u5199\u548c\u7ba1\u7406Python\u4ee3\u7801\u3002\u65e0\u8bba\u662f\u4e2a\u4eba\u9879\u76ee\u8fd8\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\uff0cLinux\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Linux\u4e0a\u5b89\u88c5Python\u73af\u5883\uff1f<\/strong><br \/>\u5728Linux\u4e0a\u5b89\u88c5Python\u73af\u5883\u975e\u5e38\u7b80\u5355\u3002\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u90fd\u9884\u88c5\u4e86Python\uff0c\u4f46\u5982\u679c\u9700\u8981\u5b89\u88c5\u7279\u5b9a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u4e0a\uff0c\u53ef\u4ee5\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165<code>sudo apt-get install python3<\/code>\u6765\u5b89\u88c5Python 3\u3002\u5bf9\u4e8e\u5176\u4ed6\u7248\u672c\uff0c\u4f7f\u7528<code>pyenv<\/code>\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5b83\u53ef\u4ee5\u7ba1\u7406\u591a\u4e2aPython\u7248\u672c\uff0c\u5e76\u5141\u8bb8\u60a8\u6839\u636e\u9700\u8981\u5207\u6362\u3002<\/p>\n<p><strong>\u5728Linux\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u7ba1\u7406Python\u865a\u62df\u73af\u5883\uff1f<\/strong><br \/>\u521b\u5efa\u548c\u7ba1\u7406Python\u865a\u62df\u73af\u5883\u53ef\u4ee5\u6709\u6548\u5730\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u547d\u4ee4\u4e3a<code>python3 -m venv myenv<\/code>\uff0c\u5176\u4e2d<code>myenv<\/code>\u662f\u60a8\u8981\u521b\u5efa\u7684\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u662f<code>source myenv\/bin\/activate<\/code>\u3002\u4e00\u65e6\u6fc0\u6d3b\uff0c\u60a8\u53ef\u4ee5\u5728\u8be5\u73af\u5883\u4e2d\u81ea\u7531\u5b89\u88c5\u548c\u4f7f\u7528\u5305\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40Python\u73af\u5883\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Linux\u4e0a\u8c03\u8bd5Python\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Linux\u4e0a\u8c03\u8bd5Python\u4ee3\u7801\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\u3002\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u8fd9\u5c06\u4f7f\u7a0b\u5e8f\u5728\u6b64\u884c\u6682\u505c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002\u9664\u4e86<code>pdb<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216VSCode\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u4e3a\u76f4\u89c2\u7684\u8c03\u8bd5\u754c\u9762\u548c\u529f\u80fd\uff0c\u65b9\u4fbf\u60a8\u9010\u6b65\u8ddf\u8e2a\u4ee3\u7801\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<p><strong>\u4f7f\u7528Linux\u7f16\u5199Python\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56\u5305\uff1f<\/strong><br \/>\u5728Linux\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u6765\u7ba1\u7406Python\u7684\u4f9d\u8d56\u5305\u3002\u901a\u8fc7\u5728\u7ec8\u7aef\u8f93\u5165<code>pip install package_name<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u5305\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\uff0c\u786e\u4fdd\u5728\u6fc0\u6d3b\u73af\u5883\u540e\u8fd0\u884c\u6b64\u547d\u4ee4\u3002\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u8bb0\u5f55\u9879\u76ee\u7684\u4f9d\u8d56\u5305\uff0c\u901a\u8fc7<code>pip install -r requirements.txt<\/code>\u547d\u4ee4\u4e00\u952e\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Linux\u7f16\u5199Python\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5Python\u3001\u9009\u62e9\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u3001\u7f16\u5199\u548c\u8fd0\u884cPyt [&hellip;]","protected":false},"author":3,"featured_media":981909,"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\/981900"}],"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=981900"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981900\/revisions"}],"predecessor-version":[{"id":981912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981900\/revisions\/981912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981909"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}