{"id":1104111,"date":"2025-01-08T16:17:21","date_gmt":"2025-01-08T08:17:21","guid":{"rendered":""},"modified":"2025-01-08T16:17:29","modified_gmt":"2025-01-08T08:17:29","slug":"python-%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e7%ac%ac%e4%b8%89%e6%96%b9%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104111.html","title":{"rendered":"python \u5982\u4f55\u5efa\u7acb\u7b2c\u4e09\u65b9\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065632\/1d90de63-06aa-4435-b4fa-c5b6b2e7c407.webp\" alt=\"python \u5982\u4f55\u5efa\u7acb\u7b2c\u4e09\u65b9\u5e93\" \/><\/p>\n<p><p> <strong>Python \u5efa\u7acb\u7b2c\u4e09\u65b9\u5e93\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u521b\u5efa\u6a21\u5757\u548c\u5305\u3001\u7f16\u5199 setup.py \u6587\u4ef6\u3001\u6253\u5305\u548c\u53d1\u5e03\u5230 PyPI\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u7f16\u5199 setup.py \u6587\u4ef6<\/strong> \u662f\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\uff0c\u56e0\u4e3a setup.py \u6587\u4ef6\u5b9a\u4e49\u4e86\u5e93\u7684\u5143\u6570\u636e\u4fe1\u606f\uff0c\u5982\u540d\u79f0\u3001\u7248\u672c\u3001\u4f9d\u8d56\u9879\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4e00\u6b65\u6b65\u5efa\u7acb\u4e00\u4e2a Python \u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u6a21\u5757\u548c\u5305<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u9879\u76ee\u7ed3\u6784\n<ul>\n<li>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u6765\u5b58\u653e\u6211\u4eec\u7684\u9879\u76ee\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>my_library<\/code> \u7684\u6587\u4ef6\u5939\u3002<\/li>\n<li>\u5728 <code>my_library<\/code> \u6587\u4ef6\u5939\u5185\uff0c\u521b\u5efa\u4e00\u4e2a\u540c\u540d\u7684\u5b50\u6587\u4ef6\u5939 <code>my_library<\/code>\uff0c\u8fd9\u4e2a\u5b50\u6587\u4ef6\u5939\u5c06\u5b58\u653e\u6211\u4eec\u7684\u5e93\u4ee3\u7801\u3002<\/li>\n<li>\u5728 <code>my_library<\/code> \u6587\u4ef6\u5939\u5185\u521b\u5efa\u4e00\u4e2a <code>__init__.py<\/code> \u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff0c\u7528\u4e8e\u5c06\u8be5\u6587\u4ef6\u5939\u6807\u8bc6\u4e3a\u4e00\u4e2a Python \u5305\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code>my_library\/<\/p>\n<p>    my_library\/<\/p>\n<p>        __init__.py<\/p>\n<p>    setup.py<\/p>\n<p>    README.md<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7f16\u5199\u6a21\u5757\u4ee3\u7801\n<ul>\n<li>\u5728 <code>my_library<\/code> \u6587\u4ef6\u5939\u5185\u7f16\u5199\u4f60\u7684\u6a21\u5757\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>utils.py<\/code> \u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4e00\u4e9b\u5b9e\u7528\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># my_library\/utils.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7f16\u5199 setup.py \u6587\u4ef6<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa setup.py \u6587\u4ef6\n<ul>\n<li>\u5728 <code>my_library<\/code> \u6587\u4ef6\u5939\u5185\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>setup.py<\/code> \u7684\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u7528\u4e8e\u5b9a\u4e49\u5e93\u7684\u5143\u6570\u636e\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># my_library\/setup.py<\/p>\n<p>from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;my_library&#39;,<\/p>\n<p>    version=&#39;0.1.0&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # \u5728\u8fd9\u91cc\u5217\u51fa\u4f60\u7684\u5e93\u4f9d\u8d56\u7684\u5176\u4ed6\u5e93<\/p>\n<p>    ],<\/p>\n<p>    author=&#39;\u4f60\u7684\u540d\u5b57&#39;,<\/p>\n<p>    author_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l=&#39;\u4f60\u7684\u90ae\u7bb1&#39;,<\/p>\n<p>    description=&#39;\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b Python \u7b2c\u4e09\u65b9\u5e93&#39;,<\/p>\n<p>    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\/\u4f60\u7684\u7528\u6237\u540d\/my_library&#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>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5728 setup.py \u4e2d\u5b9a\u4e49\u5143\u6570\u636e\n<ul>\n<li><code>name<\/code>: \u5e93\u7684\u540d\u79f0\u3002<\/li>\n<li><code>version<\/code>: \u5e93\u7684\u7248\u672c\u53f7\u3002<\/li>\n<li><code>packages<\/code>: \u4f7f\u7528 <code>find_packages()<\/code> \u81ea\u52a8\u67e5\u627e\u5e93\u4e2d\u7684\u6240\u6709\u5305\u3002<\/li>\n<li><code>install_requires<\/code>: \u5217\u51fa\u5e93\u4f9d\u8d56\u7684\u5176\u4ed6\u5e93\u3002<\/li>\n<li><code>author<\/code> \u548c <code>author_email<\/code>: \u4f5c\u8005\u4fe1\u606f\u3002<\/li>\n<li><code>description<\/code>: \u7b80\u77ed\u7684\u5e93\u63cf\u8ff0\u3002<\/li>\n<li><code>long_description<\/code> \u548c <code>long_description_content_type<\/code>: \u4ece <code>README.md<\/code> \u6587\u4ef6\u4e2d\u8bfb\u53d6\u957f\u63cf\u8ff0\uff0c\u5e76\u6307\u5b9a\u5185\u5bb9\u7c7b\u578b\u4e3a Markdown\u3002<\/li>\n<li><code>url<\/code>: \u5e93\u7684\u4e3b\u9875 URL\uff0c\u4f8b\u5982 GitHub \u4ed3\u5e93\u5730\u5740\u3002<\/li>\n<li><code>classifiers<\/code>: \u5217\u51fa\u5e93\u7684\u5206\u7c7b\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u6253\u5305\u548c\u53d1\u5e03\u5230 PyPI<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u6253\u5305\u5de5\u5177\n<ul>\n<li>\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5 <code>setuptools<\/code> \u548c <code>wheel<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install setuptools wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u5206\u53d1\u5305\n<ul>\n<li>\u5728\u7ec8\u7aef\u4e2d\u5bfc\u822a\u5230 <code>my_library<\/code> \u6587\u4ef6\u5939\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u5206\u53d1\u5305\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">python setup.py sdist bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u8fd9\u5c06\u751f\u6210 <code>dist<\/code> \u6587\u4ef6\u5939\uff0c\u5176\u4e2d\u5305\u542b <code>.tar.gz<\/code> \u548c <code>.whl<\/code> \u6587\u4ef6\u3002<\/li>\n<\/ul>\n<ol start=\"3\">\n<li>\u5b89\u88c5 Twine\n<ul>\n<li>\u5728\u7ec8\u7aef\u4e2d\u5b89\u88c5 <code>twine<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u53d1\u5e03\u5230 PyPI\n<ul>\n<li>\u4f7f\u7528 <code>twine<\/code> \u5c06\u5e93\u53d1\u5e03\u5230 PyPI\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u7cfb\u7edf\u4f1a\u63d0\u793a\u4f60\u8f93\u5165 PyPI \u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u5982\u679c\u6ca1\u6709 PyPI \u5e10\u6237\uff0c\u53ef\u4ee5\u5148\u6ce8\u518c\u4e00\u4e2a\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u9a8c\u8bc1\u5e93\u7684\u5b89\u88c5\u548c\u4f7f\u7528<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5e93\n<ul>\n<li>\u5728\u53e6\u4e00\u4e2a\u9879\u76ee\u4e2d\uff0c\u9a8c\u8bc1\u5e93\u662f\u5426\u53ef\u4ee5\u6210\u529f\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install my_library<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5bfc\u5165\u5e93\u5e76\u4f7f\u7528\n<ul>\n<li>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e93\u5e76\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from my_library.utils import add, subtract<\/p>\n<p>print(add(2, 3))         # \u8f93\u51fa: 5<\/p>\n<p>print(subtract(5, 3))    # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86\u4e00\u4e2a Python \u7b2c\u4e09\u65b9\u5e93\uff0c\u5e76\u5c06\u5176\u53d1\u5e03\u5230 PyPI\u3002\u8fd9\u4e2a\u5e93\u73b0\u5728\u53ef\u4ee5\u88ab\u5168\u7403\u7684\u5f00\u53d1\u8005\u5b89\u88c5\u548c\u4f7f\u7528\u3002\u8bb0\u5f97\u5b9a\u671f\u66f4\u65b0\u548c\u7ef4\u62a4\u4f60\u7684\u5e93\uff0c\u4ee5\u786e\u4fdd\u5176\u529f\u80fd\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u521b\u5efa\u81ea\u5df1\u7684Python\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u81ea\u5df1\u7684Python\u7b2c\u4e09\u65b9\u5e93\uff0c\u9996\u5148\u9700\u8981\u786e\u5b9a\u5e93\u7684\u529f\u80fd\u548c\u76ee\u6807\u53d7\u4f17\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u9879\u76ee\u7ed3\u6784\uff0c\u901a\u5e38\u5305\u62ec<code>setup.py<\/code>\u6587\u4ef6\u7528\u4e8e\u914d\u7f6e\u548c\u5b89\u88c5\u5e93\uff0c\u4ee5\u53ca\u4e00\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u6587\u4ef6\u6765\u5b9e\u73b0\u5e93\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u7f16\u5199\u6587\u6863\u548c\u6d4b\u8bd5\u7528\u4f8b\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5e93\u7684\u53ef\u9760\u6027\u548c\u6613\u7528\u6027\u3002\u6700\u540e\uff0c\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6258\u7ba1\u5e73\u53f0\uff08\u5982PyPI\uff09\u6765\u53d1\u5e03\u60a8\u7684\u5e93\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5b89\u88c5\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728\u6784\u5efaPython\u5e93\u65f6\uff0c\u5982\u4f55\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u6784\u5efaPython\u5e93\u65f6\uff0c\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u975e\u5e38\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528<code>install_requires<\/code>\u53c2\u6570\u6765\u58f0\u660e\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u53ef\u4ee5\u65b9\u4fbf\u5730\u5217\u51fa\u6240\u6709\u7684\u4f9d\u8d56\u9879\u53ca\u5176\u7248\u672c\uff0c\u8fd9\u6837\u7528\u6237\u5728\u5b89\u88c5\u65f6\u53ef\u4ee5\u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u5fc5\u8981\u7684\u5e93\u3002\u4e3a\u4e86\u786e\u4fdd\u517c\u5bb9\u6027\uff0c\u5efa\u8bae\u4f7f\u7528<code>virtualenv<\/code>\u6216<code>pipenv<\/code>\u7b49\u5de5\u5177\u6765\u521b\u5efa\u72ec\u7acb\u7684\u5f00\u53d1\u73af\u5883\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6211\u7684Python\u5e93\u80fd\u88ab\u5e7f\u6cdb\u4f7f\u7528\u548c\u63a5\u53d7\uff1f<\/strong><br \/>\u786e\u4fddPython\u5e93\u88ab\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e00\u4e2a\u6709\u6548\u7b56\u7565\u662f\u63d0\u4f9b\u826f\u597d\u7684\u6587\u6863\u3002\u8fd9\u5305\u62ec\u8be6\u7ec6\u7684API\u8bf4\u660e\u3001\u793a\u4f8b\u4ee3\u7801\u548c\u4f7f\u7528\u6307\u5357\u3002\u6b64\u5916\uff0c\u79ef\u6781\u53c2\u4e0e\u5f00\u6e90\u793e\u533a\uff0c\u6536\u96c6\u7528\u6237\u53cd\u9988\u5e76\u5b9a\u671f\u66f4\u65b0\u5e93\uff0c\u4ee5\u4fee\u590dbug\u548c\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u5e93\u7684\u53ef\u63a5\u53d7\u6027\u3002\u901a\u8fc7\u5728\u793e\u4ea4\u5a92\u4f53\u3001\u6280\u672f\u535a\u5ba2\u6216\u5f00\u53d1\u8005\u8bba\u575b\u4e0a\u5ba3\u4f20\u60a8\u7684\u5e93\uff0c\u4e5f\u80fd\u5438\u5f15\u66f4\u591a\u7684\u7528\u6237\u548c\u8d21\u732e\u8005\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5efa\u7acb\u7b2c\u4e09\u65b9\u5e93\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u521b\u5efa\u6a21\u5757\u548c\u5305\u3001\u7f16\u5199 setup.py \u6587\u4ef6\u3001\u6253\u5305\u548c\u53d1\u5e03\u5230 PyPI\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1104121,"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\/1104111"}],"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=1104111"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104111\/revisions"}],"predecessor-version":[{"id":1104126,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104111\/revisions\/1104126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104121"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}