{"id":1084268,"date":"2025-01-08T13:05:41","date_gmt":"2025-01-08T05:05:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084268.html"},"modified":"2025-01-08T13:05:44","modified_gmt":"2025-01-08T05:05:44","slug":"%e5%86%99python%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e5%8f%91%e5%b8%83%e5%ae%89%e8%a3%85%e5%8c%85-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084268.html","title":{"rendered":"\u5199python\u7a0b\u5e8f\u5982\u4f55\u53d1\u5e03\u5b89\u88c5\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194719\/9b037c8d-a9b4-464d-9704-de33b3b90238.webp\" alt=\"\u5199python\u7a0b\u5e8f\u5982\u4f55\u53d1\u5e03\u5b89\u88c5\u5305\" \/><\/p>\n<p><p> <strong>\u5199Python\u7a0b\u5e8f\u5e76\u53d1\u5e03\u5b89\u88c5\u5305\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Setuptools\u3001\u521b\u5efasetup.py\u6587\u4ef6\u3001\u751f\u6210\u5206\u53d1\u6587\u4ef6\u3001\u4e0a\u4f20\u5230PyPI\u3002<\/strong> \u4f7f\u7528Setuptools\u662f\u76ee\u524d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u4f7f\u521b\u5efa\u548c\u53d1\u5e03Python\u5305\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Setuptools<\/h3>\n<\/p>\n<p><p>Setuptools\u662f\u4e00\u4e2aPython\u5de5\u5177\uff0c\u7528\u4e8e\u4ecePython\u6e90\u4ee3\u7801\u521b\u5efa\u5b89\u88c5\u5305\u3002\u5b83\u6269\u5c55\u4e86Python\u6807\u51c6\u5e93\u4e2d\u7684distutils\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u8bb8\u591a\u529f\u80fd\u548c\u6539\u8fdb\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Setuptools<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Setuptools\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install setuptools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efasetup.py\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>setup.py\u6587\u4ef6\u662fPython\u5305\u7684\u6838\u5fc3\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u6709\u5173\u5305\u7684\u5143\u6570\u636e\u548c\u4f7f\u7528Setuptools\u8fdb\u884c\u6784\u5efa\u548c\u5206\u53d1\u7684\u6307\u4ee4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684setup.py\u6587\u4ef6\u793a\u4f8b\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;mypackage&quot;,  # \u5305\u540d\u79f0<\/p>\n<p>    version=&quot;0.1&quot;,  # \u7248\u672c\u53f7<\/p>\n<p>    packages=find_packages(),  # \u81ea\u52a8\u67e5\u627e\u5305<\/p>\n<p>    install_requires=[  # \u4f9d\u8d56\u5305\u5217\u8868<\/p>\n<p>        &#39;requests&#39;,<\/p>\n<p>        &#39;numpy&#39;,<\/p>\n<p>    ],<\/p>\n<p>    author=&quot;Your Name&quot;,  # \u4f5c\u8005\u540d\u79f0<\/p>\n<p>    author_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l=&quot;your.email@example.com&quot;,  # \u4f5c\u8005\u90ae\u7bb1<\/p>\n<p>    description=&quot;A simple example package&quot;,  # \u5305\u63cf\u8ff0<\/p>\n<p>    long_description=open(&#39;README.md&#39;).read(),  # \u957f\u63cf\u8ff0\uff0c\u901a\u5e38\u4eceREADME\u6587\u4ef6\u8bfb\u53d6<\/p>\n<p>    long_description_content_type=&#39;text\/markdown&#39;,  # \u957f\u63cf\u8ff0\u7684\u5185\u5bb9\u7c7b\u578b<\/p>\n<p>    url=&quot;https:\/\/github.com\/yourname\/mypackage&quot;,  # \u5305\u7684\u4e3b\u9875<\/p>\n<p>    classifiers=[  # \u5206\u7c7b\u5668\uff0c\u63cf\u8ff0\u5305\u7684\u7528\u9014\u548c\u53d7\u4f17<\/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;,  # Python\u7248\u672c\u8981\u6c42<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u5206\u53d1\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u597dsetup.py\u6587\u4ef6\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u751f\u6210\u5206\u53d1\u6587\u4ef6\u3002\u5206\u53d1\u6587\u4ef6\u6709\u4e24\u79cd\u4e3b\u8981\u7c7b\u578b\uff1a\u6e90\u7801\u5206\u53d1\u548c\u8f6e\u5b50\uff08Wheel\uff09\u5206\u53d1\u3002<\/p>\n<\/p>\n<p><h4>\u751f\u6210\u6e90\u7801\u5206\u53d1\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u4e00\u4e2a\u6e90\u7801\u5206\u53d1\u6587\u4ef6\uff08\u901a\u5e38\u662f.tar.gz\u6216.zip\u6587\u4ef6\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u751f\u6210\u8f6e\u5b50\u5206\u53d1\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8f6e\u5b50\u5206\u53d1\u6587\u4ef6\u662f\u4e00\u4e2a\u9884\u7f16\u8bd1\u7684\u5305\u683c\u5f0f\uff0c\u901a\u5e38\u4ee5.whl\u7ed3\u5c3e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u8f6e\u5b50\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0a\u4f20\u5230PyPI<\/h3>\n<\/p>\n<p><p>\u8981\u53d1\u5e03\u4f60\u7684\u5305\u5230Python Package Index (PyPI)\uff0c\u9700\u8981\u4f7f\u7528twine\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86twine\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e0a\u4f20\u5206\u53d1\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u4f60\u7684\u5206\u53d1\u6587\u4ef6\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\u53ef\u80fd\u9700\u8981\u63d0\u4f9b\u4f60\u7684PyPI\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u5b8c\u6210\u4e0a\u4f20\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u6574\u4e2a\u8fc7\u7a0b\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u9879\u76ee\u7684\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>\u251c\u2500\u2500 mypackage\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 module2.py<\/p>\n<p>\u251c\u2500\u2500 tests\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 test_module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 test_module2.py<\/p>\n<p>\u251c\u2500\u2500 setup.py<\/p>\n<p>\u2514\u2500\u2500 README.md<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0cmypackage\u662f\u4f60\u7684\u4e3b\u5305\u76ee\u5f55\uff0c\u5305\u542b\u4e24\u4e2a\u6a21\u5757module1.py\u548cmodule2.py\u3002tests\u76ee\u5f55\u5305\u542b\u6d4b\u8bd5\u7528\u4f8b\u3002setup.py\u6587\u4ef6\u4f4d\u4e8e\u9879\u76ee\u7684\u6839\u76ee\u5f55\uff0cREADME.md\u6587\u4ef6\u5305\u542b\u5305\u7684\u957f\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u53d1\u5e03Python\u5b89\u88c5\u5305\u7684\u8fc7\u7a0b\u5305\u62ec\u521b\u5efasetup.py\u6587\u4ef6\u3001\u751f\u6210\u5206\u53d1\u6587\u4ef6\u4ee5\u53ca\u5c06\u5b83\u4eec\u4e0a\u4f20\u5230PyPI\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u4f7f\u4f60\u7684\u5305\u5728Python\u793e\u533a\u4e2d\u5171\u4eab\u548c\u5206\u53d1\uff0c\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5\u548c\u4f7f\u7528\u4f60\u7684\u5305\u3002\u4f7f\u7528Setuptools\u548ctwine\u662f\u76ee\u524d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u5de5\u5177\u6765\u7ba1\u7406\u548c\u53d1\u5e03Python\u5305\u3002\u901a\u8fc7\u9075\u5faa\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u53d1\u5e03\u81ea\u5df1\u7684Python\u5305\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u6253\u5305\u6211\u7684Python\u9879\u76ee\u4ee5\u4fbf\u53d1\u5e03\uff1f<\/strong><br \/>\u8981\u5c06Python\u9879\u76ee\u6253\u5305\u4e3a\u5b89\u88c5\u5305\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u4e86\u9879\u76ee\u7684\u5143\u6570\u636e\uff0c\u6bd4\u5982\u540d\u79f0\u3001\u7248\u672c\u3001\u4f5c\u8005\u4fe1\u606f\u53ca\u4f9d\u8d56\u5e93\u7b49\u3002\u4f7f\u7528<code>setuptools<\/code>\u5e93\u53ef\u4ee5\u7b80\u5316\u6253\u5305\u8fc7\u7a0b\u3002\u786e\u4fdd\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>python setup.py sdist bdist_wheel<\/code>\u6765\u751f\u6210\u6e90\u4ee3\u7801\u5206\u53d1\u548c\u8f6e\u5b50\u6587\u4ef6\u3002\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5728<code>dist<\/code>\u6587\u4ef6\u5939\u4e2d\u627e\u5230\u751f\u6210\u7684\u5b89\u88c5\u5305\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u6765\u53d1\u5e03Python\u5b89\u88c5\u5305\uff1f<\/strong><br \/>\u53d1\u5e03Python\u5b89\u88c5\u5305\u901a\u5e38\u9700\u8981\u51e0\u4e2a\u5de5\u5177\uff0c\u5305\u62ec<code>setuptools<\/code>\u548c<code>twine<\/code>\u3002<code>setuptools<\/code>\u7528\u4e8e\u521b\u5efa\u548c\u6253\u5305\u9879\u76ee\uff0c\u800c<code>twine<\/code>\u5219\u7528\u6765\u5c06\u5305\u4e0a\u4f20\u5230Python Package Index (PyPI)\u3002\u786e\u4fdd\u5728\u60a8\u7684\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install setuptools twine<\/code>\u6765\u5b8c\u6210\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u6211\u7684Python\u5305\u4e0a\u4f20\u5230PyPI\uff1f<\/strong><br \/>\u5728\u6253\u5305\u5b8c\u6210\u540e\uff0c\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u4e0a\u4f20\u60a8\u7684\u5305\u81f3PyPI\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5728PyPI\u4e0a\u6709\u4e00\u4e2a\u5e10\u6237\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u547d\u4ee4<code>twine upload dist\/*<\/code>\u5c06\u751f\u6210\u7684\u5305\u4e0a\u4f20\u3002\u7cfb\u7edf\u4f1a\u63d0\u793a\u60a8\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u4e0a\u4f20\u6210\u529f\u540e\uff0c\u5176\u4ed6\u7528\u6237\u5c31\u53ef\u4ee5\u901a\u8fc7<code>pip install &lt;\u5305\u540d&gt;<\/code>\u6765\u5b89\u88c5\u60a8\u7684\u5305\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684\u5b89\u88c5\u5305\u517c\u5bb9\u4e0d\u540c\u7684Python\u7248\u672c\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u60a8\u7684\u5b89\u88c5\u5305\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u4e2d\u6b63\u5e38\u5de5\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u6307\u5b9a\u517c\u5bb9\u7684Python\u7248\u672c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>python_requires=&#39;&gt;=3.6&#39;<\/code>\u6765\u9650\u5236\u5b89\u88c5\u5305\u4ec5\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u53ef\u7528\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u6d4b\u8bd5\u5de5\u5177\uff0c\u5982<code>tox<\/code>\uff0c\u5728\u591a\u79cd\u73af\u5883\u4e2d\u6d4b\u8bd5\u60a8\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5199Python\u7a0b\u5e8f\u5e76\u53d1\u5e03\u5b89\u88c5\u5305\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Setuptools\u3001\u521b\u5efasetup.py\u6587\u4ef6\u3001\u751f\u6210\u5206\u53d1\u6587\u4ef6\u3001\u4e0a\u4f20 [&hellip;]","protected":false},"author":3,"featured_media":1084270,"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\/1084268"}],"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=1084268"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084268\/revisions"}],"predecessor-version":[{"id":1084271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084268\/revisions\/1084271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084270"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}