{"id":1167204,"date":"2025-01-15T15:41:23","date_gmt":"2025-01-15T07:41:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167204.html"},"modified":"2025-01-15T15:41:25","modified_gmt":"2025-01-15T07:41:25","slug":"python%e7%bc%96%e5%86%99%e5%ae%8c%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167204.html","title":{"rendered":"python\u7f16\u5199\u5b8c\u7a0b\u5e8f\u5982\u4f55\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25211151\/3ad89763-3525-4829-9dfd-a9f102e9e897.webp\" alt=\"python\u7f16\u5199\u5b8c\u7a0b\u5e8f\u5982\u4f55\u8fd0\u884c\" \/><\/p>\n<p><p> \u8981\u8fd0\u884c\u4e00\u4e2a\u7f16\u5199\u5b8c\u6210\u7684Python\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u547d\u4ee4\u884c\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6216\u4f7f\u7528Jupyter Notebook<\/strong>\u6765\u5b9e\u73b0\u3002<strong>\u547d\u4ee4\u884c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00<\/strong>\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fd0\u884cPython\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><h4>1. \u786e\u4fdd\u5df2\u5b89\u88c5Python<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/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\u88c5Python\uff0c\u53ef\u4ee5\u8bbf\u95eePython\u5b98\u7f51\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%E4%B8%8B%E8%BD%BD%E5%B9%B6%E5%AE%89%E8%A3%85%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E3%80%82\">https:\/\/www.python.org\/\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002<\/a><\/p>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u7f16\u5199Python\u4ee3\u7801\u5e76\u4fdd\u5b58\u4e3a\u4e00\u4e2a<code>.py<\/code>\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>hello.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\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. \u5728\u547d\u4ee4\u884c\u4e2d\u5bfc\u822a\u5230\u811a\u672c\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u547d\u4ee4\u884c\u7a97\u53e3\uff08Windows\u7684cmd\u6216PowerShell\uff0cMac\u548cLinux\u7684Terminal\uff09\uff0c\u5e76\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5bfc\u822a\u5230\u5305\u542b<code>hello.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd path\/to\/your\/script<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u8fd0\u884c\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u6b64\u547d\u4ee4\u540e\uff0c\u4f60\u5e94\u8be5\u4f1a\u5728\u547d\u4ee4\u884c\u4e2d\u770b\u5230\u8f93\u51fa<code>Hello, World!<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/h3>\n<\/p>\n<p><h4>1. \u9009\u62e9\u548c\u5b89\u88c5IDE<\/h4>\n<\/p>\n<p><p>\u6709\u8bb8\u591a\u4f18\u79c0\u7684IDE\u652f\u6301Python\u7f16\u7a0b\uff0c\u4f8b\u5982PyCharm\u3001VSCode\u3001Eclipse + PyDev\u3001Atom\u7b49\u3002\u9009\u62e9\u4e00\u4e2a\u4f60\u559c\u6b22\u7684\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2. \u6253\u5f00\u548c\u914d\u7f6eIDE<\/h4>\n<\/p>\n<p><p>\u6253\u5f00\u4f60\u7684IDE\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\u6216\u6253\u5f00\u73b0\u6709\u9879\u76ee\u3002\u7136\u540e\u5c06\u4f60\u7684Python\u811a\u672c\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3. \u8fd0\u884cPython\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570IDE\u90fd\u63d0\u4f9b\u4e86\u8fd0\u884c\u6309\u94ae\uff08\u901a\u5e38\u662f\u7eff\u8272\u7684\u4e09\u89d2\u5f62\uff09\u3002\u70b9\u51fb\u8be5\u6309\u94ae\u5373\u53ef\u8fd0\u884c\u4f60\u7684Python\u811a\u672c\uff0c\u5e76\u5728IDE\u7684\u63a7\u5236\u53f0\u7a97\u53e3\u4e2d\u67e5\u770b\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Jupyter Notebook<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Jupyter Notebook\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u542f\u52a8Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\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\u4e00\u4e2a\u65b0\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u6216\u6807\u7b7e\uff0c\u663e\u793aJupyter Notebook\u7684\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>3. \u521b\u5efa\u548c\u8fd0\u884cNotebook<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u754c\u9762\u4e2d\uff0c\u70b9\u51fb\u201cNew\u201d\u6309\u94ae\u5e76\u9009\u62e9\u201cPython 3\u201d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Notebook\u3002\u5728Notebook\u5355\u5143\u683c\u4e2d\u8f93\u5165\u4f60\u7684Python\u4ee3\u7801\uff0c\u7136\u540e\u6309Shift + Enter\u8fd0\u884c\u8be5\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u8bd5\u548c\u4f18\u5316Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u547d\u4ee4\u884c\u3001IDE\u8fd8\u662fJupyter Notebook\uff0c\u8c03\u8bd5\u90fd\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5927\u591a\u6570IDE\u90fd\u5185\u7f6e\u4e86\u8c03\u8bd5\u5668\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u4ee3\u7801\u3001\u76d1\u63a7\u53d8\u91cf\u7b49\u3002\u4f8b\u5982\uff0c\u5728PyCharm\u4e2d\uff0c\u4f60\u53ef\u4ee5\u53f3\u952e\u70b9\u51fb\u4ee3\u7801\u884c\u5e76\u9009\u62e9\u201cToggle Breakpoint\u201d\u6765\u8bbe\u7f6e\u65ad\u70b9\uff0c\u7136\u540e\u70b9\u51fb\u8c03\u8bd5\u6309\u94ae\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u5c0f\u866b\u5b50\u56fe\u6807\uff09\u6765\u542f\u52a8\u8c03\u8bd5\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u4ee3\u7801\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u4f18\u5316Python\u4ee3\u7801\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u51cf\u5c11\u8d44\u6e90\u6d88\u8017\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/strong>\uff1a\u5c3d\u91cf\u51cf\u5c11\u5faa\u73af\u4e2d\u7684\u8ba1\u7b97\u91cf\uff0c\u4f7f\u7528\u7f13\u5b58\u6216\u4e2d\u95f4\u53d8\u91cf\u5b58\u50a8\u7ed3\u679c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/strong>\uff1aPython\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u901a\u5e38\u7ecf\u8fc7\u4f18\u5316\uff0c\u6548\u7387\u8f83\u9ad8\uff0c\u5c3d\u91cf\u4f7f\u7528\u5b83\u4eec\u800c\u4e0d\u662f\u81ea\u884c\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u3001\u96c6\u5408\u3001\u5b57\u5178\u7b49\u3002<\/li>\n<li><strong>\u5e76\u884c\u548c\u5f02\u6b65\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<\/ul>\n<p><h4>3. \u4ee3\u7801\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5\u662f\u786e\u4fdd\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684unittest\u5e93\u6216\u7b2c\u4e09\u65b9\u5e93\u5982pytest\u6765\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528unittest\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestMathFunctions(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8be5\u6d4b\u8bd5\u6587\u4ef6\uff0c\u67e5\u770b\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6253\u5305\u548c\u53d1\u5e03Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>1. \u6253\u5305\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u5b8c\u6210\u4e86Python\u9879\u76ee\u5e76\u5e0c\u671b\u5206\u4eab\u6216\u90e8\u7f72\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u6253\u5305\u4e3a\u53ef\u5b89\u88c5\u7684\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528setuptools\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;your_project_name&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # \u5217\u51fa\u9879\u76ee\u6240\u9700\u7684\u4f9d\u8d56\u5305\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>        &#39;numpy&#39;,<\/p>\n<p>        &#39;pandas&#39;,<\/p>\n<p>    ],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5305\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a<code>dist<\/code>\u76ee\u5f55\uff0c\u5305\u542b\u4e00\u4e2a<code>.tar.gz<\/code>\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u5206\u4eab\u7ed9\u5176\u4ed6\u4eba\u3002<\/p>\n<\/p>\n<p><h4>2. \u53d1\u5e03\u5230PyPI<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5c06\u9879\u76ee\u53d1\u5e03\u5230Python Package Index (PyPI)\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528twine\u5de5\u5177\u3002\u9996\u5148\uff0c\u5b89\u88c5twine\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5305\u4e0a\u4f20\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5728PyPI\u4e0a\u6ce8\u518c\u4e00\u4e2a\u8d26\u6237\uff0c\u5e76\u5728\u4e0a\u4f20\u65f6\u63d0\u4f9b\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u4e0a\u4f20\u6210\u529f\u540e\uff0c\u5176\u4ed6\u4eba\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u4f60\u7684\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install your_project_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u52a8\u5316\u548c\u6301\u7eed\u96c6\u6210<\/h3>\n<\/p>\n<p><h4>1. \u81ea\u52a8\u5316\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\u6765\u6267\u884c\u5e38\u89c1\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Makefile\u6216shell\u811a\u672c\u6765\u81ea\u52a8\u5316\u6d4b\u8bd5\u3001\u6253\u5305\u548c\u90e8\u7f72\u6d41\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Makefile\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-makefile\">test:<\/p>\n<p>    python -m unittest discover<\/p>\n<p>package:<\/p>\n<p>    python setup.py sdist<\/p>\n<p>deploy:<\/p>\n<p>    twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>make test<\/code>\u3001<code>make package<\/code>\u6216<code>make deploy<\/code>\u5373\u53ef\u6267\u884c\u76f8\u5e94\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2. \u6301\u7eed\u96c6\u6210<\/h4>\n<\/p>\n<p><p>\u6301\u7eed\u96c6\u6210\uff08CI\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u5b9e\u8df5\uff0c\u901a\u8fc7\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u6784\u5efa\u6765\u63d0\u9ad8\u8f6f\u4ef6\u8d28\u91cf\u548c\u4ea4\u4ed8\u6548\u7387\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528CI\u5de5\u5177\uff08\u5982Jenkins\u3001Travis CI\u3001CircleCI\u7b49\uff09\u6765\u81ea\u52a8\u8fd0\u884c\u6d4b\u8bd5\u548c\u90e8\u7f72\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Travis CI\u8fdb\u884c\u6301\u7eed\u96c6\u6210\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a<code>.travis.yml<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">language: python<\/p>\n<p>python:<\/p>\n<p>  - &quot;3.8&quot;<\/p>\n<p>install:<\/p>\n<p>  - pip install -r requirements.txt<\/p>\n<p>script:<\/p>\n<p>  - python -m unittest discover<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5c06\u9879\u76ee\u63a8\u9001\u5230GitHub\uff0c\u5e76\u5728Travis CI\u7f51\u7ad9\u4e0a\u542f\u7528\u4ed3\u5e93\u3002\u6bcf\u6b21\u63a8\u9001\u4ee3\u7801\u65f6\uff0cTravis CI\u5c06\u81ea\u52a8\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u90e8\u7f72Python\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u90e8\u7f72\u5230Web\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684Python\u9879\u76ee\u662f\u4e00\u4e2aWeb\u5e94\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528Web\u670d\u52a1\u5668\uff08\u5982Nginx\u3001Apache\uff09\u548c\u5e94\u7528\u670d\u52a1\u5668\uff08\u5982Gunicorn\u3001uWSGI\uff09\u8fdb\u884c\u90e8\u7f72\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Gunicorn\u548cNginx\u90e8\u7f72Flask\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Gunicorn\u548cFlask\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gunicorn flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff08<code>app.py<\/code>\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Gunicorn\u8fd0\u884cFlask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gunicorn -w 4 -b 0.0.0.0:8000 app:app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u914d\u7f6eNginx\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230Gunicorn\u3002\u521b\u5efa\u4e00\u4e2aNginx\u914d\u7f6e\u6587\u4ef6\uff08\u4f8b\u5982<code>\/etc\/nginx\/sites-available\/myapp<\/code>\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-nginx\">server {<\/p>\n<p>    listen 80;<\/p>\n<p>    server_name your_domain.com;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass http:\/\/127.0.0.1:8000;<\/p>\n<p>        proxy_set_header Host $host;<\/p>\n<p>        proxy_set_header X-Real-IP $remote_addr;<\/p>\n<p>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n<p>        proxy_set_header X-Forwarded-Proto $scheme;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u542f\u7528\u914d\u7f6e\u5e76\u91cd\u542fNginx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/<\/p>\n<p>sudo systemctl restart nginx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u7684Flask\u5e94\u7528\u5df2\u7ecf\u90e8\u7f72\u5e76\u53ef\u4ee5\u901a\u8fc7Web\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>2. \u90e8\u7f72\u5230\u4e91\u5e73\u53f0<\/h4>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5c06Python\u5e94\u7528\u90e8\u7f72\u5230\u4e91\u5e73\u53f0\uff0c\u5982AWS\u3001Google Cloud\u3001Heroku\u7b49\u3002\u4ee5\u4e0b\u662f\u5c06Flask\u5e94\u7528\u90e8\u7f72\u5230Heroku\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Heroku CLI\u5e76\u767b\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl https:\/\/cli-assets.heroku.com\/install.sh | sh<\/p>\n<p>heroku login<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a<code>Procfile<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-txt\">web: gunicorn app:app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521d\u59cb\u5316Git\u4ed3\u5e93\u5e76\u63d0\u4ea4\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">git init<\/p>\n<p>git add .<\/p>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Heroku\u5e94\u7528\u5e76\u63a8\u9001\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">heroku create<\/p>\n<p>git push heroku master<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5e94\u7528\u90e8\u7f72\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Heroku\u63d0\u4f9b\u7684URL\u8bbf\u95ee\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001Python\u4ee3\u7801\u89c4\u8303\u548c\u6587\u6863<\/h3>\n<\/p>\n<p><h4>1. \u4ee3\u7801\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u9075\u5faa\u4ee3\u7801\u89c4\u8303\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002Python\u63a8\u8350\u4f7f\u7528PEP 8\u4ee3\u7801\u89c4\u8303\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u89c4\u8303\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7f29\u8fdb\u4f7f\u75284\u4e2a\u7a7a\u683c<\/li>\n<li>\u6bcf\u884c\u4ee3\u7801\u4e0d\u8d85\u8fc779\u4e2a\u5b57\u7b26<\/li>\n<li>\u51fd\u6570\u548c\u53d8\u91cf\u540d\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf<\/li>\n<li>\u7c7b\u540d\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5<\/li>\n<li>\u5728\u6a21\u5757\u548c\u7c7b\u4e4b\u95f4\u4f7f\u7528\u4e24\u4e2a\u7a7a\u884c\uff0c\u5728\u51fd\u6570\u548c\u65b9\u6cd5\u4e4b\u95f4\u4f7f\u7528\u4e00\u4e2a\u7a7a\u884c<\/li>\n<\/ul>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982<code>flake8<\/code>\u6216<code>pylint<\/code>\u6765\u68c0\u67e5\u4ee3\u7801\u662f\u5426\u7b26\u5408\u89c4\u8303\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flake8<\/p>\n<p>flake8 your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u826f\u597d\u7684\u6587\u6863\u5bf9\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528docstring\u548c\u6ce8\u91ca\u6765\u7f16\u5199\u6587\u6863\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int): \u7b2c\u4e00\u4e2a\u6570<\/p>\n<p>    b (int): \u7b2c\u4e8c\u4e2a\u6570<\/p>\n<p>    \u8fd4\u56de\u503c:<\/p>\n<p>    int: a\u548cb\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982Sphinx\u751f\u6210\u9879\u76ee\u7684HTML\u6587\u6863\u3002\u9996\u5148\uff0c\u5b89\u88c5Sphinx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sphinx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u521d\u59cb\u5316Sphinx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sphinx-quickstart<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6309\u7167\u63d0\u793a\u914d\u7f6eSphinx\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210HTML\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">make html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684HTML\u6587\u6863\u5c06\u4f4d\u4e8e<code>_build\/html<\/code>\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b66\u4e60\u548c\u63d0\u5347Python\u6280\u80fd<\/h3>\n<\/p>\n<p><h4>1. \u9605\u8bfb\u548c\u5b9e\u8df5<\/h4>\n<\/p>\n<p><p>\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u3001\u4e66\u7c4d\u548c\u535a\u5ba2\u662f\u63d0\u5347Python\u6280\u80fd\u7684\u91cd\u8981\u9014\u5f84\u3002\u4f8b\u5982\uff0c\u300aPython\u7f16\u7a0b\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b\u3001\u300a\u6d41\u7545\u7684Python\u300b\u548c\u300aPython Cookbook\u300b\u662f\u4e00\u4e9b\u7ecf\u5178\u7684\u4e66\u7c4d\u3002<\/p>\n<\/p>\n<p><h4>2. \u53c2\u4e0e\u5f00\u6e90\u9879\u76ee<\/h4>\n<\/p>\n<p><p>\u53c2\u4e0e\u5f00\u6e90\u9879\u76ee\u662f\u63d0\u9ad8\u7f16\u7a0b\u6280\u80fd\u548c\u79ef\u7d2f\u7ecf\u9a8c\u7684\u597d\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5728GitHub\u3001GitLab\u7b49\u5e73\u53f0\u4e0a\u627e\u5230\u611f\u5174\u8da3\u7684\u9879\u76ee\uff0c\u63d0\u4ea4\u95ee\u9898\u548c\u8865\u4e01\uff0c\u4e0e\u5176\u4ed6\u5f00\u53d1\u8005\u5408\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>3. \u53c2\u52a0\u793e\u533a\u6d3b\u52a8<\/h4>\n<\/p>\n<p><p>\u53c2\u52a0Python\u793e\u533a\u7684\u6d3b\u52a8\uff0c\u5982PyCon\u4f1a\u8bae\u3001\u7ebf\u4e0b\u805a\u4f1a\u3001\u7ebf\u4e0a\u7814\u8ba8\u4f1a\u7b49\uff0c\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5f00\u53d1\u8005\u4ea4\u6d41\u7ecf\u9a8c\uff0c\u5b66\u4e60\u65b0\u6280\u672f\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8fd0\u884cPython\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u547d\u4ee4\u884c\u3001IDE\u548cJupyter Notebook\u3002\u547d\u4ee4\u884c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5404\u79cd\u64cd\u4f5c\u7cfb\u7edf\u3002\u9664\u4e86\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u8c03\u8bd5\u3001\u4f18\u5316\u3001\u6d4b\u8bd5\u3001\u6253\u5305\u3001\u53d1\u5e03\u548c\u90e8\u7f72\u4ee3\u7801\u4e5f\u662f\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u9075\u5faa\u4ee3\u7801\u89c4\u8303\u3001\u7f16\u5199\u6587\u6863\u3001\u5b66\u4e60\u548c\u63d0\u5347Python\u6280\u80fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6210\u4e3a\u66f4\u597d\u7684Python\u5f00\u53d1\u8005\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u7f16\u7a0b\u4e4b\u65c5\u4e2d\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u9996\u5148\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5Python\u3002\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5bfc\u822a\u5230\u5b58\u50a8\u7a0b\u5e8f\u6587\u4ef6\u7684\u76ee\u5f55\uff0c\u8f93\u5165<code>python \u6587\u4ef6\u540d.py<\/code>\uff08\u5728Windows\u4e0a\uff09\u6216<code>python3 \u6587\u4ef6\u540d.py<\/code>\uff08\u5728macOS\u6216Linux\u4e0a\uff09\uff0c\u6309\u56de\u8f66\u952e\u5373\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\u8fd0\u884cPython\u7a0b\u5e8f\uff1f<\/strong><br \/>\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08\u5982PyCharm\u3001Visual Studio Code\u6216Jupyter Notebook\uff09\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fd0\u884cPython\u7a0b\u5e8f\u3002\u5728IDE\u4e2d\u6253\u5f00\u60a8\u7684Python\u6587\u4ef6\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2a\u201c\u8fd0\u884c\u201d\u6309\u94ae\u6216\u9009\u9879\uff0c\u70b9\u51fb\u540e\u7a0b\u5e8f\u4f1a\u5728\u5185\u7f6e\u7ec8\u7aef\u4e2d\u6267\u884c\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5feb\u6377\u952e\u6765\u52a0\u901f\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5Python\u7a0b\u5e8f\u4ee5\u89e3\u51b3\u8fd0\u884c\u65f6\u9519\u8bef\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u4f7f\u7528IDE\u7684\u8c03\u8bd5\u529f\u80fd\u53ef\u4ee5\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u5e76\u68c0\u67e5\u53d8\u91cf\u503c\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u811a\u672c\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0<code>print()<\/code>\u8bed\u53e5\u6765\u8f93\u51fa\u53d8\u91cf\u72b6\u6001\uff0c\u5e2e\u52a9\u8bc6\u522b\u95ee\u9898\u6240\u5728\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8fd0\u884c\u4e00\u4e2a\u7f16\u5199\u5b8c\u6210\u7684Python\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6216\u4f7f\u7528Jupyter Notebo [&hellip;]","protected":false},"author":3,"featured_media":1167206,"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\/1167204"}],"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=1167204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167204\/revisions"}],"predecessor-version":[{"id":1167207,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167204\/revisions\/1167207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167206"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}