{"id":1131859,"date":"2025-01-08T20:52:07","date_gmt":"2025-01-08T12:52:07","guid":{"rendered":""},"modified":"2025-01-08T20:52:09","modified_gmt":"2025-01-08T12:52:09","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%87%aa%e5%b7%b1%e7%9a%84%e5%88%9b%e5%bb%ba%e7%9a%84%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131859.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u4f7f\u7528\u81ea\u5df1\u7684\u521b\u5efa\u7684\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101533\/e2b362a2-07d2-4330-a915-227678c2f2a1.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u4f7f\u7528\u81ea\u5df1\u7684\u521b\u5efa\u7684\u5305\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u81ea\u5df1\u521b\u5efa\u7684\u5305\uff0c\u9700\u8981\u9075\u5faa\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u5305\u3001\u914d\u7f6e\u5305\u3001\u5b89\u88c5\u5305\u3001\u5bfc\u5165\u5305\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u521b\u5efa\u5305<\/strong> \u662f\u6700\u57fa\u672c\u7684\u4e00\u6b65\uff0c<strong>\u914d\u7f6e\u5305<\/strong> \u6d89\u53ca\u5230\u8bbe\u7f6e\u5305\u7684\u5143\u6570\u636e\u548c\u4f9d\u8d56\u9879\uff0c<strong>\u5b89\u88c5\u5305<\/strong> \u5219\u662f\u5c06\u5305\u5b89\u88c5\u5230\u73af\u5883\u4e2d\uff0c\u6700\u540e<strong>\u5bfc\u5165\u5305<\/strong> \u662f\u5728\u4ee3\u7801\u4e2d\u5b9e\u9645\u4f7f\u7528\u5305\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5305<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa\u76ee\u5f55\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b50\u76ee\u5f55\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5047\u8bbe\u4f60\u7684\u5305\u540d\u4e3a <code>mypackage<\/code>\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u7684\u7ed3\u6784\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>    \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u251c\u2500\u2500 module1.py<\/p>\n<p>    \u2514\u2500\u2500 module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>__init__.py<\/code> \u6587\u4ef6\u662f\u5fc5\u987b\u7684\uff0c\u5b83\u4f7f\u5f97 <code>mypackage<\/code> \u76ee\u5f55\u88abPython\u89e3\u91ca\u5668\u8bc6\u522b\u4e3a\u4e00\u4e2a\u5305\u3002 <code>module1.py<\/code> \u548c <code>module2.py<\/code> \u662f\u5305\u4e2d\u7684\u6a21\u5757\u6587\u4ef6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728 <code>module1.py<\/code> \u548c <code>module2.py<\/code> \u4e2d\u7f16\u5199\u4f60\u9700\u8981\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/module1.py<\/p>\n<p>def func1():<\/p>\n<p>    return &quot;This is function 1 from module 1&quot;<\/p>\n<h2><strong>mypackage\/module2.py<\/strong><\/h2>\n<p>def func2():<\/p>\n<p>    return &quot;This is function 2 from module 2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u5305<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa setup.py \u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u5305\u53ef\u4ee5\u88ab\u5b89\u88c5\u548c\u4f7f\u7528\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a <code>setup.py<\/code> \u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u5305\u542b\u5305\u7684\u5143\u6570\u636e\u548c\u5b89\u88c5\u4fe1\u606f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mypackage&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # \u8fd9\u91cc\u53ef\u4ee5\u5217\u51fa\u4f60\u7684\u5305\u4f9d\u8d56\u7684\u5176\u4ed6Python\u5305\uff0c\u4f8b\u5982<\/p>\n<p>        # &#39;numpy&#39;,<\/p>\n<p>        # &#39;pandas&#39;,<\/p>\n<p>    ],<\/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 simple example package&#39;,<\/p>\n<p>    url=&#39;https:\/\/github.com\/yourusername\/mypackage&#39;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b89\u88c5\u5305<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528 pip \u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5728\u5305\u76ee\u5f55\u4e0b\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8bfb\u53d6 <code>setup.py<\/code> \u6587\u4ef6\u7684\u4fe1\u606f\u5e76\u5b89\u88c5\u5305\u5230Python\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bfc\u5165\u5305<\/h3>\n<\/p>\n<p><h4>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u5305\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># test.py<\/p>\n<p>from mypackage.module1 import func1<\/p>\n<p>from mypackage.module2 import func2<\/p>\n<p>print(func1())<\/p>\n<p>print(func2())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u4f60\u5c31\u6210\u529f\u5730\u521b\u5efa\u3001\u914d\u7f6e\u3001\u5b89\u88c5\u5e76\u4f7f\u7528\u4e86\u81ea\u5df1\u7684Python\u5305\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e94\u3001\u53d1\u5e03\u5230PyPI<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa\u8d26\u6237<\/h4>\n<\/p>\n<p><p>\u5728\u53d1\u5e03\u4f60\u7684\u5305\u4e4b\u524d\uff0c\u9700\u8981\u5728 <a href=\"https:\/\/pypi.org\/\">Python Package Index (PyPI)<\/a> \u521b\u5efa\u4e00\u4e2a\u8d26\u6237\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 Twine \u53d1\u5e03<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5 <code>twine<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\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>\u4f7f\u7528 <code>twine<\/code> \u4e0a\u4f20\u5230 PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u7684\u5305\u5c31\u53ef\u4ee5\u88ab\u5168\u7403\u7684Python\u7528\u6237\u4e0b\u8f7d\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u516d\u3001\u7ef4\u62a4\u548c\u66f4\u65b0\u5305<\/h3>\n<\/p>\n<p><h4>\u66f4\u65b0\u7248\u672c\u53f7<\/h4>\n<\/p>\n<p><p>\u6bcf\u6b21\u66f4\u65b0\u5305\u65f6\uff0c\u9700\u8981\u4fee\u6539 <code>setup.py<\/code> \u6587\u4ef6\u4e2d\u7684 <code>version<\/code> \u5b57\u6bb5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>setup(<\/p>\n<p>    ...<\/p>\n<p>    version=&#39;0.2&#39;,<\/p>\n<p>    ...<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u53d1\u5e03\u65b0\u7248\u672c<\/h4>\n<\/p>\n<p><p>\u91cd\u590d\u751f\u6210\u5206\u53d1\u5305\u548c\u4f7f\u7528 <code>twine<\/code> \u4e0a\u4f20\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py sdist bdist_wheel<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<hr>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u5305\u5bfc\u5165\u5931\u8d25<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8def\u5f84\u95ee\u9898<\/strong>: \u786e\u4fdd\u5305\u7684\u8def\u5f84\u5728 <code>PYTHONPATH<\/code> \u4e2d\u3002<\/li>\n<li><strong>\u547d\u540d\u51b2\u7a81<\/strong>: \u786e\u4fdd\u5305\u540d\u548c\u6a21\u5757\u540d\u4e0d\u4e0e\u5df2\u6709\u7684\u5305\u6216\u6a21\u5757\u51b2\u7a81\u3002<\/li>\n<\/ul>\n<p><h4>\u4f9d\u8d56\u9879\u5b89\u88c5\u5931\u8d25<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7248\u672c\u4e0d\u517c\u5bb9<\/strong>: \u786e\u4fdd\u5728 <code>setup.py<\/code> \u4e2d\u6b63\u786e\u6307\u5b9a\u4e86\u4f9d\u8d56\u9879\u7684\u7248\u672c\u8303\u56f4\u3002<\/li>\n<li><strong>\u7f51\u7edc\u95ee\u9898<\/strong>: \u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u6216\u8005\u4f7f\u7528\u955c\u50cf\u6e90\u3002<\/li>\n<\/ul>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u987a\u5229\u5730\u5728Python\u4e2d\u4f7f\u7528\u81ea\u5df1\u521b\u5efa\u7684\u5305\uff0c\u5e76\u8fdb\u884c\u53d1\u5e03\u548c\u7ef4\u62a4\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u5305\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u521b\u5efa\u4e00\u4e2aPython\u5305\u5e76\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u5b83\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2aPython\u5305\uff0c\u60a8\u9700\u8981\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u5728\u8fd9\u4e2a\u6587\u4ef6\u5939\u4e2d\u7f16\u5199\u60a8\u7684\u6a21\u5757\u4ee3\u7801\u3002\u4f7f\u7528<code>pip install -e .<\/code>\u547d\u4ee4\u53ef\u4ee5\u5c06\u8be5\u5305\u5b89\u88c5\u4e3a\u53ef\u7f16\u8f91\u6a21\u5f0f\uff0c\u4ece\u800c\u5728\u9879\u76ee\u4e2d\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bfc\u5165\u81ea\u5b9a\u4e49\u5305\u7684\u6a21\u5757\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u81ea\u5b9a\u4e49\u5305\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u5305\u540d\u4e3a<code>my_package<\/code>\uff0c\u6a21\u5757\u540d\u4e3a<code>my_module<\/code>\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>from my_package import my_module<\/code>\u6216<code>import my_package.my_module<\/code>\u6765\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u7c7b\u3002<\/p>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5305\u65f6\u5982\u4f55\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u81ea\u5b9a\u4e49\u5305\u65f6\uff0c\u53ef\u4ee5\u5728<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4f9d\u8d56\u5173\u7cfb\u3002\u901a\u8fc7\u5728<code>install_requires<\/code>\u53c2\u6570\u4e2d\u5217\u51fa\u6240\u9700\u7684\u5e93\uff0c\u7528\u6237\u5728\u5b89\u88c5\u60a8\u7684\u5305\u65f6\u4f1a\u81ea\u52a8\u5b89\u88c5\u8fd9\u4e9b\u4f9d\u8d56\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u5171\u4eab\u4f9d\u8d56\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u81ea\u5df1\u521b\u5efa\u7684\u5305\uff0c\u9700\u8981\u9075\u5faa\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u5305\u3001\u914d\u7f6e\u5305\u3001\u5b89\u88c5\u5305\u3001\u5bfc\u5165\u5305\u3002 \u5176\u4e2d\uff0c\u521b\u5efa\u5305 \u662f\u6700\u57fa\u672c\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1131866,"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\/1131859"}],"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=1131859"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131859\/revisions"}],"predecessor-version":[{"id":1131868,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131859\/revisions\/1131868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131866"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}