{"id":1041436,"date":"2024-12-31T12:49:17","date_gmt":"2024-12-31T04:49:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041436.html"},"modified":"2024-12-31T12:49:19","modified_gmt":"2024-12-31T04:49:19","slug":"%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e5%8f%af%e5%ae%89%e8%a3%85%e5%8c%85%e7%9a%84python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041436.html","title":{"rendered":"\u5982\u4f55\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c896b316-4fce-4b72-b149-60ced6cd4022.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684python\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684Python<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684Python\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u51c6\u5907\u9879\u76ee\u7ed3\u6784\u3001\u7f16\u5199 <code>setup.py<\/code> \u6587\u4ef6\u3001\u521b\u5efa\u548c\u4e0a\u4f20\u5206\u53d1\u5305\u3001\u4f7f\u7528 <code>twine<\/code> \u4e0a\u4f20\u5230 PyPI\u3001\u6d4b\u8bd5\u5b89\u88c5\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\u7684\u4e00\u4e2a\uff1a<strong>\u51c6\u5907\u9879\u76ee\u7ed3\u6784\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u51c6\u5907\u9879\u76ee\u7ed3\u6784<\/strong>\uff1a\u5728\u5f00\u59cb\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u7ed3\u6784\u7b26\u5408\u4e00\u5b9a\u7684\u6807\u51c6\u3002\u4e00\u4e2a\u5178\u578b\u7684Python\u9879\u76ee\u7ed3\u6784\u5e94\u8be5\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u9879\u76ee\u6839\u76ee\u5f55<\/li>\n<li><code>src<\/code>\u6216\u9879\u76ee\u540d\u76ee\u5f55<\/li>\n<li><code>tests<\/code>\u76ee\u5f55<\/li>\n<li><code>README.md<\/code>\u3001<code>LICENSE<\/code>\u3001<code>.gitignore<\/code>\u7b49\u6587\u4ef6<\/li>\n<li><code>setup.py<\/code>\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4e00\u4e2a\u7b80\u5355\u7684\u9879\u76ee\u7ed3\u6784\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>my_project\/<\/p>\n<p>\u251c\u2500\u2500 src\/<\/p>\n<p>\u2502   \u2514\u2500\u2500 my_module\/<\/p>\n<p>\u2502       \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502       \u2514\u2500\u2500 my_module.py<\/p>\n<p>\u251c\u2500\u2500 tests\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 test_my_module.py<\/p>\n<p>\u251c\u2500\u2500 README.md<\/p>\n<p>\u251c\u2500\u2500 LICENSE<\/p>\n<p>\u251c\u2500\u2500 .gitignore<\/p>\n<p>\u2514\u2500\u2500 setup.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u6765\u5b58\u653e\u4f60\u7684\u9879\u76ee\u4ee3\u7801\u548c\u76f8\u5173\u6587\u4ef6\u3002\u63a8\u8350\u7684\u9879\u76ee\u7ed3\u6784\u6709\u52a9\u4e8e\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u5177\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u4e5f\u7b26\u5408\u793e\u533a\u7684\u6807\u51c6\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h4>1. \u9879\u76ee\u6839\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u9879\u76ee\u6839\u76ee\u5f55\u662f\u4f60\u6240\u6709\u4ee3\u7801\u548c\u6587\u4ef6\u7684\u9876\u5c42\u76ee\u5f55\u3002\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff0c\u5982\u6e90\u4ee3\u7801\u76ee\u5f55\u3001\u6d4b\u8bd5\u76ee\u5f55\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. <code>src<\/code> \u6216\u9879\u76ee\u540d\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u8fd9\u662f\u5b58\u653e\u4f60\u4e3b\u8981\u4ee3\u7801\u7684\u5730\u65b9\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u7528 <code>src<\/code> \u4f5c\u4e3a\u76ee\u5f55\u540d\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f60\u7684\u9879\u76ee\u540d\u4f5c\u4e3a\u76ee\u5f55\u540d\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u786e\u4fdd\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u5305\u542b\u4e00\u4e2a <code>__init__.py<\/code> \u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2aPython\u5305\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u9879\u76ee\u540d\u4e3a <code>my_module<\/code>\uff0c\u90a3\u4e48\u5728 <code>src<\/code> \u76ee\u5f55\u4e0b\u5e94\u8be5\u6709\u4e00\u4e2a <code>my_module<\/code> \u5b50\u76ee\u5f55\uff0c\u5e76\u4e14\u5728 <code>my_module<\/code> \u76ee\u5f55\u4e0b\u6709\u4e00\u4e2a <code>__init__.py<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3. <code>tests<\/code> \u76ee\u5f55<\/h4>\n<\/p>\n<p><p><code>tests<\/code> \u76ee\u5f55\u7528\u4e8e\u5b58\u653e\u4f60\u7684\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u3002\u6d4b\u8bd5\u4ee3\u7801\u5e94\u8be5\u653e\u5728\u4e0e\u6e90\u4ee3\u7801\u76f8\u5bf9\u5e94\u7684\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u4e8e\u6d4b\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>4. <code>README.md<\/code>\u3001<code>LICENSE<\/code>\u3001<code>.gitignore<\/code> \u7b49\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd9\u4e9b\u6587\u4ef6\u662f\u9879\u76ee\u7684\u6587\u6863\u548c\u914d\u7f6e\u6587\u4ef6\u3002<code>README.md<\/code> \u6587\u4ef6\u7528\u4e8e\u63cf\u8ff0\u9879\u76ee\u7684\u57fa\u672c\u4fe1\u606f\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c<code>LICENSE<\/code> \u6587\u4ef6\u7528\u4e8e\u58f0\u660e\u9879\u76ee\u7684\u7248\u6743\u548c\u8bb8\u53ef\uff0c<code>.gitignore<\/code> \u6587\u4ef6\u7528\u4e8e\u914d\u7f6e Git \u5ffd\u7565\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199 <code>setup.py<\/code> \u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>setup.py<\/code> \u6587\u4ef6\u662f\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684\u5173\u952e\u6587\u4ef6\u3002\u5b83\u5305\u542b\u4e86\u5305\u7684\u5143\u6570\u636e\u548c\u914d\u7f6e\u4fe1\u606f\uff0c\u5982\u5305\u540d\u3001\u7248\u672c\u3001\u4f5c\u8005\u3001\u4f9d\u8d56\u9879\u7b49\u3002<\/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;my_module&#39;,<\/p>\n<p>    version=&#39;0.1.0&#39;,<\/p>\n<p>    description=&#39;A simple example package&#39;,<\/p>\n<p>    author=&#39;Your Name&#39;,<\/p>\n<p>    author_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l=&#39;your.email@example.com&#39;,<\/p>\n<p>    url=&#39;https:\/\/github.com\/yourusername\/my_module&#39;,<\/p>\n<p>    packages=find_packages(where=&#39;src&#39;),<\/p>\n<p>    package_dir={&#39;&#39;: &#39;src&#39;},<\/p>\n<p>    classifiers=[<\/p>\n<p>        &#39;Programming Language :: Python :: 3&#39;,<\/p>\n<p>        &#39;License :: OSI Approved :: MIT License&#39;,<\/p>\n<p>        &#39;Operating System :: OS Independent&#39;,<\/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><h3>\u4e09\u3001\u521b\u5efa\u548c\u4e0a\u4f20\u5206\u53d1\u5305<\/h3>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u521b\u5efa\u5206\u53d1\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py sdist bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u5728 <code>dist<\/code> \u76ee\u5f55\u4e0b\u751f\u6210 <code>.tar.gz<\/code> \u548c <code>.whl<\/code> \u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u5c31\u662f\u4f60\u7684\u53ef\u5b89\u88c5\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>twine<\/code> \u4e0a\u4f20\u5230 PyPI<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>twine<\/code> \u5de5\u5177\u5c06\u751f\u6210\u7684\u5305\u4e0a\u4f20\u5230 PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install twine<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6d4b\u8bd5\u4f60\u7684\u5305\u662f\u5426\u53ef\u4ee5\u6210\u529f\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install my_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5c06\u4f60\u7684Python\u9879\u76ee\u6253\u5305\u5e76\u53d1\u5e03\u5230PyPI\uff0c\u4f7f\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u548c\u4f7f\u7528\u4f60\u7684\u5305\u3002\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684\u8fc7\u7a0b\u4e0d\u4ec5\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u5206\u53d1\u548c\u5171\u4eab\uff0c\u8fd8\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u53ef\u5b89\u88c5\u5305\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u53ef\u5b89\u88c5\u5305\u7684\u8fc7\u7a0b\u6d89\u53ca\u51e0\u4e2a\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u60a8\u7684Python\u9879\u76ee\u6709\u4e00\u4e2a\u6807\u51c6\u7684\u76ee\u5f55\u7ed3\u6784\uff0c\u5305\u62ec\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u5b9a\u4e49\u4e86\u60a8\u7684\u5305\u7684\u5143\u6570\u636e\uff0c\u4f8b\u5982\u540d\u79f0\u3001\u7248\u672c\u548c\u4f9d\u8d56\u9879\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u5e93\u6765\u5e2e\u52a9\u60a8\u7ba1\u7406\u8fd9\u4e9b\u7ec6\u8282\u3002\u7f16\u5199\u597d<code>setup.py<\/code>\u540e\uff0c\u8fd0\u884c<code>python setup.py sdist<\/code>\u547d\u4ee4\u5c06\u751f\u6210\u6e90\u4ee3\u7801\u5206\u53d1\u5305\uff0c\u63a5\u7740\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u5c06\u5176\u4e0a\u4f20\u5230PyPI\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b89\u88c5\u5305\u4e2d\u7ba1\u7406\u4f9d\u8d56\u9879\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u53ef\u5b89\u88c5\u5305\u65f6\uff0c\u7ba1\u7406\u4f9d\u8d56\u9879\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>install_requires<\/code>\u53c2\u6570\u5217\u51fa\u6240\u6709\u7684\u4f9d\u8d56\u5e93\u3002\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u5b89\u88c5\u60a8\u7684\u5305\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u9879\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\uff0c\u4ee5\u4fbf\u7528\u6237\u80fd\u591f\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5feb\u901f\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4e3aPython\u5305\u6dfb\u52a0\u81ea\u5b9a\u4e49\u547d\u4ee4\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728<code>setup.py<\/code>\u4e2d\u5b9a\u4e49\u81ea\u5b9a\u4e49\u547d\u4ee4\uff0c\u4ee5\u6269\u5c55\u5b89\u88c5\u8fc7\u7a0b\u3002\u901a\u8fc7\u7ee7\u627f<code>setuptools.Command<\/code>\u7c7b\u5e76\u5b9e\u73b0\u5fc5\u8981\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u547d\u4ee4\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u60a8\u5728\u5305\u5b89\u88c5\u65f6\u6267\u884c\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u751f\u6210\u4ee3\u7801\u3001\u6570\u636e\u8fc1\u79fb\u7b49\u3002\u786e\u4fdd\u5728<code>setup.py<\/code>\u4e2d\u5c06\u81ea\u5b9a\u4e49\u547d\u4ee4\u6ce8\u518c\u5230<code>cmdclass<\/code>\u53c2\u6570\u4e2d\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684Python \u5236\u4f5c\u53ef\u5b89\u88c5\u5305\u7684Python\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u51c6\u5907\u9879\u76ee\u7ed3\u6784\u3001\u7f16\u5199 setup.p [&hellip;]","protected":false},"author":3,"featured_media":1041445,"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\/1041436"}],"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=1041436"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041436\/revisions"}],"predecessor-version":[{"id":1041448,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041436\/revisions\/1041448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041445"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}