{"id":1127303,"date":"2025-01-08T20:09:08","date_gmt":"2025-01-08T12:09:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127303.html"},"modified":"2025-01-08T20:09:10","modified_gmt":"2025-01-08T12:09:10","slug":"python-%e5%ba%94%e7%94%a8%e5%a6%82%e4%bd%95%e6%9b%b4%e6%96%b0%e7%89%88%e6%9c%ac%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127303.html","title":{"rendered":"python \u5e94\u7528\u5982\u4f55\u66f4\u65b0\u7248\u672c\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094342\/86ba7880-cd9b-45f4-a578-c6bf8311fcbf.webp\" alt=\"python \u5e94\u7528\u5982\u4f55\u66f4\u65b0\u7248\u672c\u53f7\" \/><\/p>\n<p><p> <strong>Python\u5e94\u7528\u66f4\u65b0\u7248\u672c\u53f7\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u67e5\u770b\u5f53\u524d\u7248\u672c\u3001\u4fee\u6539\u7248\u672c\u6587\u4ef6\u3001\u66f4\u65b0\u4f9d\u8d56\u3001\u6d4b\u8bd5\u548c\u53d1\u5e03\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4fee\u6539\u7248\u672c\u6587\u4ef6<\/strong>\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u8fd9\u662f\u6b63\u5f0f\u7248\u672c\u63a7\u5236\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u51c6\u786e\u5730\u4fee\u6539\u7248\u672c\u6587\u4ef6\u786e\u4fdd\u4e86\u5e94\u7528\u7248\u672c\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8ffd\u6eaf\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u67e5\u770b\u5f53\u524d\u7248\u672c<\/h2>\n<\/p>\n<p><p>\u5728\u66f4\u65b0\u7248\u672c\u53f7\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u8ba4\u5f53\u524d\u7684\u7248\u672c\u3002\u901a\u5e38\uff0cPython\u9879\u76ee\u7684\u7248\u672c\u4fe1\u606f\u4fdd\u5b58\u5728<code>setup.py<\/code>\u6216<code>pyproject.toml<\/code>\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff0c\u5982\u679c\u4f7f\u7528\u7684\u662f<code>poetry<\/code>\uff0c\u53ef\u4ee5\u67e5\u770b<code>pyproject.toml<\/code>\u6587\u4ef6\u4e2d\u7684\u7248\u672c\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u67e5\u770b\u5f53\u524d\u7248\u672c\u53f7\u6709\u52a9\u4e8e\u786e\u4fdd\u4f60\u4e86\u89e3\u9879\u76ee\u7684\u73b0\u72b6\uff0c\u5e76\u907f\u514d\u5728\u66f4\u65b0\u7248\u672c\u53f7\u65f6\u51fa\u73b0\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4fee\u6539\u7248\u672c\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u7248\u672c\u53f7\u901a\u5e38\u9075\u5faa\u8bed\u4e49\u7248\u672c\u63a7\u5236\uff08Semantic Versioning\uff09\u7684\u683c\u5f0f\uff0c\u5373<code>MAJOR.MINOR.PATCH<\/code>\u3002\u5728\u8fdb\u884c\u7248\u672c\u53f7\u66f4\u65b0\u65f6\uff0c\u4f60\u9700\u8981\u6839\u636e\u9879\u76ee\u7684\u5b9e\u9645\u6539\u52a8\u6765\u51b3\u5b9a\u662f\u66f4\u65b0\u4e3b\u7248\u672c\u53f7\u3001\u6b21\u7248\u672c\u53f7\u8fd8\u662f\u8865\u4e01\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<p><h3>1. \u4fee\u6539 <code>setup.py<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u4f7f\u7528<code>setuptools<\/code>\u8fdb\u884c\u6253\u5305\u548c\u53d1\u5e03\uff0c\u4f60\u9700\u8981\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u8fdb\u884c\u7248\u672c\u53f7\u7684\u4fee\u6539\u3002\u627e\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">setup(<\/p>\n<p>    name=&#39;your_project_name&#39;,<\/p>\n<p>    version=&#39;1.0.0&#39;,<\/p>\n<p>    ...<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u7248\u672c\u53f7\u66f4\u65b0\u4e3a\u65b0\u7684\u7248\u672c\u53f7\uff0c\u4f8b\u5982<code>1.1.0<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">setup(<\/p>\n<p>    name=&#39;your_project_name&#39;,<\/p>\n<p>    version=&#39;1.1.0&#39;,<\/p>\n<p>    ...<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4fee\u6539 <code>pyproject.toml<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u9879\u76ee\u4f7f\u7528<code>poetry<\/code>\u8fdb\u884c\u6253\u5305\u548c\u53d1\u5e03\uff0c\u4f60\u9700\u8981\u5728<code>pyproject.toml<\/code>\u6587\u4ef6\u4e2d\u8fdb\u884c\u7248\u672c\u53f7\u7684\u4fee\u6539\u3002\u627e\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-toml\">[tool.poetry]<\/p>\n<p>name = &quot;your_project_name&quot;<\/p>\n<p>version = &quot;1.0.0&quot;<\/p>\n<p>...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u7248\u672c\u53f7\u66f4\u65b0\u4e3a\u65b0\u7684\u7248\u672c\u53f7\uff0c\u4f8b\u5982<code>1.1.0<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-toml\">[tool.poetry]<\/p>\n<p>name = &quot;your_project_name&quot;<\/p>\n<p>version = &quot;1.1.0&quot;<\/p>\n<p>...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u66f4\u65b0\u4f9d\u8d56<\/h2>\n<\/p>\n<p><p>\u7248\u672c\u53f7\u66f4\u65b0\u5b8c\u6210\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u66f4\u65b0\u9879\u76ee\u7684\u4f9d\u8d56\u9879\u3002\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u662f\u6700\u65b0\u7684\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u66f4\u65b0\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528 <code>pip<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<code>pip<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u66f4\u65b0\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528 <code>poetry<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<code>poetry<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u66f4\u65b0\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry update<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>\u5728\u66f4\u65b0\u7248\u672c\u53f7\u548c\u4f9d\u8d56\u9879\u540e\uff0c\u52a1\u5fc5\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\u3002\u786e\u4fdd\u6240\u6709\u529f\u80fd\u6b63\u5e38\uff0c\u4e14\u6ca1\u6709\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pytest<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<code>tox<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">tox<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u6ce8\u610f\u68c0\u67e5\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u5355\u5143\u6d4b\u8bd5\u901a\u8fc7\u3002<\/li>\n<li><strong>\u96c6\u6210\u6d4b\u8bd5<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u96c6\u6210\u6d4b\u8bd5\u901a\u8fc7\u3002<\/li>\n<li><strong>\u56de\u5f52\u6d4b\u8bd5<\/strong>\uff1a\u786e\u4fdd\u65b0\u7248\u672c\u6ca1\u6709\u5f15\u5165\u65e7\u7248\u672c\u4e2d\u6ca1\u6709\u7684\u9519\u8bef\u3002<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u53d1\u5e03<\/h2>\n<\/p>\n<p><p>\u6d4b\u8bd5\u901a\u8fc7\u540e\uff0c\u6700\u540e\u4e00\u6b65\u662f\u53d1\u5e03\u65b0\u7684\u7248\u672c\u3002\u53d1\u5e03\u8fc7\u7a0b\u5305\u62ec\u5c06\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u63d0\u4ea4\u5230\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\uff0c\u5e76\u5c06\u65b0\u7248\u672c\u53d1\u5e03\u5230\u5305\u7ba1\u7406\u5e73\u53f0\uff08\u5982PyPI\uff09\u3002<\/p>\n<\/p>\n<p><h3>1. \u63d0\u4ea4\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u63d0\u4ea4\u5230\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">git add .<\/p>\n<p>git commit -m &quot;Update version to 1.1.0&quot;<\/p>\n<p>git push<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u53d1\u5e03\u5230PyPI<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<code>setuptools<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5c06\u65b0\u7248\u672c\u53d1\u5e03\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist bdist_wheel<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f<code>poetry<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5c06\u65b0\u7248\u672c\u53d1\u5e03\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry publish --build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53d1\u5e03\u5b8c\u6210\u540e\uff0c\u52a1\u5fc5\u5728\u9879\u76ee\u7684\u6587\u6863\u4e2d\u66f4\u65b0\u7248\u672c\u4fe1\u606f\uff0c\u5e76\u901a\u77e5\u76f8\u5173\u56e2\u961f\u6210\u5458\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u66f4\u65b0Python\u5e94\u7528\u7684\u7248\u672c\u53f7\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u5b83\u786e\u4fdd\u4e86\u9879\u76ee\u7684\u7248\u672c\u63a7\u5236\u548c\u53d1\u5e03\u8fc7\u7a0b\u7684\u89c4\u8303\u6027\u3002\u901a\u8fc7\u67e5\u770b\u5f53\u524d\u7248\u672c\u3001\u4fee\u6539\u7248\u672c\u6587\u4ef6\u3001\u66f4\u65b0\u4f9d\u8d56\u3001\u6d4b\u8bd5\u548c\u53d1\u5e03\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u9879\u76ee\u7684\u7248\u672c\u66f4\u65b0\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u9879\u76ee\u7684\u7248\u672c\u7ba1\u7406\u4e2d\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u5b9a\u6211\u7684Python\u5e94\u7528\u7684\u5f53\u524d\u7248\u672c\u53f7\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u5e94\u7528\u7684<code>__init__.py<\/code>\u6587\u4ef6\u6216<code>setup.py<\/code>\u6587\u4ef6\u6765\u627e\u5230\u5f53\u524d\u7248\u672c\u53f7\u3002\u901a\u5e38\uff0c\u7248\u672c\u4fe1\u606f\u4f1a\u4ee5<code>__version__<\/code>\u7684\u5f62\u5f0f\u5728\u4ee3\u7801\u4e2d\u5b9a\u4e49\uff0c\u6216\u8005\u5728<code>setup.py<\/code>\u4e2d\u6709\u7c7b\u4f3c<code>version=&#39;x.y.z&#39;<\/code>\u7684\u884c\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u7684\u5e94\u7528\u5df2\u53d1\u5e03\u5230PyPI\uff0c\u53ef\u4ee5\u5728PyPI\u4e0a\u627e\u5230\u7248\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u66f4\u65b0\u7248\u672c\u53f7\u65f6\u6709\u54ea\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><br \/>\u66f4\u65b0\u7248\u672c\u53f7\u65f6\uff0c\u9075\u5faa\u8bed\u4e49\u7248\u672c\u63a7\u5236\uff08SemVer\uff09\u662f\u4e2a\u597d\u4e3b\u610f\u3002\u8bed\u4e49\u7248\u672c\u63a7\u5236\u901a\u5e38\u91c7\u7528<code>\u4e3b\u7248\u672c.\u6b21\u7248\u672c.\u4fee\u8ba2\u53f7<\/code>\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u589e\u52a0\u4e3b\u7248\u672c\u53f7\u610f\u5473\u7740\u6709\u91cd\u5927\u66f4\u6539\uff0c\u589e\u52a0\u6b21\u7248\u672c\u53f7\u8868\u793a\u6709\u65b0\u529f\u80fd\uff0c\u4f46\u5411\u540e\u517c\u5bb9\uff0c\u800c\u4fee\u8ba2\u53f7\u5219\u7528\u4e8e\u5c0f\u7684\u4fee\u590d\u6216\u6539\u8fdb\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u66f4\u65b0\u7248\u672c\u53f7\u65f6\u66f4\u65b0\u76f8\u5173\u6587\u6863\u548c\u53d8\u66f4\u65e5\u5fd7\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Git\u4e2d\u7ba1\u7406\u7248\u672c\u53f7\u7684\u66f4\u65b0\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Git\u8fdb\u884c\u7248\u672c\u63a7\u5236\u65f6\uff0c\u5efa\u8bae\u5728\u66f4\u65b0\u7248\u672c\u53f7\u540e\u7acb\u5373\u63d0\u4ea4\u66f4\u6539\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u7b7e\u529f\u80fd\u6765\u6807\u8bb0\u7279\u5b9a\u7248\u672c\uff0c\u4f7f\u7528<code>git tag v1.0.0<\/code>\u7684\u547d\u4ee4\u8fdb\u884c\u6807\u8bb0\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ffd\u8e2a\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u7684\u53d8\u5316\uff0c\u786e\u4fdd\u56e2\u961f\u6210\u5458\u80fd\u591f\u6e05\u6670\u5730\u7406\u89e3\u7248\u672c\u5386\u53f2\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u6bcf\u4e2a\u7248\u672c\u53d1\u5e03\u524d\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a>\u548c\u6d4b\u8bd5\uff0c\u4ee5\u907f\u514d\u5f15\u5165\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5e94\u7528\u66f4\u65b0\u7248\u672c\u53f7\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u67e5\u770b\u5f53\u524d\u7248\u672c\u3001\u4fee\u6539\u7248\u672c\u6587\u4ef6\u3001\u66f4\u65b0\u4f9d\u8d56\u3001\u6d4b\u8bd5\u548c\u53d1\u5e03\u3002 \u5176\u4e2d\uff0c\u4fee\u6539\u7248\u672c\u6587\u4ef6\u5c24 [&hellip;]","protected":false},"author":3,"featured_media":1127310,"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\/1127303"}],"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=1127303"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127303\/revisions"}],"predecessor-version":[{"id":1127311,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127303\/revisions\/1127311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127310"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}