{"id":1101061,"date":"2025-01-08T15:45:49","date_gmt":"2025-01-08T07:45:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101061.html"},"modified":"2025-01-08T15:45:51","modified_gmt":"2025-01-08T07:45:51","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%a8%a1%e5%9d%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101061.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064020\/19c1db3d-4684-4121-8fdf-c646b594aabf.webp\" alt=\"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u7684\u4e3b\u8981\u6b65\u9aa4\u662f<strong>\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6\u3001\u5728\u6587\u4ef6\u4e2d\u7f16\u5199\u51fd\u6570\u548c\u53d8\u91cf\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757<\/strong>\u3002\u6a21\u5757\u662f\u5305\u542bPython\u5b9a\u4e49\u548c\u8bed\u53e5\u7684\u6587\u4ef6\u3002\u6587\u4ef6\u540d\u5373\u6a21\u5757\u540d\uff0c\u52a0\u4e0a\u4e00\u4e2a<code>.py<\/code>\u6269\u5c55\u540d\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u547d\u540d\u4e3a<code>mymodule.py<\/code>\u3002<\/li>\n<li>\u5728\u6587\u4ef6\u4e2d\u7f16\u5199\u4e00\u4e9b\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/li>\n<li>\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a<code>.py<\/code>\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6253\u5f00\u4f60\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff08\u5982VSCode\u3001PyCharm\u7b49\uff09\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u547d\u540d\u4e3a<code>mymodule.py<\/code>\u3002\u8fd9\u5c31\u662f\u4f60\u7684\u6a21\u5757\u6587\u4ef6\u3002\u8bb0\u4f4f\uff0c\u6587\u4ef6\u540d\u5fc5\u987b\u4ee5<code>.py<\/code>\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u6587\u4ef6\u4e2d\u7f16\u5199\u51fd\u6570\u548c\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684<code>mymodule.py<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e9bPython\u4ee3\u7801\u3002\u53ef\u4ee5\u5b9a\u4e49\u51fd\u6570\u3001\u53d8\u91cf\u3001\u7c7b\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>PI = 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u95ee\u5019\u8bed\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>add<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>PI<\/code>\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u53e6\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u547d\u540d\u4e3a<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528<code>mymodule<\/code>\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import mymodule<\/p>\n<p>print(mymodule.greet(&quot;Alice&quot;))<\/p>\n<p>print(mymodule.add(5, 3))<\/p>\n<p>print(mymodule.PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u4e86<code>mymodule<\/code>\uff0c\u7136\u540e\u8c03\u7528\u4e86\u6a21\u5757\u4e2d\u7684<code>greet<\/code>\u548c<code>add<\/code>\u51fd\u6570\uff0c\u5e76\u8bbf\u95ee\u4e86<code>PI<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u51fd\u6570\u548c\u53d8\u91cf\u5b9a\u4e49\uff0cPython\u6a21\u5757\u8fd8\u652f\u6301\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u6765\u4f7f\u6a21\u5757\u65e2\u53ef\u4ee5\u88ab\u5bfc\u5165\u4e5f\u53ef\u4ee5\u72ec\u7acb\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>PI = 3.14159<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    print(greet(&quot;World&quot;))<\/p>\n<p>    print(add(2, 3))<\/p>\n<p>    print(PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>if __name__ == &quot;__main__&quot;:<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u53ea\u6709\u5728\u6a21\u5757\u4f5c\u4e3a\u72ec\u7acb\u811a\u672c\u8fd0\u884c\u65f6\u624d\u4f1a\u6267\u884c\u3002\u5982\u679c\u8be5\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u88ab\u5bfc\u5165\uff0c\u5219\u8be5\u5757\u5185\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>import<\/code>\u8bed\u53e5\uff0cPython\u8fd8\u5141\u8bb8\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from mymodule import greet, add, PI<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p>print(add(5, 3))<\/p>\n<p>print(PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u4ece\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u6a21\u5757\u540d\u4f5c\u4e3a\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u7684\u5305\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a\u5c06\u591a\u4e2a\u6a21\u5757\u7ec4\u7ec7\u6210\u5305\uff08package\uff09\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u8be5\u76ee\u5f55\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5305\u7ed3\u6784\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u6216\u5b9a\u4e49\u5305\u7ea7\u522b\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/__init__.py<\/p>\n<p>from .module1 import function1<\/p>\n<p>from .module2 import function2<\/p>\n<p>__all__ = [&quot;function1&quot;, &quot;function2&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>__init__.py<\/code>\u6587\u4ef6\u5bfc\u5165\u4e86<code>module1<\/code>\u548c<code>module2<\/code>\u4e2d\u7684\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230<code>__all__<\/code>\u5217\u8868\u4e2d\u3002\u8fd9\u6837\uff0c\u5f53\u4f60\u5bfc\u5165\u5305\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from mypackage import function1, function2<\/p>\n<p>print(function1())<\/p>\n<p>print(function2())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6a21\u5757\u7684\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6a21\u5757\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\uff0c\u53ef\u4ee5\u4e3a\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u3002\u6587\u6863\u5b57\u7b26\u4e32\u662f\u653e\u5728\u51fd\u6570\u6216\u7c7b\u5b9a\u4e49\u4e0b\u7684\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u63cf\u8ff0\u5176\u529f\u80fd\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def greet(name):<\/p>\n<p>    &quot;&quot;&quot;Return a greeting message for the given name.&quot;&quot;&quot;<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;Return the sum of two numbers.&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>PI = 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528<code>help<\/code>\u51fd\u6570\u67e5\u770b\u51fd\u6570\u6216\u7c7b\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import mymodule<\/p>\n<p>help(mymodule.greet)<\/p>\n<p>help(mymodule.add)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6a21\u5757\u7684\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u6a21\u5757\u65f6\uff0c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def divide(a, b):<\/p>\n<p>    &quot;&quot;&quot;Return the division of a by b.&quot;&quot;&quot;<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        return &quot;Cannot divide by zero&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>divide<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406<code>ZeroDivisionError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import mymodule<\/p>\n<p>print(mymodule.divide(10, 2))<\/p>\n<p>print(mymodule.divide(10, 0))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u5728\u9047\u5230\u5f02\u5e38\u60c5\u51b5\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u800c\u662f\u8fd4\u56de\u9002\u5f53\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6a21\u5757\u7684\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6a21\u5757\u7684\u6b63\u786e\u6027\uff0c\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>unittest<\/code>\u6a21\u5757\u6765\u7f16\u5199\u548c\u8fd0\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># test_mymodule.py<\/p>\n<p>import unittest<\/p>\n<p>import mymodule<\/p>\n<p>class TestMyModule(unittest.TestCase):<\/p>\n<p>    def test_greet(self):<\/p>\n<p>        self.assertEqual(mymodule.greet(&quot;Alice&quot;), &quot;Hello, Alice!&quot;)<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(mymodule.add(5, 3), 8)<\/p>\n<p>    def test_divide(self):<\/p>\n<p>        self.assertEqual(mymodule.divide(10, 2), 5)<\/p>\n<p>        self.assertEqual(mymodule.divide(10, 0), &quot;Cannot divide by zero&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u7f16\u5199\u4e86\u6d4b\u8bd5\u4ee3\u7801\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e09\u4e2a\u6d4b\u8bd5\u65b9\u6cd5\u6765\u6d4b\u8bd5<code>greet<\/code>\u3001<code>add<\/code>\u548c<code>divide<\/code>\u51fd\u6570\u3002\u8fd0\u884c\u6d4b\u8bd5\u4ee3\u7801\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u7684\u529f\u80fd\u6b63\u786e\u65e0\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6a21\u5757\u7684\u6587\u6863\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6a21\u5757\u7684\u6587\u6863\u66f4\u52a0\u4e13\u4e1a\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u81ea\u52a8\u751f\u6210\u6587\u6863\u3002Sphinx\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u6587\u6863\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aHTML\u3001PDF\u7b49\u683c\u5f0f\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Sphinx\u751f\u6210\u6587\u6863\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Sphinx\uff1a<code>pip install sphinx<\/code><\/li>\n<li>\u8fd0\u884c<code> sphinx-quickstart<\/code>\u547d\u4ee4\u521d\u59cb\u5316Sphinx\u9879\u76ee\u3002<\/li>\n<li>\u914d\u7f6eSphinx\u9879\u76ee\uff0c\u6307\u5b9a\u6a21\u5757\u8def\u5f84\u3002<\/li>\n<li>\u8fd0\u884c<code>make html<\/code>\u547d\u4ee4\u751f\u6210HTML\u6587\u6863\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u751f\u6210\u4e13\u4e1a\u7684\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u6a21\u5757\u66f4\u6613\u4e8e\u4f7f\u7528\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u6a21\u5757\u7684\u53d1\u5e03<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5982\u679c\u5e0c\u671b\u5c06\u6a21\u5757\u53d1\u5e03\u5230Python\u5305\u7d22\u5f15\uff08PyPI\uff09\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u53ef\u4ee5\u5b89\u88c5\u548c\u4f7f\u7528\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5305\u542b\u6a21\u5757\u7684\u5143\u6570\u636e\u548c\u914d\u7f6e\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u4e0a\u4f20\u6a21\u5757\u5230PyPI\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>setup.py<\/code>\u793a\u4f8b\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=&quot;mymodule&quot;,<\/p>\n<p>    version=&quot;0.1&quot;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    author=&quot;Your Name&quot;,<\/p>\n<p>    author_email=&quot;your.email@example.com&quot;,<\/p>\n<p>    description=&quot;A simple example module&quot;,<\/p>\n<p>    long_description=open(&quot;README.md&quot;).read(),<\/p>\n<p>    long_description_content_type=&quot;text\/markdown&quot;,<\/p>\n<p>    url=&quot;https:\/\/github.com\/yourusername\/mymodule&quot;,<\/p>\n<p>    classifiers=[<\/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=&quot;&gt;=3.6&quot;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>setuptools<\/code>\u5de5\u5177\u5b9a\u4e49\u4e86\u6a21\u5757\u7684\u5143\u6570\u636e\u548c\u914d\u7f6e\u4fe1\u606f\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u6a21\u5757\u4e0a\u4f20\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p>python setup.py sdist<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u6a21\u5757\u53d1\u5e03\u5230PyPI\uff0c\u5176\u4ed6\u4eba\u53ef\u4ee5\u901a\u8fc7<code>pip install mymodule<\/code>\u547d\u4ee4\u5b89\u88c5\u548c\u4f7f\u7528\u4f60\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u9ad8\u7ea7\u529f\u80fd\u548c\u5de5\u5177\u4f7f\u6a21\u5757\u66f4\u52a0\u4e13\u4e1a\u548c\u6613\u7528\u3002\u521b\u5efa\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u9879\u76ee\u7684\u7ec4\u7ec7\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u521b\u5efaPython\u6a21\u5757\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u60a8\u9700\u8981\u9996\u5148\u7f16\u5199\u4e00\u4e2aPython\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.py<\/code>\uff09\uff0c\u5728\u5176\u4e2d\u5b9a\u4e49\u60a8\u60f3\u8981\u7684\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\u3002\u5b8c\u6210\u540e\uff0c\u53ea\u9700\u5728\u53e6\u4e00\u4e2aPython\u811a\u672c\u4e2d\u4f7f\u7528<code>import mymodule<\/code>\u5373\u53ef\u5bfc\u5165\u8be5\u6a21\u5757\u5e76\u4f7f\u7528\u5176\u4e2d\u7684\u5185\u5bb9\u3002\u786e\u4fdd\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u5728Python\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u4ee5\u4fbf\u80fd\u591f\u6210\u529f\u5bfc\u5165\u3002<\/p>\n<p><strong>\u6a21\u5757\u4e0e\u5305\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6a21\u5757\u662f\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542bPython\u4ee3\u7801\uff0c\u53ef\u4ee5\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u800c\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u5e38\u4f1a\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\u3002\u5305\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u7ed3\u6784\u5316\u548c\u53ef\u7ef4\u62a4\u3002\u4e86\u89e3\u6a21\u5757\u548c\u5305\u7684\u533a\u522b\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7ba1\u7406\u5927\u578b\u9879\u76ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6a21\u5757\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u5728\u81ea\u5b9a\u4e49\u6a21\u5757\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7684\u8fc7\u7a0b\u4e0e\u5728\u666e\u901aPython\u811a\u672c\u4e2d\u76f8\u540c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u901a\u8fc7<code>pip install library_name<\/code>\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u7136\u540e\uff0c\u5728\u6a21\u5757\u6587\u4ef6\u4e2d\u4f7f\u7528<code>import library_name<\/code>\u8bed\u53e5\u5bfc\u5165\u8be5\u5e93\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u6a21\u5757\u4e2d\u8c03\u7528\u5e93\u7684\u529f\u80fd\uff0c\u589e\u5f3a\u6a21\u5757\u7684\u529f\u80fd\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u7684\u4e3b\u8981\u6b65\u9aa4\u662f\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\u5e76\u4fdd\u5b58\u4e3a.py\u6587\u4ef6\u3001\u5728\u6587\u4ef6\u4e2d\u7f16\u5199\u51fd\u6570\u548c\u53d8\u91cf\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1101070,"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\/1101061"}],"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=1101061"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101061\/revisions"}],"predecessor-version":[{"id":1101072,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101061\/revisions\/1101072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101070"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}