{"id":1016017,"date":"2024-12-27T12:12:55","date_gmt":"2024-12-27T04:12:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016017.html"},"modified":"2024-12-27T12:13:10","modified_gmt":"2024-12-27T04:13:10","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%b7%b1%e5%88%b6%e4%bd%9c%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016017.html","title":{"rendered":"python\u5982\u4f55\u81ea\u5df1\u5236\u4f5c\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095700\/845f1fc0-76f2-477c-8a9a-533b92123920.webp\" alt=\"python\u5982\u4f55\u81ea\u5df1\u5236\u4f5c\u6a21\u5757\" \/><\/p>\n<p><p> \u5236\u4f5cPython\u6a21\u5757\u662f\u6269\u5c55\u7a0b\u5e8f\u529f\u80fd\u3001\u7ec4\u7ec7\u4ee3\u7801\u548c\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u6027\u7684\u6709\u6548\u65b9\u6cd5\u3002<strong>\u5236\u4f5cPython\u6a21\u5757\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\u7684Python\u6587\u4ef6\u3001\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u7f6e\u5728\u5408\u9002\u7684\u76ee\u5f55\u4e2d\u3001\u4f7f\u7528\u5bfc\u5165\u8bed\u53e5\u5728\u5176\u4ed6Python\u811a\u672c\u4e2d\u8c03\u7528\u6a21\u5757<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4e00\u6b65\u6b65\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5df1\u7684Python\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u6a21\u5757\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2aPython\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\u3002\u6a21\u5757\u7684\u6587\u4ef6\u540d\u5c06\u6210\u4e3a\u6a21\u5757\u7684\u540d\u79f0\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4f60\u9700\u8981\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>1. \u7f16\u5199\u6a21\u5757\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efaPython\u6a21\u5757\u65f6\uff0c\u4f60\u9700\u8981\u8003\u8651\u6a21\u5757\u7684\u76ee\u7684\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u5b66\u8fd0\u7b97\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u6570\u5b66\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_math.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u548c&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u5dee&quot;&quot;&quot;<\/p>\n<p>    return a - b<\/p>\n<p>def multiply(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u79ef&quot;&quot;&quot;<\/p>\n<p>    return a * b<\/p>\n<p>def divide(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u5546&quot;&quot;&quot;<\/p>\n<p>    if b == 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)<\/p>\n<p>    return a \/ b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6a21\u5757\u6587\u4ef6\u7684\u5b58\u653e\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9Python\u8bc6\u522b\u4f60\u7684\u6a21\u5757\uff0c\u4f60\u9700\u8981\u5c06\u5176\u5b58\u653e\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6700\u7b80\u5355\u7684\u505a\u6cd5\u662f\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u5728\u4e0e\u4f60\u7684\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06\u6a21\u5757\u6587\u4ef6\u653e\u5728Python\u7684\u5e93\u76ee\u5f55\u4e2d\u6216\u8005\u901a\u8fc7\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u6a21\u5757\u5e76\u5c06\u5176\u653e\u7f6e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\uff0c\u5c31\u53ef\u4ee5\u5728\u4f60\u7684Python\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h3>1. \u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u4f60\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_math<\/p>\n<p>result = my_math.add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u529f\u80fd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_math import add<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u522b\u540d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6a21\u5757\u540d\u53ef\u80fd\u6bd4\u8f83\u957f\u6216\u8005\u5bb9\u6613\u548c\u5176\u4ed6\u6a21\u5757\u51b2\u7a81\u3002\u8fd9\u65f6\uff0c\u4f60\u53ef\u4ee5\u4e3a\u6a21\u5757\u6216\u8005\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u529f\u80fd\u6307\u5b9a\u4e00\u4e2a\u522b\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_math as mm<\/p>\n<p>result = mm.add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u6240\u6709\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import *<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u4f7f\u7528\u65f6\u9700\u8c28\u614e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_math import *<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6a21\u5757\u4e2d\u7684\u7279\u6b8a\u53d8\u91cf<\/p>\n<\/p>\n<p><p>Python\u6a21\u5757\u4e2d\u6709\u4e00\u4e9b\u7279\u6b8a\u53d8\u91cf\uff0c\u5982<code>__name__<\/code>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u66f4\u7075\u6d3b\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1. <code>__name__<\/code>\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a<code>&quot;__main__&quot;<\/code>\uff1b\u800c\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a\u6a21\u5757\u540d\u3002\u8fd9\u4e00\u7279\u6027\u901a\u5e38\u7528\u4e8e\u7f16\u5199\u6a21\u5757\u7684\u6d4b\u8bd5\u4ee3\u7801\u6216\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_math.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    # \u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884cmy_math.py\u65f6\uff0c\u8fd9\u6bb5\u4ee3\u7801\u624d\u4f1a\u6267\u884c<\/p>\n<p>    print(&quot;\u8fd0\u884c\u6d4b\u8bd5&quot;)<\/p>\n<p>    print(add(2, 3))  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6a21\u5757\u7684\u7ec4\u7ec7<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u5c06\u4ee3\u7801\u62c6\u5206\u4e3a\u591a\u4e2a\u6a21\u5757\u662f\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u4f60\u53ef\u4ee5\u5c06\u591a\u4e2a\u76f8\u5173\u6a21\u5757\u7ec4\u7ec7\u6210\u4e00\u4e2a\u5305\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u5305<\/h3>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\u3002\u5305\u4e2d\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f46\u901a\u5e38\u7528\u4e8e\u521d\u59cb\u5316\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-text\">my_package\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u53f7<code>&quot;.&quot;<\/code>\u6765\u6307\u5b9a\u6a21\u5757\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package import module1<\/p>\n<h2><strong>\u4f7f\u7528module1\u4e2d\u7684\u529f\u80fd<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6a21\u5757\u7684\u6587\u6863\u548c\u6ce8\u91ca<\/p>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u4e2d\u6dfb\u52a0\u6587\u6863\u548c\u6ce8\u91ca\u662f\u4e00\u4e2a\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>1. \u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u4e3a\u6a21\u5757\u3001\u51fd\u6570\u548c\u7c7b\u6dfb\u52a0\u8bf4\u660e\u3002\u8fd9\u4e9b\u5b57\u7b26\u4e32\u901a\u5e38\u653e\u5728\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\uff0c\u5e76\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&quot;&quot;&quot;<\/p>\n<p>my_math\u6a21\u5757\u63d0\u4f9b\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u529f\u80fd\u3002<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u548c&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u89e3\u91ca\u590d\u6742\u7684\u903b\u8f91\u6216\u8bb0\u5f55\u91cd\u8981\u7684\u6ce8\u610f\u4e8b\u9879\u3002\u6ce8\u91ca\u5e94\u7b80\u660e\u627c\u8981\uff0c\u5e76\u4e0e\u4ee3\u7801\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    &quot;&quot;&quot;\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u5546&quot;&quot;&quot;<\/p>\n<p>    if b == 0:<\/p>\n<p>        raise ValueError(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)  # \u68c0\u67e5\u9664\u6570\u662f\u5426\u4e3a\u96f6<\/p>\n<p>    return a \/ b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u6a21\u5757\u7684\u6d4b\u8bd5<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u6a21\u5757\u65f6\uff0c\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u4ee5\u9a8c\u8bc1\u6a21\u5757\u529f\u80fd\u7684\u6b63\u786e\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>unittest<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>unittest<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u6d4b\u8bd5\u6846\u67b6\u3002\u4f60\u53ef\u4ee5\u4e3a\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u6d4b\u8bd5\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>unittest<\/code>\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>import my_math<\/p>\n<p>class TestMyMath(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(my_math.add(2, 3), 5)<\/p>\n<p>    def test_divide(self):<\/p>\n<p>        self.assertRaises(ValueError, my_math.divide, 10, 0)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>pytest<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pytest<\/code>\u662f\u4e00\u4e2a\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u652f\u6301\u66f4\u590d\u6742\u7684\u6d4b\u8bd5\u573a\u666f\u548c\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u3002\u4f60\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u5f15\u5165<code>pytest<\/code>\u4ee5\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def test_add():<\/p>\n<p>    assert my_math.add(2, 3) == 5<\/p>\n<p>def test_divide():<\/p>\n<p>    with pytest.raises(ValueError):<\/p>\n<p>        my_math.divide(10, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u6a21\u5757\u7684\u53d1\u5e03<\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u7684\u6a21\u5757\u529f\u80fd\u5b8c\u6574\u5e76\u7ecf\u8fc7\u6d4b\u8bd5\u540e\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u53d1\u5e03\u5230Python\u5305\u7d22\u5f15\uff08PyPI\uff09\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u51c6\u5907\u53d1\u5e03\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u63cf\u8ff0\u6a21\u5757\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5982\u540d\u79f0\u3001\u7248\u672c\u3001\u4f5c\u8005\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;my_math&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    description=&#39;A simple math operations module&#39;,<\/p>\n<p>    author=&#39;Your Name&#39;,<\/p>\n<p>    author_email=&#39;your.email@example.com&#39;,<\/p>\n<p>    packages=[&#39;my_package&#39;],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4e0a\u4f20\u5230PyPI<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u4f20\u4e4b\u524d\uff0c\u9700\u8981\u6ce8\u518c\u4e00\u4e2aPyPI\u8d26\u6237\u5e76\u5b89\u88c5<code>twine<\/code>\u5de5\u5177\u3002\u7136\u540e\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4e0a\u4f20\u4f60\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python setup.py sdist<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u6210\u529f\u521b\u5efa\u3001\u4f7f\u7528\u548c\u53d1\u5e03\u81ea\u5df1\u7684Python\u6a21\u5757\u4e86\u3002\u5e0c\u671b\u8fd9\u7bc7\u6307\u5357\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u521b\u5efa\u4e00\u4e2aPython\u6a21\u5757\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2aPython\u6a21\u5757\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ea\u9700\u5c06\u76f8\u5173\u7684Python\u4ee3\u7801\uff08\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\uff09\u4fdd\u5b58\u5728\u4e00\u4e2a\u4ee5\u201c.py\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u60a8\u7684\u4ee3\u7801\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import mymodule<\/code>\u5728\u5176\u4ed6Python\u811a\u672c\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u3002<\/p>\n<p><strong>\u6211\u5982\u4f55\u5728\u6a21\u5757\u4e2d\u4f7f\u7528\u51fd\u6570\u548c\u7c7b\uff1f<\/strong><br \/>\u5728\u60a8\u7684\u6a21\u5757\u6587\u4ef6\u4e2d\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8c03\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>mymodule.py<\/code>\u4e2d\u6709\u4e00\u4e2a\u51fd\u6570<code>my_function()<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>mymodule.my_function()<\/code>\u6765\u8c03\u7528\u5b83\u3002\u786e\u4fdd\u5728\u6a21\u5757\u6587\u4ef6\u548c\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u76ee\u5f55\uff0c\u6216\u8005\u5c06\u6a21\u5757\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84\u4e2d\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ec4\u7ec7\u548c\u7ba1\u7406\u591a\u4e2a\u6a21\u5757\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u6709\u591a\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\uff0c\u8fd9\u6837Python\u5c31\u4f1a\u5c06\u8be5\u6587\u4ef6\u5939\u8bc6\u522b\u4e3a\u4e00\u4e2a\u5305\u3002\u60a8\u53ef\u4ee5\u5728\u5305\u4e2d\u5bfc\u5165\u5404\u4e2a\u6a21\u5757\uff0c\u4f7f\u7528<code>from mypackage import mymodule<\/code>\u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u7279\u5b9a\u6a21\u5757\u4e2d\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u7ed3\u6784\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u7ba1\u7406\uff0c\u4f7f\u5f97\u5927\u578b\u9879\u76ee\u7684\u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5236\u4f5cPython\u6a21\u5757\u662f\u6269\u5c55\u7a0b\u5e8f\u529f\u80fd\u3001\u7ec4\u7ec7\u4ee3\u7801\u548c\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u6027\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5236\u4f5cPython\u6a21\u5757\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1016051,"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\/1016017"}],"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=1016017"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016017\/revisions"}],"predecessor-version":[{"id":1016059,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016017\/revisions\/1016059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}