{"id":1015821,"date":"2024-12-27T12:11:19","date_gmt":"2024-12-27T04:11:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1015821.html"},"modified":"2024-12-27T12:11:22","modified_gmt":"2024-12-27T04:11:22","slug":"python%e7%88%ac%e8%99%ab%e6%a8%a1%e5%9d%97%e5%a6%82%e4%bd%95%e6%89%93%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1015821.html","title":{"rendered":"python\u722c\u866b\u6a21\u5757\u5982\u4f55\u6253\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095547\/de3a6363-43c2-4d54-8652-409f265a8fd0.webp\" alt=\"python\u722c\u866b\u6a21\u5757\u5982\u4f55\u6253\u5305\" \/><\/p>\n<p><p> <strong>Python\u722c\u866b\u6a21\u5757\u7684\u6253\u5305\u6d89\u53ca\u5230\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u91cd\u7528\u548c\u5206\u53d1\u7684\u7a0b\u5e8f\u5305\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u9879\u76ee\u7ed3\u6784\u3001\u521b\u5efasetup.py\u6587\u4ef6\u3001\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u3001\u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u6253\u5305\u3002<\/strong> \u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c<strong>\u521b\u5efasetup.py\u6587\u4ef6<\/strong>\u662f\u4e00\u4e2a\u5173\u952e\u73af\u8282\uff0c\u8fd9\u662f\u56e0\u4e3asetup.py\u6587\u4ef6\u662fPython\u6253\u5305\u5de5\u5177setuptools\u7528\u6765\u7ba1\u7406\u9879\u76ee\u7684\u6838\u5fc3\u6587\u4ef6\uff0c\u6b63\u786e\u914d\u7f6e\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684\u722c\u866b\u6a21\u5757\u80fd\u591f\u88ab\u6b63\u786e\u5b89\u88c5\u548c\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p><strong>\u521b\u5efasetup.py\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0csetup.py\u6587\u4ef6\u662f\u7528\u6765\u7ba1\u7406\u9879\u76ee\u7684\u6838\u5fc3\u6587\u4ef6\u3002\u5b83\u5b9a\u4e49\u4e86\u9879\u76ee\u7684\u540d\u79f0\u3001\u7248\u672c\u3001\u4f5c\u8005\u3001\u63cf\u8ff0\u3001\u4f9d\u8d56\u5173\u7cfb\u7b49\u4fe1\u606f\uff0c\u662f\u6253\u5305\u548c\u53d1\u5e03Python\u6a21\u5757\u7684\u57fa\u7840\u3002\u4ee5\u4e0b\u662f\u521b\u5efasetup.py\u6587\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165setuptools<\/strong>\uff1a\u5728setup.py\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165setuptools\u6a21\u5757\uff0c\u8fd9\u662fPython\u4e2d\u7528\u4e8e\u521b\u5efa\u548c\u5206\u53d1\u5305\u7684\u6807\u51c6\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49setup\u51fd\u6570<\/strong>\uff1asetup\u51fd\u6570\u662fsetuptools\u63d0\u4f9b\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u7528\u4e8e\u5b9a\u4e49\u5305\u7684\u5143\u6570\u636e\u548c\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">setup(<\/p>\n<p>    name=&#39;your_package_name&#39;,  # \u5305\u7684\u540d\u79f0<\/p>\n<p>    version=&#39;0.1.0&#39;,  # \u5305\u7684\u7248\u672c<\/p>\n<p>    author=&#39;Your Name&#39;,  # \u4f5c\u8005\u540d\u79f0<\/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;,  # \u4f5c\u8005\u7684\u90ae\u4ef6\u5730\u5740<\/p>\n<p>    description=&#39;A brief description of your package&#39;,  # \u5305\u7684\u7b80\u5355\u63cf\u8ff0<\/p>\n<p>    packages=find_packages(),  # \u81ea\u52a8\u67e5\u627e\u5305<\/p>\n<p>    install_requires=[  # \u5b9a\u4e49\u5305\u7684\u4f9d\u8d56\u9879<\/p>\n<p>        &#39;requests&gt;=2.24.0&#39;,  # \u4f8b\u5982requests\u5e93<\/p>\n<p>        &#39;beautifulsoup4&gt;=4.9.0&#39;,<\/p>\n<p>    ],<\/p>\n<p>    classifiers=[  # \u5206\u7c7b\u5668<\/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>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u4f9d\u8d56\u5173\u7cfb<\/strong>\uff1a\u5728install_requires\u4e2d\u5217\u51fa\u4f60\u7684\u6a21\u5757\u6240\u9700\u7684\u4f9d\u8d56\u5e93\u53ca\u5176\u7248\u672c\u53f7\u3002\u786e\u4fdd\u5217\u51fa\u7684\u7248\u672c\u53f7\u662f\u7ecf\u8fc7\u6d4b\u8bd5\u5e76\u4e14\u517c\u5bb9\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u5176\u4ed6\u4fe1\u606f<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u5176\u4ed6\u4fe1\u606f\uff0c\u6bd4\u5982\u957f\u63cf\u8ff0\u3001\u9879\u76ee\u7684URL\u3001\u8bb8\u53ef\u8bc1\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">long_description=open(&#39;README.md&#39;).read(),<\/p>\n<p>long_description_content_type=&#39;text\/markdown&#39;,<\/p>\n<p>url=&#39;https:\/\/github.com\/your_username\/your_package_name&#39;,<\/p>\n<p>license=&#39;MIT&#39;,<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u5904\u7406\u9879\u76ee\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efasetup.py\u6587\u4ef6\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u6709\u4e00\u4e2a\u6e05\u6670\u7684\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63a8\u8350\u7684Python\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>your_project\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 your_package_name\/         # \u5305\u7684\u76ee\u5f55<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py            # \u521d\u59cb\u5316\u6587\u4ef6<\/p>\n<p>\u2502   \u251c\u2500\u2500 module1.py             # \u6a21\u57571<\/p>\n<p>\u2502   \u2514\u2500\u2500 module2.py             # \u6a21\u57572<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 tests\/                     # \u6d4b\u8bd5\u76ee\u5f55<\/p>\n<p>\u2502   \u251c\u2500\u2500 test_module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 test_module2.py<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 README.md                  # \u9879\u76ee\u8bf4\u660e\u6587\u4ef6<\/p>\n<p>\u251c\u2500\u2500 setup.py                   # setup\u6587\u4ef6<\/p>\n<p>\u2514\u2500\u2500 requirements.txt           # \u4f9d\u8d56\u6587\u4ef6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u6253\u5305<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u5e2e\u52a9\u6253\u5305\u548c\u5206\u53d1\u4f60\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528setuptools<\/strong>\uff1asetuptools\u662fPython\u7684\u6807\u51c6\u6253\u5305\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u53d1\u884c\u7248\u3002<\/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\u521b\u5efa\u4e00\u4e2a\u6e90\u5206\u53d1\u5305\u548c\u4e00\u4e2aWheel\u5206\u53d1\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0a\u4f20\u5230PyPI<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528twine\u5de5\u5177\u5c06\u4f60\u7684\u5305\u4e0a\u4f20\u5230Python Package Index (PyPI)\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u53ef\u4ee5\u5b89\u88c5\u548c\u4f7f\u7528\u4f60\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u5904\u7406\u4f9d\u8d56\u5173\u7cfb<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6253\u5305\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u6240\u6709\u7684\u4f9d\u8d56\u5173\u7cfb\u90fd\u5728\u4f60\u7684<code>install_requires<\/code>\u4e2d\u6b63\u786e\u5217\u51fa\u3002\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u6765\u7ba1\u7406\u5f00\u53d1\u65f6\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5e76\u5728setup.py\u4e2d\u5f15\u7528\u8fd9\u4e9b\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<p><p><strong>\u6d4b\u8bd5\u4f60\u7684\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u53d1\u5e03\u4e4b\u524d\uff0c\u52a1\u5fc5\u6d4b\u8bd5\u4f60\u7684\u5305\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u786e\u4fdd\u4e00\u5207\u6b63\u5e38\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u672c\u5730\u5b89\u88c5<\/strong>\uff1a\u5728\u865a\u62df\u73af\u5883\u4e2d\u672c\u5730\u5b89\u88c5\u4f60\u7684\u5305\uff0c\u786e\u4fdd\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong>\uff1a\u4f7f\u7528pytest\u6216unittest\u7b49\u6d4b\u8bd5\u6846\u67b6\u6765\u8fd0\u884c\u4f60\u7684\u6d4b\u8bd5\u5957\u4ef6\uff0c\u786e\u4fdd\u6240\u6709\u6d4b\u8bd5\u90fd\u901a\u8fc7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u517c\u5bb9\u6027<\/strong>\uff1a\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u6d4b\u8bd5\u4f60\u7684\u5305\uff0c\u4ee5\u786e\u4fdd\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u6253\u5305\u4e00\u4e2aPython\u722c\u866b\u6a21\u5757\u5e76\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u9700\u8981\u4ed4\u7ec6\u7684\u8ba1\u5212\u548c\u6267\u884c\u3002\u901a\u8fc7\u5408\u7406\u5730\u7ec4\u7ec7\u9879\u76ee\u7ed3\u6784\u3001\u6b63\u786e\u914d\u7f6esetup.py\u6587\u4ef6\u3001\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u5e76\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9ad8\u8d28\u91cf\u7684Python\u6a21\u5757\uff0c\u65b9\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u5b89\u88c5\u548c\u4f7f\u7528\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4f60\u7684\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u8fd8\u4e3a\u5f00\u6e90\u793e\u533a\u505a\u51fa\u4e86\u8d21\u732e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u722c\u866b\u6a21\u5757\u6253\u5305\u4e3a\u53ef\u5206\u53d1\u7684\u5305\uff1f<\/strong><br \/>\u8981\u5c06Python\u722c\u866b\u6a21\u5757\u6253\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u5de5\u5177\u3002\u9996\u5148\uff0c\u5728\u6a21\u5757\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5b9a\u4e49\u6a21\u5757\u7684\u540d\u79f0\u3001\u7248\u672c\u3001\u63cf\u8ff0\u548c\u4f9d\u8d56\u9879\u7b49\u4fe1\u606f\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u547d\u4ee4<code>python setup.py sdist<\/code>\u6765\u751f\u6210\u6e90\u4ee3\u7801\u5206\u53d1\u5305\uff0c\u6216\u4f7f\u7528<code>python setup.py bdist_wheel<\/code>\u751f\u6210wheel\u683c\u5f0f\u7684\u5305\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u5c06\u751f\u6210\u7684\u5305\u4e0a\u4f20\u5230PyPI\uff0c\u4f9b\u5176\u4ed6\u7528\u6237\u4e0b\u8f7d\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u6253\u5305Python\u722c\u866b\u6a21\u5757\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4f9d\u8d56\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u6253\u5305Python\u722c\u866b\u6a21\u5757\u65f6\uff0c\u786e\u4fdd\u5728<code>setup.py<\/code>\u4e2d\u7684<code>install_requires<\/code>\u90e8\u5206\u5217\u51fa\u6240\u6709\u4f9d\u8d56\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>requests<\/code>\u3001<code>beautifulsoup4<\/code>\u7b49\u3002\u540c\u65f6\uff0c\u68c0\u67e5\u8fd9\u4e9b\u5e93\u7684\u7248\u672c\u517c\u5bb9\u6027\uff0c\u4ee5\u907f\u514d\u7528\u6237\u5728\u5b89\u88c5\u65f6\u9047\u5230\u4f9d\u8d56\u51b2\u7a81\u3002\u4f7f\u7528<code>pip freeze<\/code>\u547d\u4ee4\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u4f9d\u8d56\u9879\u53ca\u5176\u7248\u672c\u53f7\uff0c\u65b9\u4fbf\u8fdb\u884c\u8bb0\u5f55\u548c\u6574\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u6d4b\u8bd5\u6253\u5305\u540e\u7684\u722c\u866b\u6a21\u5757\uff1f<\/strong><br \/>\u5728\u6253\u5305\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u6765\u6d4b\u8bd5\u4f60\u7684\u722c\u866b\u6a21\u5757\u3002\u4f7f\u7528<code>python -m venv myenv<\/code>\u547d\u4ee4\u521b\u5efa\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u7136\u540e\u6fc0\u6d3b\u5b83\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>pip install &lt;\u4f60\u7684\u5305\u540d&gt;<\/code>\u547d\u4ee4\u5b89\u88c5\u521a\u6253\u5305\u7684\u6a21\u5757\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002\u540c\u65f6\uff0c\u8fd0\u884c\u4e00\u4e9b\u57fa\u672c\u7684\u722c\u866b\u529f\u80fd\uff0c\u786e\u4fdd\u6ca1\u6709\u9057\u6f0f\u4f9d\u8d56\u6216\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u6a21\u5757\u7684\u6253\u5305\u6d89\u53ca\u5230\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u91cd\u7528\u548c\u5206\u53d1\u7684\u7a0b\u5e8f\u5305\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u9879\u76ee\u7ed3\u6784\u3001\u521b\u5efasetup.p [&hellip;]","protected":false},"author":3,"featured_media":1015828,"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\/1015821"}],"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=1015821"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015821\/revisions"}],"predecessor-version":[{"id":1015830,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015821\/revisions\/1015830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1015828"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1015821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1015821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1015821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}