{"id":299353,"date":"2024-05-20T17:15:31","date_gmt":"2024-05-20T09:15:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299353.html"},"modified":"2024-05-20T17:15:41","modified_gmt":"2024-05-20T09:15:41","slug":"python-packaging-%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299353.html","title":{"rendered":"python packaging \u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27151501\/fcabee33-87b0-438b-a78b-3373ddcd130f.webp\" alt=\"python packaging \u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>Python\u8f6f\u4ef6\u5305\u6700\u4f73\u5b9e\u8df5\u5305\u62ec<strong>\u4f7f\u7528<code>setuptools<\/code>\u6216<code>poetry<\/code>\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/23316.html\" target=\"_blank\">\u9879\u76ee\u7ba1\u7406<\/a>\u3001\u7f16\u5199\u9ad8\u8d28\u91cf\u7684<code>README<\/code>\u6587\u4ef6\u3001\u7ef4\u62a4\u9700\u6c42\u6587\u4ef6\uff08<code>requirements.txt<\/code>\uff09\u3001\u9002\u5f53\u7684\u7248\u672c\u63a7\u5236\u3001\u5355\u5143\u6d4b\u8bd5\u548c\u6301\u7eed\u96c6\u6210<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>setuptools<\/code>\u6216<code>poetry<\/code>\u8fdb\u884c\u9879\u76ee\u7ba1\u7406\u662f\u6838\u5fc3\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u6807\u51c6\u5316\u6253\u5305\u548c\u5206\u53d1Python\u9879\u76ee\u7684\u5de5\u5177\u3002<code>setuptools<\/code>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6253\u5305\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9a\u4e49\u3001\u5206\u53d1\u548c\u5b89\u88c5\u9879\u76ee\u3002\u800c<code>poetry<\/code>\u662f\u4e00\u79cd\u65b0\u5174\u7684\u4f9d\u8d56\u7ba1\u7406\u548c\u6253\u5305\u5de5\u5177\uff0c\u4ee5\u5176\u76f4\u89c2\u6613\u7528\u548c\u73b0\u4ee3\u5316\u7684\u7279\u6027\u9010\u6e10\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9879\u76ee\u7ed3\u6784\u548c\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6784\u5efa\u4e00\u4e2a\u8f6f\u4ef6\u5305\u6d89\u53ca\u5230\u9075\u5faa\u4e00\u5b9a\u7684\u9879\u76ee\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1. \u6807\u51c6\u76ee\u5f55\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u4e00\u4e2a\u5178\u578b\u7684Python\u9879\u76ee\u76ee\u5f55\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<\/p>\n<p><pre><code>package_name\/<\/p>\n<p>    src\/<\/p>\n<p>        package_name\/<\/p>\n<p>            __init__.py<\/p>\n<p>            module1.py<\/p>\n<p>            module2.py<\/p>\n<p>    tests\/<\/p>\n<p>        __init__.py<\/p>\n<p>        test_module1.py<\/p>\n<p>        test_module2.py<\/p>\n<p>    README.md<\/p>\n<p>    setup.py<\/p>\n<p>    pyproject.toml<\/p>\n<p>    LICENSE<\/p>\n<p>    .gitignore<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>src\/<\/code> \u76ee\u5f55\u5305\u542b\u8f6f\u4ef6\u5305\u6e90\u7801\u3002<\/li>\n<li><code>tests\/<\/code> \u76ee\u5f55\u542b\u6709\u6240\u6709\u7684\u6d4b\u8bd5\u811a\u672c\u3002<\/li>\n<li><code>README.md<\/code> \u4ecb\u7ecd\u9879\u76ee\u548c\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/li>\n<li><code>setup.py<\/code> \u63cf\u8ff0\u4e86\u5305\u7684\u5143\u6570\u636e\uff0c\u5e76\u5305\u542b\u4e86\u6253\u5305\u548c\u5206\u53d1\u8be5\u8f6f\u4ef6\u5305\u6240\u9700\u7684\u6307\u4ee4\u3002<\/li>\n<li><code>pyproject.toml<\/code> \u662f\u4e00\u4e2a\u65b0\u7684\u6253\u5305\u6761\u4ef6\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u6765\u66ff\u4ee3<code>setup.py<\/code>\u3002<\/li>\n<li><code>LICENSE<\/code> \u6587\u4ef6\u5305\u542b\u9879\u76ee\u7684\u8bb8\u53ef\u8bc1\u4fe1\u606f\u3002<\/li>\n<li><code>.gitignore<\/code> \u6587\u4ef6\u6307\u5b9a\u4e86git\u5e94\u8be5\u5ffd\u7565\u7684\u975e\u7248\u672c\u63a7\u5236\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528<code>setuptools<\/code>\u6216<code>poetry<\/code><\/h4>\n<\/p>\n<p><p>\u8fd9\u4e24\u4e2a\u5de5\u5177\u89e3\u51b3\u4e86\u4f9d\u8d56\u7ba1\u7406\u3001\u9879\u76ee\u6784\u5efa\u548c\u5305\u53d1\u5e03\u7684\u95ee\u9898\uff0c\u8ba9\u6253\u5305\u7684\u8fc7\u7a0b\u53d8\u5f97\u7b80\u6d01\u548c\u53ef\u9760\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>setuptools<\/code><\/strong> \u662f\u6700\u4f20\u7edf\u4e14\u5e7f\u6cdb\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u5305\u542b<code>setup.py<\/code>\u811a\u672c\u6765\u5b9a\u4e49\u5305\u7684\u5143\u6570\u636e\u3001\u4f9d\u8d56\u548c\u6253\u5305\u4fe1\u606f\u3002<\/li>\n<li><strong><code>poetry<\/code><\/strong> \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u73b0\u4ee3\u5316\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u901a\u8fc7<code>pyproject.toml<\/code>\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u548c\u5143\u6570\u636e\uff0c\u63d0\u4f9b\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u7684\u5305\u7ba1\u7406\u4f53\u9a8c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u9ad8\u8d28\u91cf\u7684<code>README<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e00\u4e2a\u6709\u7528\u7684<code>README<\/code>\u6587\u4ef6\u5e94\u8be5\u8bf4\u660e\u9879\u76ee\u7684\u529f\u80fd\u3001\u5b89\u88c5\u65b9\u6cd5\u3001\u4f7f\u7528\u793a\u4f8b\u548c\u8d21\u732e\u6307\u5357\u3002<\/p>\n<\/p>\n<p><h4>1. \u6e05\u6670\u4ecb\u7ecd\u9879\u76ee\u7528\u9014<\/h4>\n<\/p>\n<p><p><strong>\u9879\u76ee\u7684\u63cf\u8ff0<\/strong> \u5e94\u8be5\u7b80\u5355\u660e\u4e86\uff0c\u8ba9\u4eba\u7acb\u5373\u7406\u89e3\u9879\u76ee\u7684\u7528\u9014\u548c\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u6b65\u9aa4\u548c\u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u63d0\u4f9b\u7cbe\u786e\u7684<strong>\u5b89\u88c5\u6307\u5357<\/strong> \u548c <strong>\u5b9e\u9645\u7684\u4f7f\u7528\u793a\u4f8b<\/strong>\uff0c\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u5165\u95e8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ef4\u62a4\u9700\u6c42\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>requirements.txt<\/code> \u6587\u4ef6\u5217\u51fa\u4e86\u9879\u76ee\u6240\u6709\u7684\u4f9d\u8d56\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7<code>pip freeze &gt; requirements.txt<\/code>\u751f\u6210\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u7f16\u8f91\u3002\u4f7f\u7528\u8be5\u6587\u4ef6\u53ef\u4ee5\u786e\u4fdd\u5176\u4ed6\u4eba\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u80fd\u6709\u76f8\u540c\u7684\u4f9d\u8d56\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u865a\u62df\u73af\u5883\u548c\u9700\u6c42\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u63a8\u8350\u4f7f\u7528<strong>\u865a\u62df\u73af\u5883<\/strong>\u6765\u7ba1\u7406\u4f9d\u8d56\uff0c\u7136\u540e\u7528<code>pip<\/code>\u547d\u4ee4\u521b\u5efa<code>requirements.txt<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u5206\u7c7b\u7ba1\u7406\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u5c06\u4f9d\u8d56\u5206\u4e3a\u751f\u4ea7\u73af\u5883\u548c\u5f00\u53d1\u73af\u5883\uff0c\u4f8b\u5982\u53ef\u4ee5\u521b\u5efa<code>requirements-dev.txt<\/code>\u6765\u5217\u51fa\u4ec5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9700\u8981\u7684\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7248\u672c\u63a7\u5236\u548c\u7248\u672c\u53f7\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u5bf9\u8f6f\u4ef6\u5305\u8fdb\u884c\u5408\u7406\u7684\u7248\u672c\u63a7\u5236\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5b83\u5305\u62ec\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u4f8b\u5982git\uff0c\u5e76\u9075\u5faa\u8bed\u4e49\u5316\u7248\u672c\u53f7\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5c06\u4ee3\u7801\u5e93\u6258\u7ba1\u5728\u5982<strong>GitHub<\/strong>\u3001<strong>GitLab<\/strong>\u4e0a\uff0c\u8fdb\u884c\u7248\u672c\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h4>2. \u91c7\u7528\u8bed\u4e49\u7248\u672c\u53f7<\/h4>\n<\/p>\n<p><p>\u8bed\u4e49\u7248\u672c\u53f7\u7684\u683c\u5f0f\u901a\u5e38\u4e3a<strong>\u4e3b\u7248\u672c\u53f7.\u6b21\u7248\u672c\u53f7.\u4fee\u8ba2\u53f7<\/strong>\uff0c\u8868\u660e\u4e86\u5305\u7684\u66f4\u65b0\u5c42\u6b21\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u4e0e\u6301\u7eed\u96c6\u6210<\/h3>\n<\/p>\n<p><p>\u6d4b\u8bd5\u662f\u4efb\u4f55\u8f6f\u4ef6\u9879\u76ee\u7684\u5173\u952e\u7ec4\u6210\u90e8\u5206\uff0c\u800c\u6301\u7eed\u96c6\u6210\u5219\u786e\u4fdd\u4e86\u4ee3\u7801\u6539\u53d8\u540e\u53ca\u65f6\u6267\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u7f16\u5199<strong>\u5355\u5143\u6d4b\u8bd5<\/strong>\u4ee5\u9a8c\u8bc1\u4ee3\u7801\u7684\u6bcf\u4e2a\u90e8\u5206\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6301\u7eed\u96c6\u6210\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u63a5\u5165<strong>\u6301\u7eed\u96c6\u6210\u670d\u52a1<\/strong> \u5982 Travis CI\u3001GitHub Actions\u7b49\uff0c\u81ea\u52a8\u5316\u6d4b\u8bd5\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u4ee5\u4e0aPython\u8f6f\u4ef6\u5305\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u521b\u5efa\u6613\u4e8e\u7ba1\u7406\u3001\u53ef\u7ef4\u62a4\u4e14\u5177\u6709\u9ad8\u5ea6\u53ef\u7528\u6027\u7684Python\u9879\u76ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u8fdb\u884cPython\u6253\u5305\uff1f<\/strong><\/p>\n<p>A: \u8fdb\u884cPython\u6253\u5305\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4f7f\u7528<code>setuptools<\/code>\u5e93\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>setup.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u6709\u5173\u4f60\u7684\u9879\u76ee\u7684\u4fe1\u606f\uff0c\u5982\u540d\u79f0\u3001\u7248\u672c\u3001\u4f5c\u8005\u7b49\u3002\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u63d0\u4f9b\u7684<code>setup()<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u4f60\u7684\u9879\u76ee\u7684\u6784\u5efa\u548c\u5b89\u88c5\u89c4\u5219\u3002\u6700\u540e\uff0c\u8fd0\u884c<code>python setup.py sdist<\/code>\u547d\u4ee4\u6765\u751f\u6210\u4e00\u4e2a\u6e90\u4ee3\u7801\u5206\u53d1\u5305\uff0c\u6216\u8005\u4f7f\u7528<code>python setup.py bdist_wheel<\/code>\u547d\u4ee4\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u7684\u4e8c\u8fdb\u5236wheel\u5305\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5b89\u88c5Python\u5305\uff1f<\/strong><\/p>\n<p>A: \u5b89\u88c5Python\u5305\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u6700\u65b0\u7248\u672c\u7684<code>pip<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>pip install<\/code>\u547d\u4ee4\u52a0\u4e0a\u5305\u540d\u6765\u5b89\u88c5\u6240\u9700\u7684\u5305\u3002\u5982\u679c\u4f60\u60f3\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5305\uff0c\u53ef\u4ee5\u5728\u5305\u540d\u540e\u6dfb\u52a0\u7248\u672c\u53f7\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u4e00\u6b21\u6027\u5b89\u88c5\u591a\u4e2a\u5305\uff0c\u53ea\u9700\u8981\u5c06\u6bcf\u4e2a\u5305\u7684\u540d\u79f0\u53ca\u5176\u53ef\u9009\u7684\u7248\u672c\u53f7\u5199\u5728\u6587\u4ef6\u4e2d\u5e76\u4f7f\u7528<code>pip install -r requirements.txt<\/code>\u547d\u4ee4\u6267\u884c\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u53d1\u5e03\u6211\u7684Python\u5305\u5230PyPI\uff1f<\/strong><\/p>\n<p>A: \u53d1\u5e03Python\u5305\u5230PyPI\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u6ce8\u518c\u4e86PyPI\u8d26\u53f7\u5e76\u5b89\u88c5\u4e86<code>twine<\/code>\u3002\u7136\u540e\uff0c\u8fd0\u884c<code>python setup.py sdist bdist_wheel<\/code>\u547d\u4ee4\u6765\u751f\u6210\u4f60\u7684\u5305\u7684\u5206\u53d1\u5305\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528<code>twine upload dist\/*<\/code>\u547d\u4ee4\u5c06\u5206\u53d1\u5305\u4e0a\u4f20\u5230PyPI\u3002\u5728\u4e0a\u4f20\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u9700\u8981\u63d0\u4f9b\u4f60\u7684PyPI\u8d26\u53f7\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u4e00\u65e6\u4e0a\u4f20\u5b8c\u6210\uff0c\u4f60\u7684\u5305\u5c06\u4f1a\u5728PyPI\u4e0a\u53ef\u7528\uff0c\u5176\u4ed6\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528<code>pip install<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u4f60\u7684\u5305\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f6f\u4ef6\u5305\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u4f7f\u7528setuptools\u6216poetry\u8fdb\u884c\u9879\u76ee\u7ba1\u7406\u3001\u7f16\u5199\u9ad8\u8d28\u91cf\u7684README\u6587\u4ef6 [&hellip;]","protected":false},"author":3,"featured_media":299369,"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\/299353"}],"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=299353"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299369"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}