{"id":1182182,"date":"2025-01-15T19:00:17","date_gmt":"2025-01-15T11:00:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182182.html"},"modified":"2025-01-15T19:00:19","modified_gmt":"2025-01-15T11:00:19","slug":"python%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e8%bd%af%e4%bb%b6%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182182.html","title":{"rendered":"python\u5982\u4f55\u5efa\u7acb\u8f6f\u4ef6\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130253\/b4e9c0e3-020b-4d07-9821-3247df16e2fc.webp\" alt=\"python\u5982\u4f55\u5efa\u7acb\u8f6f\u4ef6\u5305\" \/><\/p>\n<p><p> <strong>Python\u5efa\u7acb\u8f6f\u4ef6\u5305\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u3001\u7f16\u5199\u4ee3\u7801\u3001\u6dfb\u52a0\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u3001\u7f16\u5199\u6d4b\u8bd5\u3001\u751f\u6210\u5206\u53d1\u5305\u548c\u53d1\u5e03\u5230PyPI\u7b49\u3002<\/strong>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u9879\u76ee\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u76ee\u5f55\u5e76\u8fdb\u5165\u8be5\u76ee\u5f55\u3002\u8fd9\u4e2a\u76ee\u5f55\u5c06\u5305\u542b\u4f60\u7684\u5305\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mkdir my_package<\/p>\n<p>cd my_package<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u5b50\u76ee\u5f55\uff0c\u7528\u4e8e\u5b58\u653e\u4f60\u7684\u5305\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">mkdir my_package<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5305\u7684\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u662f\u7a7a\u7684\uff0c\u4f46\u5b83\u7684\u5b58\u5728\u544a\u8bc9Python\u8fd9\u4e2a\u76ee\u5f55\u5e94\u8be5\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">touch my_package\/__init__.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e2d\u6dfb\u52a0\u4f60\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>module.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/module.py<\/p>\n<p>def hello():<\/p>\n<p>    print(&quot;Hello, world!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1. <code>setup.py<\/code><\/h4>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u91cd\u8981\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u5173\u4e8e\u4f60\u7684\u5305\u7684\u6240\u6709\u4fe1\u606f\u3002\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\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=&quot;my_package&quot;,<\/p>\n<p>    version=&quot;0.1.0&quot;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # \u5728\u8fd9\u91cc\u5217\u51fa\u4f60\u7684\u4f9d\u8d56\u5305<\/p>\n<p>    ],<\/p>\n<p>    entry_points={<\/p>\n<p>        &#39;console_scripts&#39;: [<\/p>\n<p>            # \u5728\u8fd9\u91cc\u5b9a\u4e49\u4f60\u7684\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<p>        ],<\/p>\n<p>    },<\/p>\n<p>    author=&quot;\u4f60\u7684\u540d\u5b57&quot;,<\/p>\n<p>    author_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l=&quot;\u4f60\u7684\u90ae\u7bb1&quot;,<\/p>\n<p>    description=&quot;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8bPython\u5305&quot;,<\/p>\n<p>    long_description=open(&#39;README.md&#39;).read(),<\/p>\n<p>    long_description_content_type=&#39;text\/markdown&#39;,<\/p>\n<p>    url=&quot;https:\/\/github.com\/\u4f60\u7684\u7528\u6237\u540d\/\u4f60\u7684\u4ed3\u5e93\u540d&quot;,<\/p>\n<p>    classifiers=[<\/p>\n<p>        &quot;Programming Language :: Python :: 3&quot;,<\/p>\n<p>        &quot;License :: OSI Approved :: MIT License&quot;,<\/p>\n<p>        &quot;Operating System :: OS Independent&quot;,<\/p>\n<p>    ],<\/p>\n<p>    python_requires=&#39;&gt;=3.6&#39;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>README.md<\/code><\/h4>\n<\/p>\n<p><p>\u8fd9\u662f\u4f60\u7684\u5305\u7684\u8bf4\u660e\u6587\u4ef6\uff0c\u5305\u542b\u6709\u5173\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528\u4f60\u7684\u5305\u7684\u4fe1\u606f\u3002\u521b\u5efa\u4e00\u4e2a<code>README.md<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-markdown\"># My Package<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8bPython\u5305\u3002<\/p>\n<p>## \u5b89\u88c5<\/p>\n<p>```bash<\/p>\n<p>pip install my_package<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package.module import hello<\/p>\n<p>hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code><\/p>\n<p>#### 3. `MANIFEST.in`<\/p>\n<p>\u8fd9\u4e2a\u6587\u4ef6\u6307\u5b9a\u4e86\u54ea\u4e9b\u6587\u4ef6\u5e94\u8be5\u5305\u542b\u5728\u5206\u53d1\u5305\u4e2d\u3002\u521b\u5efa\u4e00\u4e2a`MANIFEST.in`\u6587\u4ef6\uff1a<\/p>\n<p>```text<\/p>\n<p>include README.md<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4f60\u7684\u5305\u7f16\u5199\u6d4b\u8bd5\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u4f60\u53ef\u4ee5\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>tests<\/code>\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u6d4b\u8bd5\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>test_module.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># tests\/test_module.py<\/p>\n<p>import unittest<\/p>\n<p>from my_package.module import hello<\/p>\n<p>class TestModule(unittest.TestCase):<\/p>\n<p>    def test_hello(self):<\/p>\n<p>        self.assertEqual(hello(), &quot;Hello, world!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u5206\u53d1\u5305<\/h3>\n<\/p>\n<p><p>\u5728\u751f\u6210\u5206\u53d1\u5305\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>setuptools<\/code>\u548c<code>wheel<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install setuptools wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u5206\u53d1\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728<code>dist<\/code>\u76ee\u5f55\u4e0b\u751f\u6210\u4f60\u7684\u5206\u53d1\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u53d1\u5e03\u5230PyPI<\/h3>\n<\/p>\n<p><h4>1. \u6ce8\u518cPyPI\u8d26\u6237<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709PyPI\u8d26\u6237\uff0c\u9996\u5148\u9700\u8981\u6ce8\u518c\u4e00\u4e2a\uff1a<a href=\"https:\/\/pypi.org\/account\/register\/\">https:\/\/pypi.org\/account\/register\/<\/a><\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5Twine<\/h4>\n<\/p>\n<p><p>Twine\u662f\u7528\u4e8e\u53d1\u5e03Python\u5305\u7684\u5de5\u5177\u3002\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><h4>3. \u4e0a\u4f20\u5305\u5230PyPI<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Twine\u5c06\u4f60\u7684\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\u5c06\u9700\u8981\u8f93\u5165\u4f60\u7684PyPI\u8d26\u6237\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5305\u7684\u7248\u672c\u63a7\u5236<\/h3>\n<\/p>\n<p><p><strong>\u7248\u672c\u63a7\u5236<\/strong>\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u4e0d\u65ad\u5730\u5bf9\u4ee3\u7801\u8fdb\u884c\u4fee\u6539\u548c\u6539\u8fdb\uff0c\u6bcf\u6b21\u4fee\u6539\u540e\uff0c\u4f60\u9700\u8981\u66f4\u65b0\u5305\u7684\u7248\u672c\u53f7\u3002\u5e38\u89c1\u7684\u7248\u672c\u53f7\u683c\u5f0f\u662f<code>MAJOR.MINOR.PATCH<\/code>\uff0c\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>MAJOR<\/strong>\uff1a\u91cd\u5927\u66f4\u65b0\uff0c\u53ef\u80fd\u4f1a\u7834\u574f\u5411\u540e\u517c\u5bb9\u6027\u3002<\/li>\n<li><strong>MINOR<\/strong>\uff1a\u65b0\u529f\u80fd\u66f4\u65b0\uff0c\u5411\u540e\u517c\u5bb9\u3002<\/li>\n<li><strong>PATCH<\/strong>\uff1a\u5c0f\u7684\u4fee\u590d\u548c\u6539\u8fdb\uff0c\u5411\u540e\u517c\u5bb9\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u7248\u672c\u53f7\u4ece<code>0.1.0<\/code>\u66f4\u65b0\u4e3a<code>0.2.0<\/code>\uff0c\u4ee5\u8868\u793a\u6dfb\u52a0\u4e86\u65b0\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5305\u7684\u4f9d\u8d56\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728<code>setup.py<\/code>\u4e2d\u7684<code>install_requires<\/code>\u90e8\u5206\uff0c\u4f60\u53ef\u4ee5\u5217\u51fa\u4f60\u7684\u5305\u6240\u4f9d\u8d56\u7684\u5176\u4ed6Python\u5305\u3002\u8fd9\u4e9b\u5305\u5c06\u5728\u7528\u6237\u5b89\u88c5\u4f60\u7684\u5305\u65f6\u81ea\u52a8\u5b89\u88c5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">install_requires=[<\/p>\n<p>    &#39;numpy&gt;=1.18.0&#39;,<\/p>\n<p>    &#39;requests&gt;=2.23.0&#39;<\/p>\n<p>],<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5305\u7684\u547d\u540d\u7a7a\u95f4<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5305\u53ef\u80fd\u4e0e\u5176\u4ed6\u5305\u53d1\u751f\u547d\u540d\u51b2\u7a81\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u5305\u3002\u547d\u540d\u7a7a\u95f4\u5305\u5141\u8bb8\u4f60\u5c06\u5305\u7684\u591a\u4e2a\u90e8\u5206\u5206\u5e03\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\u3002\u8981\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u5305\uff0c\u53ea\u9700\u5728<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/__init__.py<\/p>\n<p>__path__ = __import__(&#39;pkgutil&#39;).extend_path(__path__, __name__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5305\u7684\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u4e3a\u4f60\u7684\u5305\u7f16\u5199\u6587\u6863\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u9664\u4e86<code>README.md<\/code>\u6587\u4ef6\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Sphinx\u751f\u6210\u66f4\u8be6\u7ec6\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Sphinx<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sphinx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521d\u59cb\u5316Sphinx<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\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\u3002\u5b8c\u6210\u540e\uff0c\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542bSphinx\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h4>3. \u7f16\u5199\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5728Sphinx\u751f\u6210\u7684<code>docs<\/code>\u76ee\u5f55\u4e0b\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u548c\u7f16\u8f91\u4f60\u7684\u6587\u6863\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528reStructuredText\uff08.rst\uff09\u6216Markdown\uff08.md\uff09\u683c\u5f0f\u7f16\u5199\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>4. \u751f\u6210HTML\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5728<code>docs<\/code>\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">make html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728<code>docs\/_build\/html<\/code>\u76ee\u5f55\u4e0b\u751f\u6210HTML\u683c\u5f0f\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5305\u7684\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6d4b\u8bd5\u662f\u786e\u4fdd\u4f60\u7684\u5305\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u6b63\u5e38\u8fd0\u884c\u7684\u5173\u952e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u3001<code>pytest<\/code>\u7b49\u6d4b\u8bd5\u6846\u67b6\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pytest<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728<code>tests<\/code>\u76ee\u5f55\u4e0b\u7f16\u5199\u6d4b\u8bd5\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>test_module.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># tests\/test_module.py<\/p>\n<p>import pytest<\/p>\n<p>from my_package.module import hello<\/p>\n<p>def test_hello():<\/p>\n<p>    assert hello() == &quot;Hello, world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8fd0\u884c\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5305\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/devops\/52691.html\" target=\"_blank\">CI\/CD<\/a><\/h3>\n<\/p>\n<p><p>\u6301\u7eed\u96c6\u6210\uff08CI\uff09\u548c\u6301\u7eed\u4ea4\u4ed8\uff08CD\uff09\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u5b9e\u8df5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Travis CI\u3001GitHub Actions\u7b49\u5de5\u5177\u81ea\u52a8\u5316\u4f60\u7684\u6d4b\u8bd5\u548c\u53d1\u5e03\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6eTravis CI<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\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.6&quot;<\/p>\n<p>  - &quot;3.7&quot;<\/p>\n<p>  - &quot;3.8&quot;<\/p>\n<p>  - &quot;3.9&quot;<\/p>\n<p>install:<\/p>\n<p>  - pip install -r requirements.txt<\/p>\n<p>  - pip install pytest<\/p>\n<p>script:<\/p>\n<p>  - pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u914d\u7f6eGitHub Actions<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>.github\/workflows\/test.yml<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">name: Test<\/p>\n<p>on: [push, pull_request]<\/p>\n<p>jobs:<\/p>\n<p>  test:<\/p>\n<p>    runs-on: ubuntu-latest<\/p>\n<p>    strategy:<\/p>\n<p>      matrix:<\/p>\n<p>        python-version: [3.6, 3.7, 3.8, 3.9]<\/p>\n<p>    steps:<\/p>\n<p>    - uses: actions\/checkout@v2<\/p>\n<p>    - name: Set up Python ${{ matrix.python-version }}<\/p>\n<p>      uses: actions\/setup-python@v2<\/p>\n<p>      with:<\/p>\n<p>        python-version: ${{ matrix.python-version }}<\/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>        pip install pytest<\/p>\n<p>    - name: Run tests<\/p>\n<p>      run: |<\/p>\n<p>        pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5305\u7684\u5206\u53d1\u548c\u4f9d\u8d56\u7ba1\u7406<\/h3>\n<\/p>\n<p><p><strong>\u5206\u53d1\u548c\u4f9d\u8d56\u7ba1\u7406<\/strong>\u662fPython\u8f6f\u4ef6\u5305\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u73af\u8282\u3002\u786e\u4fdd\u4f60\u7684\u5305\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u987a\u5229\u5b89\u88c5\u548c\u8fd0\u884c\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>requirements.txt<\/code><\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\uff0c\u5217\u51fa\u4f60\u7684\u5305\u6240\u4f9d\u8d56\u7684\u6240\u6709\u7b2c\u4e09\u65b9\u5e93\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-text\">numpy&gt;=1.18.0<\/p>\n<p>requests&gt;=2.23.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u7528\u6237\u5728\u5b89\u88c5\u4f60\u7684\u5305\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u5b89\u88c5\u8fd9\u4e9b\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><h4>2. \u4f7f\u7528<code>Pipenv<\/code>\u6216<code>Poetry<\/code><\/h4>\n<\/p>\n<p><p><code>Pipenv<\/code>\u548c<code>Poetry<\/code>\u662f\u73b0\u4ee3Python\u9879\u76ee\u7684\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u4f9d\u8d56\u548c\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h5>\u4f7f\u7528Pipenv<\/h5>\n<\/p>\n<p><p>\u5b89\u88c5Pipenv\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pipenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521d\u59cb\u5316Pipenv\u73af\u5883\u5e76\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pipenv install numpy requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u4f7f\u7528Poetry<\/h5>\n<\/p>\n<p><p>\u5b89\u88c5Poetry\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install poetry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521d\u59cb\u5316Poetry\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry init<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry add numpy requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5305\u7684\u53d1\u5e03\u548c\u7ef4\u62a4<\/h3>\n<\/p>\n<p><p>\u53d1\u5e03\u548c\u7ef4\u62a4\u4f60\u7684\u5305\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4e0d\u65ad\u5730\u6539\u8fdb\u548c\u66f4\u65b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u671f\u66f4\u65b0<\/h4>\n<\/p>\n<p><p>\u5b9a\u671f\u66f4\u65b0\u4f60\u7684\u5305\uff0c\u4fee\u590d\u5df2\u77e5\u95ee\u9898\u548c\u6dfb\u52a0\u65b0\u529f\u80fd\u3002\u6bcf\u6b21\u66f4\u65b0\u65f6\uff0c\u786e\u4fdd\u66f4\u65b0\u7248\u672c\u53f7\u548c\u53d1\u5e03\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h4>2. \u54cd\u5e94\u7528\u6237\u53cd\u9988<\/h4>\n<\/p>\n<p><p>\u53ca\u65f6\u54cd\u5e94\u7528\u6237\u7684\u53cd\u9988\u548c\u95ee\u9898\uff0c\u5728GitHub\u7b49\u5e73\u53f0\u4e0a\u4e0e\u7528\u6237\u4e92\u52a8\uff0c\u4e86\u89e3\u4ed6\u4eec\u7684\u9700\u6c42\u548c\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>3. \u6301\u7eed\u6539\u8fdb\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4e0d\u65ad\u6539\u8fdb\u548c\u66f4\u65b0\u4f60\u7684\u6587\u6863\uff0c\u786e\u4fdd\u7528\u6237\u80fd\u591f\u8f7b\u677e\u5730\u7406\u89e3\u548c\u4f7f\u7528\u4f60\u7684\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u793a\u4f8b\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-text\">my_package\/<\/p>\n<p>\u251c\u2500\u2500 my_package\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 module.py<\/p>\n<p>\u251c\u2500\u2500 tests\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 test_module.py<\/p>\n<p>\u251c\u2500\u2500 .travis.yml<\/p>\n<p>\u251c\u2500\u2500 .github\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 workflows\/<\/p>\n<p>\u2502       \u2514\u2500\u2500 test.yml<\/p>\n<p>\u251c\u2500\u2500 README.md<\/p>\n<p>\u251c\u2500\u2500 MANIFEST.in<\/p>\n<p>\u251c\u2500\u2500 requirements.txt<\/p>\n<p>\u251c\u2500\u2500 setup.py<\/p>\n<p>\u251c\u2500\u2500 docs\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 conf.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 index.rst<\/p>\n<p>\u2502   \u2514\u2500\u2500 _build\/<\/p>\n<p>\u2502       \u2514\u2500\u2500 html\/<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u548c\u5efa\u8bae\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u3001\u53d1\u5e03\u548c\u7ef4\u62a4\u4e00\u4e2a\u9ad8\u8d28\u91cf\u7684Python\u8f6f\u4ef6\u5305\uff0c\u4e3a\u793e\u533a\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u5de5\u5177\u548c\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8f6f\u4ef6\u5305\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u8f6f\u4ef6\u5305\u901a\u5e38\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u662f\u7a7a\u7684\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u521d\u59cb\u5316\u4ee3\u7801\u3002\u60a8\u9700\u8981\u5728\u8be5\u76ee\u5f55\u4e0b\u6dfb\u52a0\u5176\u4ed6\u6a21\u5757\u6216\u5b50\u5305\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u547d\u540d\u548c\u7ec4\u7ec7\u7ed3\u6784\u3002\u901a\u5e38\uff0c\u60a8\u8fd8\u9700\u8981\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u5305\u7684\u5143\u6570\u636e\u548c\u4f9d\u8d56\u9879\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ba1\u7406Python\u8f6f\u4ef6\u5305\u7684\u4f9d\u8d56\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u521b\u5efaPython\u8f6f\u4ef6\u5305\u65f6\uff0c\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528<code>install_requires<\/code>\u53c2\u6570\u6765\u5217\u51fa\u6240\u9700\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u5217\u51fa\u548c\u7ba1\u7406\u9879\u76ee\u7684\u6240\u6709\u4f9d\u8d56\u9879\u4e5f\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u786e\u4fdd\u7528\u6237\u5728\u5b89\u88c5\u60a8\u7684\u8f6f\u4ef6\u5305\u65f6\u80fd\u591f\u81ea\u52a8\u5b89\u88c5\u6240\u9700\u7684\u4f9d\u8d56\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u6211\u7684Python\u8f6f\u4ef6\u5305\u53d1\u5e03\u5230PyPI\uff1f<\/strong><br \/>\u5c06\u60a8\u7684Python\u8f6f\u4ef6\u5305\u53d1\u5e03\u5230Python\u5305\u7d22\u5f15\uff08PyPI\uff09\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u65b9\u5f0f\u6765\u5206\u4eab\u60a8\u7684\u4ee3\u7801\u3002\u60a8\u9700\u8981\u9996\u5148\u6ce8\u518c\u4e00\u4e2aPyPI\u8d26\u6237\uff0c\u5e76\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u4e0a\u4f20\u60a8\u7684\u5305\u3002\u5728\u51c6\u5907\u4e0a\u4f20\u4e4b\u524d\uff0c\u786e\u4fdd\u60a8\u7684<code>setup.py<\/code>\u6587\u4ef6\u5305\u542b\u6240\u6709\u5fc5\u8981\u7684\u5143\u6570\u636e\uff0c\u5e76\u4e14\u60a8\u7684\u8f6f\u4ef6\u5305\u5df2\u7ecf\u6253\u5305\u4e3a<code>.tar.gz<\/code>\u6216<code>.whl<\/code>\u683c\u5f0f\u3002\u4f7f\u7528\u547d\u4ee4<code>twine upload dist\/*<\/code>\u53ef\u4ee5\u5c06\u60a8\u7684\u5305\u4e0a\u4f20\u5230PyPI\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5efa\u7acb\u8f6f\u4ef6\u5305\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u3001\u7f16\u5199\u4ee3\u7801\u3001\u6dfb\u52a0\u5fc5\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u3001\u7f16\u5199\u6d4b\u8bd5\u3001\u751f\u6210\u5206\u53d1\u5305\u548c\u53d1\u5e03\u5230P [&hellip;]","protected":false},"author":3,"featured_media":1182188,"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\/1182182"}],"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=1182182"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182182\/revisions"}],"predecessor-version":[{"id":1182190,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182182\/revisions\/1182190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182188"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}