{"id":1091540,"date":"2025-01-08T14:12:39","date_gmt":"2025-01-08T06:12:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091540.html"},"modified":"2025-01-08T14:12:42","modified_gmt":"2025-01-08T06:12:42","slug":"python3%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e8%bd%af%e4%bb%b6%e5%8f%91%e5%b8%83-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091540.html","title":{"rendered":"python3\u5982\u4f55\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203701\/dabb964a-44bf-4752-a748-b611c81601b6.webp\" alt=\"python3\u5982\u4f55\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\u7684\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u6253\u5305\u5de5\u5177\u3001\u7f16\u5199setup.py\u6587\u4ef6\u3001\u751f\u6210\u5206\u53d1\u5305\u3001\u4e0a\u4f20\u5230PyPI\u3001\u7f16\u5199README\u6587\u4ef6\u3001\u7f16\u5199LICENSE\u6587\u4ef6\u3001\u6d4b\u8bd5\u5b89\u88c5\u5305\u3001\u53d1\u5e03\u5230\u5176\u4ed6\u5e73\u53f0\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u6253\u5305\u5de5\u5177\u662f\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u53ef\u4ee5\u9009\u62e9setuptools\uff0c\u8fd9\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6253\u5305\u5de5\u5177\u4e4b\u4e00\u3002\u4f7f\u7528setuptools\u53ef\u4ee5\u7b80\u5316\u8f6f\u4ef6\u5305\u7684\u521b\u5efa\u548c\u53d1\u5e03\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u6253\u5305\u5de5\u5177<\/p>\n<\/p>\n<ol>\n<li><strong>setuptools<\/strong>\uff1asetuptools\u662f\u4e00\u4e2a\u589e\u5f3a\u7684distutils\u5e93\uff0c\u7528\u4e8e\u6253\u5305Python\u9879\u76ee\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u5305\u62ec\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u3001\u751f\u6210\u5206\u53d1\u5305\u7b49\u3002\u5b89\u88c5setuptools\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\uff1a\n<pre><code class=\"language-bash\">pip install setuptools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li><strong>wheel<\/strong>\uff1awheel\u662f\u53e6\u4e00\u79cd\u6253\u5305\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u9879\u76ee\u6253\u5305\u6210\u4e00\u4e2a\u6807\u51c6\u7684\u683c\u5f0f\uff0c\u4fbf\u4e8e\u5206\u53d1\u548c\u5b89\u88c5\u3002\u5b89\u88c5wheel\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\uff1a\n<pre><code class=\"language-bash\">pip install wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u7f16\u5199setup.py\u6587\u4ef6<\/p>\n<\/p>\n<p><p>setup.py\u6587\u4ef6\u662fPython\u9879\u76ee\u7684\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e\u5b9a\u4e49\u9879\u76ee\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u5305\u62ec\u540d\u79f0\u3001\u7248\u672c\u3001\u4f5c\u8005\u3001\u4f9d\u8d56\u9879\u7b49\u3002\u4e00\u4e2a\u7b80\u5355\u7684setup.py\u6587\u4ef6\u793a\u4f8b\u5982\u4e0b\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=&#39;my_project&#39;,<\/p>\n<p>    version=&#39;0.1.0&#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>    description=&#39;A short description of your project&#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\/yourusername\/my_project&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        &#39;requests&gt;=2.20.0&#39;,<\/p>\n<p>    ],<\/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><p>\u4e09\u3001\u751f\u6210\u5206\u53d1\u5305<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5206\u53d1\u5305\u662f\u5c06Python\u9879\u76ee\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u5206\u53d1\u7684\u683c\u5f0f\uff0c\u4fbf\u4e8e\u4e0a\u4f20\u5230PyPI\u6216\u5176\u4ed6\u5e73\u53f0\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u5206\u53d1\u5305\uff1a<\/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\u751f\u6210\u4e24\u4e2a\u6587\u4ef6\uff1a\u4e00\u4e2a\u6e90\u4ee3\u7801\u5206\u53d1\u5305\uff08sdist\uff09\u548c\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5206\u53d1\u5305\uff08wheel\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4e0a\u4f20\u5230PyPI<\/p>\n<\/p>\n<p><p>\u5c06\u751f\u6210\u7684\u5206\u53d1\u5305\u4e0a\u4f20\u5230PyPI\uff0c\u53ef\u4ee5\u4f7f\u7528twine\u5de5\u5177\u3002\u9996\u5148\uff0c\u5b89\u88c5twine\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e0a\u4f20\u5206\u53d1\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5df2\u7ecf\u5728PyPI\u4e0a\u6ce8\u518c\u4e86\u4e00\u4e2a\u8d26\u53f7\uff0c\u5e76\u914d\u7f6e\u4e86~\/.pypirc\u6587\u4ef6\u4ee5\u4fbftwine\u80fd\u591f\u8fdb\u884c\u8ba4\u8bc1\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7f16\u5199README\u6587\u4ef6<\/p>\n<\/p>\n<p><p>README\u6587\u4ef6\u662f\u9879\u76ee\u7684\u4ecb\u7ecd\u6587\u6863\uff0c\u901a\u5e38\u4f7f\u7528Markdown\u683c\u5f0f\u7f16\u5199\u3002\u5b83\u5e94\u8be5\u5305\u62ec\u9879\u76ee\u7684\u57fa\u672c\u4fe1\u606f\u3001\u5b89\u88c5\u65b9\u6cd5\u3001\u4f7f\u7528\u793a\u4f8b\u7b49\u3002\u4e00\u4e2a\u7b80\u5355\u7684README.md\u6587\u4ef6\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-markdown\"># My Project<\/p>\n<p>A short description of my project.<\/p>\n<p>## Installation<\/p>\n<p>You can install the project using pip:<\/p>\n<p>```bash<\/p>\n<p>pip install my_project<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>Usage<\/h2>\n<\/p>\n<p><p>Here is a simple example of how to use the project:<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_project import my_function<\/p>\n<p>result = my_function()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code><\/p>\n<h2><strong>\u516d\u3001\u7f16\u5199LICENSE\u6587\u4ef6<\/strong><\/h2>\n<p>LICENSE\u6587\u4ef6\u662f\u9879\u76ee\u7684\u8bb8\u53ef\u8bc1\uff0c\u51b3\u5b9a\u4e86\u5176\u4ed6\u4eba\u5982\u4f55\u4f7f\u7528\u3001\u4fee\u6539\u548c\u5206\u53d1\u4f60\u7684\u9879\u76ee\u3002\u5e38\u89c1\u7684\u8bb8\u53ef\u8bc1\u5305\u62ecMIT\u3001Apache 2.0\u3001GPL\u7b49\u3002\u4e00\u4e2a\u7b80\u5355\u7684MIT LICENSE\u6587\u4ef6\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>```plaintext<\/p>\n<p>MIT License<\/p>\n<p>Permission is hereby granted, free of charge, to any person obtaining a copy<\/p>\n<p>of this software and associated documentation files (the &quot;Software&quot;), to deal<\/p>\n<p>in the Software without restriction, including without limitation the rights<\/p>\n<p>to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell<\/p>\n<p>copies of the Software, and to permit persons to whom the Software is<\/p>\n<p>furnished to do so, subject to the following conditions:<\/p>\n<p>The above copyright notice and this permission notice shall be included in all<\/p>\n<p>copies or substantial portions of the Software.<\/p>\n<p>THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<\/p>\n<p>IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<\/p>\n<p>FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE<\/p>\n<p>AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<\/p>\n<p>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<\/p>\n<p>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<\/p>\n<p>SOFTWARE.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u6d4b\u8bd5\u5b89\u88c5\u5305<\/p>\n<\/p>\n<p><p>\u5728\u53d1\u5e03\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u5206\u53d1\u5305\u53ef\u4ee5\u6b63\u786e\u5b89\u88c5\u548c\u4f7f\u7528\u3002\u53ef\u4ee5\u5728\u4e00\u4e2a\u865a\u62df\u73af\u5883\u4e2d\u6d4b\u8bd5\u5b89\u88c5\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv test_env<\/p>\n<p>source test_env\/bin\/activate<\/p>\n<p>pip install my_project-0.1.0-py3-none-any.whl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6d4b\u8bd5\u5b89\u88c5\u5305\u540e\uff0c\u786e\u4fdd\u6240\u6709\u529f\u80fd\u90fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u53d1\u5e03\u5230\u5176\u4ed6\u5e73\u53f0<\/p>\n<\/p>\n<p><p>\u9664\u4e86PyPI\uff0c\u4f60\u8fd8\u53ef\u4ee5\u9009\u62e9\u53d1\u5e03\u5230\u5176\u4ed6\u5e73\u53f0\uff0c\u5982GitHub\u3001GitLab\u7b49\u3002\u53d1\u5e03\u5230\u8fd9\u4e9b\u5e73\u53f0\u53ef\u4ee5\u589e\u52a0\u9879\u76ee\u7684\u66dd\u5149\u5ea6\uff0c\u5e76\u4fbf\u4e8e\u793e\u533a\u8d21\u732e\u8005\u8fdb\u884c\u534f\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u53d1\u5e03\u5230GitHub\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684GitHub\u4ed3\u5e93\u3002<\/li>\n<li>\u5c06\u9879\u76ee\u4ee3\u7801\u63a8\u9001\u5230GitHub\u4ed3\u5e93\uff1a\n<pre><code class=\"language-bash\">git init<\/p>\n<p>git add .<\/p>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<p>git remote add origin https:\/\/github.com\/yourusername\/my_project.git<\/p>\n<p>git push -u origin master<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728GitHub\u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Release\uff0c\u4e0a\u4f20\u751f\u6210\u7684\u5206\u53d1\u5305\u3002<\/li>\n<\/ol>\n<p><p>\u53d1\u5e03\u5230GitLab\u7684\u6b65\u9aa4\u7c7b\u4f3c\uff0c\u53ea\u9700\u5728GitLab\u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4ed3\u5e93\uff0c\u5e76\u5c06\u4ee3\u7801\u63a8\u9001\u5230GitLab\u4ed3\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u6210\u529f\u5730\u5728Python3\u4e2d\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\u3002\u786e\u4fdd\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u4ed4\u7ec6\u5b8c\u6210\uff0c\u4ee5\u786e\u4fdd\u53d1\u5e03\u7684\u5305\u7a33\u5b9a\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u51c6\u5907Python3\u9879\u76ee\u4ee5\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\uff1f<\/strong><br \/>\u5728\u51c6\u5907Python3\u9879\u76ee\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u9879\u76ee\u7ed3\u6784\u6e05\u6670\uff0c\u5305\u542b<code>setup.py<\/code>\u3001<code>requirements.txt<\/code>\u548c\u6587\u6863\u7b49\u5fc5\u8981\u6587\u4ef6\u3002<code>setup.py<\/code>\u662f\u9879\u76ee\u7684\u6838\u5fc3\uff0c\u5b9a\u4e49\u4e86\u9879\u76ee\u7684\u5143\u6570\u636e\u548c\u4f9d\u8d56\u9879\u3002\u786e\u4fdd\u5c06\u4ee3\u7801\u548c\u4f9d\u8d56\u9879\u6253\u5305\u5230\u4e00\u4e2a\u7ed3\u6784\u826f\u597d\u7684\u76ee\u5f55\u4e2d\uff0c\u8fd9\u6837\u7528\u6237\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\u6765\u6253\u5305\u548c\u53d1\u5e03Python3\u8f6f\u4ef6\uff1f<\/strong><br \/>Python\u793e\u533a\u63d0\u4f9b\u4e86\u591a\u4e2a\u5de5\u5177\u6765\u534f\u52a9\u6253\u5305\u548c\u53d1\u5e03\u8f6f\u4ef6\u3002\u5e38\u7528\u7684\u5de5\u5177\u5305\u62ec<code>setuptools<\/code>\u548c<code>wheel<\/code>\uff0c\u5b83\u4eec\u80fd\u591f\u5c06\u9879\u76ee\u6253\u5305\u4e3a\u53ef\u5206\u53d1\u7684\u683c\u5f0f\u3002\u6b64\u5916\uff0c<code>twine<\/code>\u662f\u4e00\u4e2a\u5b89\u5168\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u5305\u4e0a\u4f20\u5230Python Package Index (PyPI)\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u7b80\u5316\u53d1\u5e03\u8fc7\u7a0b\uff0c\u786e\u4fdd\u8f6f\u4ef6\u80fd\u591f\u88ab\u5176\u4ed6\u7528\u6237\u8f7b\u677e\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728PyPI\u4e0a\u53d1\u5e03\u6211\u7684Python3\u8f6f\u4ef6\uff1f<\/strong><br \/>\u5728PyPI\u4e0a\u53d1\u5e03\u8f6f\u4ef6\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2aPyPI\u8d26\u6237\u3002\u5b8c\u6210\u9879\u76ee\u7684\u6253\u5305\u540e\uff0c\u4f7f\u7528<code>twine<\/code>\u5c06\u751f\u6210\u7684\u5206\u53d1\u6587\u4ef6\u4e0a\u4f20\u5230PyPI\u3002\u8fd0\u884c<code>twine upload dist\/*<\/code>\u547d\u4ee4\uff0c\u8f93\u5165\u4f60\u7684PyPI\u8d26\u6237\u4fe1\u606f\u540e\uff0c\u8f6f\u4ef6\u5c06\u88ab\u6210\u529f\u53d1\u5e03\u3002\u786e\u4fdd\u5728\u53d1\u5e03\u4e4b\u524d\u5bf9\u8f6f\u4ef6\u8fdb\u884c\u5145\u5206\u6d4b\u8bd5\uff0c\u4ee5\u4fdd\u8bc1\u5176\u7a33\u5b9a\u6027\u548c\u529f\u80fd\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\u8fdb\u884c\u8f6f\u4ef6\u53d1\u5e03\u7684\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u6253\u5305\u5de5\u5177\u3001\u7f16\u5199setup.py\u6587\u4ef6\u3001\u751f\u6210\u5206\u53d1\u5305\u3001\u4e0a\u4f20\u5230PyPI\u3001\u7f16 [&hellip;]","protected":false},"author":3,"featured_media":1091545,"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\/1091540"}],"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=1091540"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091540\/revisions"}],"predecessor-version":[{"id":1091549,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091540\/revisions\/1091549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091545"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}