{"id":1149458,"date":"2025-01-13T16:49:42","date_gmt":"2025-01-13T08:49:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149458.html"},"modified":"2025-01-13T16:49:44","modified_gmt":"2025-01-13T08:49:44","slug":"linux%e7%b3%bb%e7%bb%9f%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149458.html","title":{"rendered":"Linux\u7cfb\u7edf\u5982\u4f55\u4f7f\u7528Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180130\/aad7c0d6-ddb6-495c-8554-050ec22e2920.webp\" alt=\"Linux\u7cfb\u7edf\u5982\u4f55\u4f7f\u7528Python\" \/><\/p>\n<p><p> <strong>Linux\u7cfb\u7edf\u4f7f\u7528Python\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u5b89\u88c5Python\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406Python\u5305\u3001\u4f7f\u7528IDE\u6216\u4ee3\u7801\u7f16\u8f91\u5668\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u3002<\/strong>\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662f\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u56e0\u4e3a\u8fd9\u662f\u6700\u57fa\u7840\u7684\u64cd\u4f5c\uff0c\u4e5f\u662f\u8bb8\u591a\u5176\u4ed6\u64cd\u4f5c\u7684\u524d\u63d0\u3002<\/p>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\u8fd0\u884cPython\u811a\u672c\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u7cfb\u7edf\u5df2\u5b89\u88c5Python\uff1b\u7136\u540e\uff0c\u521b\u5efaPython\u811a\u672c\u6587\u4ef6\uff1b\u6700\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u8be5\u811a\u672c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u7248\u672c\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\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff0c\u5982<code>hello.py<\/code>\uff0c\u5e76\u7f16\u5199\u7b80\u5355\u7684\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><p>\u6700\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u8be5\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>\u8fd9\u5c06\u8f93\u51fa<code>Hello, World!<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u57fa\u7840\u4f46\u5374\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u662f\u7406\u89e3\u548c\u638c\u63e1\u5176\u4ed6\u9ad8\u7ea7\u64cd\u4f5c\u7684\u524d\u63d0\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python<\/h3>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\u5b89\u88c5Python\u662f\u4f7f\u7528Python\u8fdb\u884c\u5f00\u53d1\u7684\u7b2c\u4e00\u6b65\u3002\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u9ed8\u8ba4\u5df2\u7ecf\u5b89\u88c5\u4e86Python\uff0c\u4f46\u53ef\u80fd\u4e0d\u662f\u6700\u65b0\u7248\u672c\u3002\u56e0\u6b64\uff0c\u7528\u6237\u53ef\u80fd\u9700\u8981\u66f4\u65b0\u6216\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684Python\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u68c0\u67e5Python\u662f\u5426\u5df2\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\u7cfb\u7edf\u4e0a\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u4ee5\u53ca\u5176\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cPython\u5df2\u5b89\u88c5\uff0c\u5c06\u663e\u793a\u7248\u672c\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">Python 3.8.10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u672a\u5b89\u88c5\u6216\u9700\u8981\u7279\u5b9a\u7248\u672c\uff0c\u53ef\u4ee5\u7ee7\u7eed\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570Linux\u53d1\u884c\u7248\u63d0\u4f9b\u5305\u7ba1\u7406\u5668\uff0c\u5982APT\uff08Debian\/Ubuntu\uff09\u3001YUM\uff08CentOS\/RedHat\uff09\u7b49\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5Python\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728Debian\/Ubuntu\u4e0a\u5b89\u88c5Python\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728CentOS\/RedHat\u4e0a\u5b89\u88c5Python\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo yum install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u518d\u6b21\u8fd0\u884c<code>python3 --version<\/code>\u547d\u4ee4\u68c0\u67e5\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h4>1.3 \u6e90\u4ee3\u7801\u7f16\u8bd1\u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u7279\u5b9a\u7248\u672c\u6216\u6700\u65b0\u7248\u672c\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9Python\u7f51\u7ad9\u4e0b\u8f7d\u6e90\u7801\u5e76\u624b\u52a8\u7f16\u8bd1\u5b89\u88c5\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7e41\u7410\uff0c\u4f46\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4e0b\u8f7d\u5e76\u89e3\u538b\u6e90\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">wget https:\/\/www.python.org\/ftp\/python\/3.9.7\/Python-3.9.7.tgz<\/p>\n<p>tar -xvf Python-3.9.7.tgz<\/p>\n<p>cd Python-3.9.7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u914d\u7f6e\u3001\u7f16\u8bd1\u5e76\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\/configure --enable-optimizations<\/p>\n<p>make<\/p>\n<p>sudo make altinstall<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python3.9 --version<\/code>\u547d\u4ee4\u68c0\u67e5\u5b89\u88c5\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\u8fd0\u884cPython\u811a\u672c\u662f\u5f00\u53d1\u548c\u8c03\u8bd5Python\u4ee3\u7801\u7684\u57fa\u672c\u64cd\u4f5c\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f7b\u677e\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521b\u5efaPython\u811a\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>hello.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nano hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u4e2d\u7f16\u5199\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><p>\u4fdd\u5b58\u5e76\u5173\u95ed\u7f16\u8f91\u5668\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528<code>python3<\/code>\u547d\u4ee4\u52a0\u4e0a\u811a\u672c\u6587\u4ef6\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>2.3 \u4f7f\u7528Shebang\u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7fPython\u811a\u672c\u66f4\u50cf\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0Shebang\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8d4b\u4e88\u811a\u672c\u6267\u884c\u6743\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">chmod +x hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u811a\u672c\uff0c\u800c\u65e0\u9700\u663e\u5f0f\u8c03\u7528Python\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">.\/hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u540c\u6837\u8f93\u51fa<code>Hello, World!<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406Python\u5305<\/h3>\n<\/p>\n<p><p>\u5728Python\u5f00\u53d1\u4e2d\uff0c\u865a\u62df\u73af\u5883\uff08Virtual Environment\uff09\u662f\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\uff0c\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u907f\u514d\u5305\u7248\u672c\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5<code>venv<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myenv<\/code>\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u6240\u6709\u7684Python\u5305\u5b89\u88c5\u5c06\u4ec5\u5f71\u54cd\u8be5\u73af\u5883\uff0c\u4e0d\u4f1a\u5e72\u6270\u7cfb\u7edf\u7684\u5168\u5c40\u5305\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u901a\u5e38\u4f1a\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\uff0c\u4ee5\u6307\u793a\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>3.3 \u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684Python\u5305\u3002\u4f8b\u5982\uff0c\u5b89\u88c5<code>requests<\/code>\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e9b\u5305\u3002<\/p>\n<\/p>\n<p><h4>3.4 \u9000\u51fa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u5f00\u53d1\u5de5\u4f5c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff0c\u6062\u590d\u5230\u7cfb\u7edf\u7684Python\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528IDE\u6216\u4ee3\u7801\u7f16\u8f91\u5668\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u7f16\u5199Python\u4ee3\u7801\uff0c\u4f46\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6216\u9ad8\u7ea7\u4ee3\u7801\u7f16\u8f91\u5668\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u9009\u62e9\u5408\u9002\u7684IDE\u6216\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u6709\u8bb8\u591a\u4f18\u79c0\u7684IDE\u548c\u4ee3\u7801\u7f16\u8f91\u5668\u53ef\u4f9b\u9009\u62e9\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyCharm<\/strong>\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684Python IDE\uff0c\u63d0\u4f9b\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u3001\u7248\u672c\u63a7\u5236\u96c6\u6210\u7b49\u3002<\/li>\n<li><strong>VSCode<\/strong>\uff1a\u4e00\u4e2a\u6d41\u884c\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ecPython\uff0c\u5e76\u6709\u8bb8\u591a\u6269\u5c55\u63d2\u4ef6\u3002<\/li>\n<li><strong>Sublime Text<\/strong>\uff1a\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u5feb\u901f\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u63d2\u4ef6\u3002<\/li>\n<\/ul>\n<p><h4>4.2 \u914d\u7f6ePython\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cdIDE\u6216\u7f16\u8f91\u5668\uff0c\u901a\u5e38\u90fd\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u914d\u7f6e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u652f\u6301Python\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4ee5VSCode\u4e3a\u4f8b<\/strong>\uff0c\u9996\u5148\uff0c\u5b89\u88c5Python\u6269\u5c55\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00VSCode\uff0c\u8fdb\u5165\u6269\u5c55\uff08Extensions\uff09\u89c6\u56fe\u3002<\/li>\n<li>\u641c\u7d22\u5e76\u5b89\u88c5\u201cPython\u201d\u6269\u5c55\u3002<\/li>\n<\/ol>\n<p><p>\u7136\u540e\uff0c\u914d\u7f6ePython\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u9762\u677f\uff08Ctrl+Shift+P\uff09\u3002<\/li>\n<li>\u9009\u62e9\u201cPython: Select Interpreter\u201d\u3002<\/li>\n<li>\u9009\u62e9\u7cfb\u7edf\u6216\u865a\u62df\u73af\u5883\u4e2d\u7684Python\u89e3\u91ca\u5668\u3002<\/li>\n<\/ol>\n<p><h4>4.3 \u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u914d\u7f6e\u597d\u7684IDE\u6216\u7f16\u8f91\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f16\u5199\u548c\u8c03\u8bd5Python\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u5728VSCode\u4e2d\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u5e76\u542f\u52a8\u8c03\u8bd5\u4f1a\u8bdd\uff0c\u5e2e\u52a9\u53d1\u73b0\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>4.4 \u4f7f\u7528\u7248\u672c\u63a7\u5236\u96c6\u6210<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570IDE\u548c\u7f16\u8f91\u5668\u652f\u6301\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u7684\u96c6\u6210\uff0c\u5e2e\u52a9\u7ba1\u7406\u4ee3\u7801\u7248\u672c\u548c\u534f\u4f5c\u5f00\u53d1\u3002\u4f8b\u5982\uff0c\u5728VSCode\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e90\u4ee3\u7801\u7ba1\u7406\uff08Source Control\uff09\u89c6\u56fe\u8fdb\u884c\u63d0\u4ea4\u3001\u66f4\u6539\u548c\u5206\u652f\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305<\/h3>\n<\/p>\n<p><p>Python\u6709\u4e00\u4e2a\u5f3a\u5927\u7684\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93\u3002\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u53ef\u4ee5\u6781\u5927\u5730\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5b89\u88c5pip<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570Linux\u7cfb\u7edf\u4e0a\uff0c<code>pip<\/code>\u901a\u5e38\u968fPython\u4e00\u8d77\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528pip\u5b89\u88c5\u5305<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u6240\u9700\u7684Python\u5305\u3002\u4f8b\u5982\uff0c\u5b89\u88c5<code>numpy<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3 \u5347\u7ea7\u548c\u5378\u8f7d\u5305<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5347\u7ea7\u548c\u5378\u8f7d\u5305\u3002\u4f8b\u5982\uff0c\u5347\u7ea7<code>numpy<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5378\u8f7d<code>numpy<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.4 \u7ba1\u7406\u9879\u76ee\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u9879\u76ee\u65f6\uff0c\u901a\u5e38\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u4f9d\u8d56\u5305\u3002\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u8bb0\u5f55\u9879\u76ee\u6240\u9700\u7684\u6240\u6709\u5305\uff0c\u5e76\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">numpy<\/p>\n<p>requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u786e\u4fdd\u9879\u76ee\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Jupyter Notebook\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u79cd\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u975e\u5e38\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u5b83\u5141\u8bb8\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7f16\u5199\u548c\u8fd0\u884c\u4ee3\u7801\uff0c\u5e76\u5b9e\u65f6\u67e5\u770b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u5b89\u88c5Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5Jupyter Notebook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jupyter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u542f\u52a8Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Jupyter Notebook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">jupyter notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u663e\u793aJupyter Notebook\u7684\u4e3b\u754c\u9762\u3002\u5728\u8be5\u754c\u9762\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u548c\u7ba1\u7406Notebook\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>6.3 \u521b\u5efa\u548c\u8fd0\u884cNotebook<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Notebook\u6587\u4ef6\uff0c\u5e76\u9009\u62e9Python\u5185\u6838\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5728Notebook\u4e2d\u7f16\u5199\u548c\u8fd0\u884cPython\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Notebook\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee3\u7801\u540e\uff0c\u5c06\u5b9e\u65f6\u663e\u793a\u7ed3\u679c\uff0c\u5305\u62ec\u56fe\u5f62\u548c\u6570\u636e\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>6.4 \u4fdd\u5b58\u548c\u5171\u4eabNotebook<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u6587\u4ef6\uff08.ipynb\uff09\u53ef\u4ee5\u4fdd\u5b58\u5e76\u5206\u4eab\u7ed9\u5176\u4ed6\u7528\u6237\u3002\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u52a0\u8f7d\u5e76\u8fd0\u884c\u8fd9\u4e9bNotebook\uff0c\u67e5\u770b\u4ee3\u7801\u548c\u7ed3\u679c\u3002\u8fd9\u5bf9\u4e8e\u534f\u4f5c\u548c\u5c55\u793a\u6570\u636e\u5206\u6790\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Docker\u5bb9\u5668\u5316Python\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Docker\u662f\u4e00\u79cd\u5bb9\u5668\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u5c06\u5e94\u7528\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u5c01\u88c5\u5728\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u3001\u53ef\u79fb\u690d\u7684\u5bb9\u5668\u4e2d\u3002\u4f7f\u7528Docker\u53ef\u4ee5\u786e\u4fdd\u5e94\u7528\u5728\u4efb\u4f55\u73af\u5883\u4e2d\u90fd\u80fd\u4e00\u81f4\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u5b89\u88c5Docker<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728Linux\u7cfb\u7edf\u4e0a\u5b89\u88c5Docker\u3002\u53ef\u4ee5\u53c2\u8003Docker\u5b98\u65b9\u6587\u6863\uff0c\u6216\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u8fdb\u884c\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get install docker-ce docker-ce-cli cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nerd.io<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u521b\u5efaDockerfile<\/h4>\n<\/p>\n<p><p>Dockerfile\u662f\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u5305\u542b\u6784\u5efaDocker\u955c\u50cf\u7684\u6307\u4ee4\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>Dockerfile<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-dockerfile\"># \u4f7f\u7528\u5b98\u65b9Python\u57fa\u7840\u955c\u50cf<\/p>\n<p>FROM python:3.9-slim<\/p>\n<h2><strong>\u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55<\/strong><\/h2>\n<p>WORKDIR \/app<\/p>\n<h2><strong>\u590d\u5236\u9879\u76ee\u6587\u4ef6\u5230\u5bb9\u5668\u4e2d<\/strong><\/h2>\n<p>COPY . \/app<\/p>\n<h2><strong>\u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/strong><\/h2>\n<p>RUN pip install -r requirements.txt<\/p>\n<h2><strong>\u8fd0\u884cPython\u5e94\u7528<\/strong><\/h2>\n<p>CMD [&quot;python&quot;, &quot;hello.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aDockerfile\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e94\u7528\u7684Docker\u955c\u50cf\uff0c\u5305\u62ec\u57fa\u7840\u955c\u50cf\u3001\u5de5\u4f5c\u76ee\u5f55\u3001\u6587\u4ef6\u590d\u5236\u3001\u4f9d\u8d56\u5b89\u88c5\u548c\u8fd0\u884c\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>7.3 \u6784\u5efa\u548c\u8fd0\u884cDocker\u955c\u50cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efaDocker\u955c\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker build -t my-python-app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker run -it --rm my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8\u5bb9\u5668\u5e76\u8fd0\u884cPython\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>7.4 \u7ba1\u7406Docker\u5bb9\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Docker\u7684\u7ba1\u7406\u547d\u4ee4\u67e5\u770b\u548c\u7ba1\u7406\u5bb9\u5668\u3002\u4f8b\u5982\uff0c\u67e5\u770b\u6b63\u5728\u8fd0\u884c\u7684\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker ps<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u505c\u6b62\u548c\u5220\u9664\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker stop &lt;container_id&gt;<\/p>\n<p>docker rm &lt;container_id&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u5bb9\u5668\u5316\u65b9\u6cd5\u786e\u4fdd\u4e86\u5e94\u7528\u7684\u53ef\u79fb\u690d\u6027\u548c\u4e00\u81f4\u6027\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/blog\/devops\/52691.html\" target=\"_blank\">CI\/CD<\/a>\u5de5\u5177\u8fdb\u884c\u81ea\u52a8\u5316\u90e8\u7f72<\/h3>\n<\/p>\n<p><p>\u6301\u7eed\u96c6\u6210\u548c\u6301\u7eed\u90e8\u7f72\uff08CI\/CD\uff09\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u7684\u91cd\u8981\u5b9e\u8df5\uff0c\u80fd\u591f\u81ea\u52a8\u5316\u6784\u5efa\u3001\u6d4b\u8bd5\u548c\u90e8\u7f72\u6d41\u7a0b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u9009\u62e9CI\/CD\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u6709\u8bb8\u591aCI\/CD\u5de5\u5177\u53ef\u4f9b\u9009\u62e9\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Jenkins<\/strong>\uff1a\u4e00\u4e2a\u5f00\u6e90\u7684\u81ea\u52a8\u5316\u670d\u52a1\u5668\uff0c\u652f\u6301\u591a\u79cd\u63d2\u4ef6\u548c\u96c6\u6210\u3002<\/li>\n<li><strong>GitLab CI\/CD<\/strong>\uff1aGitLab\u63d0\u4f9b\u7684\u5185\u7f6eCI\/CD\u5de5\u5177\uff0c\u96c6\u6210\u4ee3\u7801\u4ed3\u5e93\u548cCI\/CD\u7ba1\u9053\u3002<\/li>\n<li><strong>GitHub Actions<\/strong>\uff1aGitHub\u63d0\u4f9b\u7684CI\/CD\u670d\u52a1\uff0c\u96c6\u6210\u5728GitHub\u5e73\u53f0\u4e0a\u3002<\/li>\n<\/ul>\n<p><h4>8.2 \u914d\u7f6eCI\/CD\u7ba1\u9053<\/h4>\n<\/p>\n<p><p>\u4ee5GitHub Actions\u4e3a\u4f8b\uff0c\u9996\u5148\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>.github\/workflows<\/code>\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u6d41\u6587\u4ef6\uff0c\u4f8b\u5982<code>ci.yml<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">name: CI<\/p>\n<p>on:<\/p>\n<p>  push:<\/p>\n<p>    branches:<\/p>\n<p>      - main<\/p>\n<p>jobs:<\/p>\n<p>  build:<\/p>\n<p>    runs-on: ubuntu-latest<\/p>\n<p>    steps:<\/p>\n<p>    - name: Checkout code<\/p>\n<p>      uses: actions\/checkout@v2<\/p>\n<p>    - name: Set up Python<\/p>\n<p>      uses: actions\/setup-python@v2<\/p>\n<p>      with:<\/p>\n<p>        python-version: 3.9<\/p>\n<p>    - name: Install dependencies<\/p>\n<p>      run: |<\/p>\n<p>        python -m pip install --upgrade pip<\/p>\n<p>        pip install -r requirements.txt<\/p>\n<p>    - name: Run tests<\/p>\n<p>      run: |<\/p>\n<p>        pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5de5\u4f5c\u6d41\u5b9a\u4e49\u4e86\u4e00\u4e2aCI\u7ba1\u9053\uff0c\u5305\u62ec\u4ee3\u7801\u68c0\u67e5\u3001\u8bbe\u7f6ePython\u73af\u5883\u3001\u5b89\u88c5\u4f9d\u8d56\u548c\u8fd0\u884c\u6d4b\u8bd5\u3002\u5f53\u4ee3\u7801\u63a8\u9001\u5230<code>main<\/code>\u5206\u652f\u65f6\uff0c\u5c06\u81ea\u52a8\u89e6\u53d1\u8fd9\u4e2a\u5de5\u4f5c\u6d41\u3002<\/p>\n<\/p>\n<p><h4>8.3 \u96c6\u6210\u90e8\u7f72\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728CI\/CD\u7ba1\u9053\u4e2d\u6dfb\u52a0\u90e8\u7f72\u6b65\u9aa4\uff0c\u4f8b\u5982\u5c06\u6784\u5efa\u540e\u7684\u5e94\u7528\u90e8\u7f72\u5230\u670d\u52a1\u5668\u6216\u4e91\u5e73\u53f0\u3002\u5177\u4f53\u6b65\u9aa4\u53d6\u51b3\u4e8e\u90e8\u7f72\u76ee\u6807\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>scp<\/code>\u547d\u4ee4\u5c06\u5e94\u7528\u6587\u4ef6\u590d\u5236\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u3002<\/li>\n<li>\u4f7f\u7528\u4e91\u5e73\u53f0\u63d0\u4f9b\u7684CLI\u5de5\u5177\uff08\u5982AWS CLI\u3001GCP CLI\u7b49\uff09\u8fdb\u884c\u81ea\u52a8\u5316\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>scp<\/code>\u547d\u4ee4\u5c06\u5e94\u7528\u6587\u4ef6\u590d\u5236\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">- name: Deploy to server<\/p>\n<p>  run: |<\/p>\n<p>    scp -r .\/app user@remote-server:\/path\/to\/deploy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u81ea\u52a8\u5316\u90e8\u7f72\u65b9\u6cd5\u786e\u4fdd\u4e86\u4ee3\u7801\u66f4\u6539\u80fd\u591f\u5feb\u901f\u3001\u5b89\u5168\u5730\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\uff0c\u63d0\u9ad8\u4e86\u53d1\u5e03\u6548\u7387\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u65e5\u5fd7\u548c\u76d1\u63a7\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8fd0\u884cPython\u5e94\u7528\u65f6\uff0c\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7\u662f\u786e\u4fdd\u5e94\u7528\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>9.1 \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002\u53ef\u4ee5\u5728\u5e94\u7528\u4e2d\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4ee5\u6355\u83b7\u548c\u5b58\u50a8\u91cd\u8981\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO,<\/p>\n<p>                    format=&#39;%(asctime)s:%(levelname)s:%(message)s&#39;)<\/p>\n<p>logging.info(&#39;Application started&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u914d\u7f6e\u5c06\u65e5\u5fd7\u4fe1\u606f\u5199\u5165<code>app.log<\/code>\u6587\u4ef6\uff0c\u5305\u62ec\u65f6\u95f4\u6233\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u6d88\u606f\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>9.2 \u4f7f\u7528\u76d1\u63a7\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u76d1\u63a7\u5de5\u5177\u53ef\u7528\u4e8e\u5b9e\u65f6\u76d1\u63a7\u5e94\u7528\u7684\u6027\u80fd\u548c\u5065\u5eb7\u72b6\u6001\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Prometheus<\/strong>\uff1a\u4e00\u4e2a\u5f00\u6e90\u7684\u76d1\u63a7\u548c\u62a5\u8b66\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u91c7\u96c6\u548c\u67e5\u8be2\u3002<\/li>\n<li><strong>Grafana<\/strong>\uff1a\u4e00\u4e2a\u5f00\u6e90\u7684\u5206\u6790\u548c\u76d1\u63a7\u5e73\u53f0\uff0c\u53ef\u4ee5\u4e0ePrometheus\u96c6\u6210\uff0c\u63d0\u4f9b\u5b9e\u65f6\u53ef\u89c6\u5316\u4eea\u8868\u76d8\u3002<\/li>\n<li><strong>ELK Stack<\/strong>\uff1a\u7531Elasticsearch\u3001Logstash\u548cKibana\u7ec4\u6210\u7684\u65e5\u5fd7\u7ba1\u7406\u548c\u5206\u6790\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><h4>9.3 \u914d\u7f6ePrometheus\u548cGrafana<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Prometheus\u548cGrafana\u6765\u76d1\u63a7Python\u5e94\u7528\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Prometheus\u76d1\u63a7\u5e94\u7528\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728Grafana\u4e2d\u5c55\u793a\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5\u548c\u914d\u7f6ePrometheus\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u5e76\u89e3\u538bPrometheus\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">wget https:\/\/github.com\/prometheus\/prometheus\/releases\/download\/v2.29.1\/prometheus-2.29.1.linux-amd64.tar.gz<\/p>\n<p>tar -xvf prometheus-2.29.1.linux-amd64.tar.gz<\/p>\n<p>cd prometheus-2.29.1.linux-amd64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u914d\u7f6ePrometheus\u7684<code>prometheus.yml<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0\u76d1\u63a7\u76ee\u6807\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-yaml\">scrape_configs:<\/p>\n<p>  - job_name: &#39;python-app&#39;<\/p>\n<p>    static_configs:<\/p>\n<p>      - targets: [&#39;localhost:8000&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u542f\u52a8Prometheus\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">.\/prometheus --config.file=prometheus.yml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5b89\u88c5\u548c\u914d\u7f6eGrafana\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5Grafana\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">sudo apt-get install -y adduser libfontconfig1<\/p>\n<p>wget https:\/\/dl.grafana.com\/oss\/release<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e0a\u5b89\u88c5Python\uff1f<\/strong><br \/>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u5b89\u88c5Python\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u6765\u5b8c\u6210\u3002\u5bf9\u4e8e\u57fa\u4e8eDebian\u7684\u7cfb\u7edf\uff08\u5982Ubuntu\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>sudo apt-get install python3<\/code>\u6765\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Python 3\u3002\u5bf9\u4e8e\u57fa\u4e8eRed Hat\u7684\u7cfb\u7edf\uff08\u5982CentOS\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sudo yum install python3<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u8f93\u5165<code>python3 --version<\/code>\u6765\u68c0\u67e5Python\u7684\u5b89\u88c5\u60c5\u51b5\u3002<\/p>\n<p><strong>2. Linux\u4e0b\u5982\u4f55\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Linux\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u811a\u672c\u6587\u4ef6\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u3002\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>chmod +x your_script.py<\/code>\u6765\u8bbe\u7f6e\u6743\u9650\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>.\/your_script.py<\/code>\u6765\u8fd0\u884c\u811a\u672c\u3002\u5982\u679c\u4e0d\u60f3\u4fee\u6539\u6743\u9650\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u547d\u4ee4<code>python3 your_script.py<\/code>\u6765\u6267\u884c\u811a\u672c\u3002<\/p>\n<p><strong>3. \u5728Linux\u4e2d\u4f7f\u7528Python\u5f00\u53d1\u73af\u5883\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56\u5305\uff1f<\/strong><br \/>\u5728Linux\u4e0a\u7ba1\u7406Python\u9879\u76ee\u7684\u4f9d\u8d56\u5305\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u3002\u4e3a\u4e86\u786e\u4fdd\u9879\u76ee\u7684\u4f9d\u8d56\u6027\u6e05\u6670\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>requirements.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5217\u51fa\u6240\u6709\u9700\u8981\u7684\u5305\u53ca\u5176\u7248\u672c\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c\u547d\u4ee4<code>pip install -r requirements.txt<\/code>\u5373\u53ef\u81ea\u52a8\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>virtualenv<\/code>\u6216<code>venv<\/code>\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u53ef\u4ee5\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u9879\u76ee\u63d0\u4f9b\u72ec\u7acb\u7684\u73af\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Linux\u7cfb\u7edf\u4f7f\u7528Python\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u5b89\u88c5Python\u3001\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u811a\u672c\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883 [&hellip;]","protected":false},"author":3,"featured_media":1149462,"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\/1149458"}],"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=1149458"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149458\/revisions"}],"predecessor-version":[{"id":1149463,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149458\/revisions\/1149463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149462"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}