{"id":1024883,"date":"2024-12-30T14:17:42","date_gmt":"2024-12-30T06:17:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024883.html"},"modified":"2024-12-30T14:17:44","modified_gmt":"2024-12-30T06:17:44","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%a8%a1%e5%9d%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024883.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3d59ed94-17a9-49a4-8486-3796a378e618.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6a21\u5757\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4f7f\u7528\u6a21\u5757\u4e3b\u8981\u5305\u62ec\uff1a\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u3001\u7ba1\u7406\u6a21\u5757\u4f9d\u8d56\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5bfc\u5165\u6a21\u5757\u662f\u4f7f\u7528\u6a21\u5757\u7684\u7b2c\u4e00\u6b65\uff0c\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5c06\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u7684Python\u811a\u672c\u4e2d\u3002\u4f8b\u5982\uff0c\u5bfc\u5165\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7<code>import math<\/code>\u8bed\u53e5\u5b9e\u73b0\u3002\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u901a\u8fc7\u201c\u6a21\u5757\u540d.\u51fd\u6570\u540d\u201d\u6216\u201c\u6a21\u5757\u540d.\u53d8\u91cf\u540d\u201d\u5f62\u5f0f\u6765\u8c03\u7528\u3002\u81ea\u5b9a\u4e49\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684Python\u6587\u4ef6\u5b9e\u73b0\uff0c\u5e76\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u8be5\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u4e3a\u4e86\u7ba1\u7406\u6a21\u5757\u4f9d\u8d56\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u6765\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165\u6a21\u5757\u7684\u6700\u5e38\u89c1\u65b9\u5f0f\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5c06\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\u5230\u811a\u672c\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5bfc\u5165\u4e86<code>math<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u4e86\u5176\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<p>print(sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c\u800c\u65e0\u9700\u901a\u8fc7<code>math.sqrt<\/code>\u6765\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u7ed9\u6a21\u5757\u6216\u51fd\u6570\u8d77\u522b\u540d<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\u6216\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u7ed9\u6a21\u5757\u6216\u51fd\u6570\u8d77\u522b\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from math import sqrt as square_root<\/p>\n<p>print(np.array([1, 2, 3]))<\/p>\n<p>print(square_root(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u548c\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u201c\u6a21\u5757\u540d.\u51fd\u6570\u540d\u201d\u5f62\u5f0f\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>print(random.randint(1, 10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbf\u95ee\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u201c\u6a21\u5757\u540d.\u53d8\u91cf\u540d\u201d\u5f62\u5f0f\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee<code>math<\/code>\u6a21\u5757\u4e2d\u7684\u5e38\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.pi)<\/p>\n<p>print(math.e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u7f16\u5199\u4e00\u4e2a\u5305\u542b\u51fd\u6570\u548c\u53d8\u91cf\u7684Python\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\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>pi = 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u5e76\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.greet(&quot;Alice&quot;))<\/p>\n<p>print(mymodule.pi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ba1\u7406\u6a21\u5757\u4f9d\u8d56<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u7528\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u8fdb\u884c\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5<code>requests<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u7ba1\u7406\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fbf\u4e8e\u7ba1\u7406\u9879\u76ee\u4e2d\u7684\u4f9d\u8d56\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u5217\u51fa\u6240\u6709\u9700\u8981\u7684\u6a21\u5757\u53ca\u5176\u7248\u672c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a<code>requirements.txt<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>requests==2.25.1<\/p>\n<p>numpy==1.20.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u6839\u636e<code>requirements.txt<\/code>\u6587\u4ef6\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5bfc\u5165\u3001\u4f7f\u7528\u6a21\u5757\u4ee5\u53ca\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757\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>pi = 3.14159<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import mymodule<\/p>\n<p>import math<\/p>\n<p>import random<\/p>\n<p>from math import sqrt as square_root<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/h2>\n<p>print(mymodule.greet(&quot;Alice&quot;))<\/p>\n<p>print(mymodule.pi)<\/p>\n<h2><strong>\u4f7f\u7528\u6807\u51c6\u5e93\u6a21\u5757<\/strong><\/h2>\n<p>print(math.sqrt(16))<\/p>\n<p>print(random.randint(1, 10))<\/p>\n<h2><strong>\u4f7f\u7528\u522b\u540d<\/strong><\/h2>\n<p>print(square_root(25))<\/p>\n<h2><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u6a21\u5757<\/strong><\/h2>\n<p>print(np.array([1, 2, 3]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\uff0c\u4ee5\u53ca\u5982\u4f55\u521b\u5efa\u548c\u7ba1\u7406\u81ea\u5b9a\u4e49\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u7684\u7ec4\u7ec7\u548c\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6a21\u5757\u7684\u7ec4\u7ec7\u548c\u7ed3\u6784\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7ec4\u7ec7\u548c\u7ed3\u6784\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5305\uff08Package\uff09<\/h4>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\u3002\u5305\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u5177\u5c42\u6b21\u7ed3\u6784\u3002\u521b\u5efa\u5305\u65f6\uff0c\u9700\u8981\u5728\u5305\u76ee\u5f55\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\u3002\u8fd9\u4e2a\u6587\u4ef6\u53ef\u4ee5\u662f\u7a7a\u7684\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mypackage<\/code>\u7684\u5305\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\u4f7f\u7528\u5305\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u53f7\u8bed\u6cd5\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage import module1<\/p>\n<p>import mypackage.module2<\/p>\n<p>print(module1.some_function())<\/p>\n<p>print(mypackage.module2.some_variable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u5757\u7684\u547d\u540d<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u548c\u5305\u7684\u547d\u540d\u5e94\u9075\u5faaPython\u7684\u547d\u540d\u89c4\u8303\uff0c\u5373\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u3002\u4f8b\u5982\uff0c\u6a21\u5757\u540d<code>my_module.py<\/code>\u548c\u5305\u540d<code>my_package<\/code>\u90fd\u662f\u7b26\u5408\u89c4\u8303\u7684\u547d\u540d\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6a21\u5757\u7684\u6587\u6863\u548c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u6a21\u5757\u65f6\uff0c\u5e94\u5305\u542b\u9002\u5f53\u7684\u6587\u6863\u548c\u6ce8\u91ca\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u7406\u89e3\u548c\u4f7f\u7528\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u4e3a\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u7f16\u5199\u6587\u6863\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Returns a greeting message for the given name.<\/p>\n<p>    Parameters:<\/p>\n<p>    name (str): The name of the person to greet.<\/p>\n<p>    Returns:<\/p>\n<p>    str: The greeting message.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6a21\u5757\u7684\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6a21\u5757\u7684\u6b63\u786e\u6027\uff0c\u5e94\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u5bf9\u6a21\u5757\u8fdb\u884c\u6d4b\u8bd5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>unittest<\/code>\u6a21\u5757\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002\u4f8b\u5982\uff0c\u4e3a<code>mymodule.py<\/code>\u7f16\u5199\u6d4b\u8bd5\uff1a<\/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_pi(self):<\/p>\n<p>        self.assertAlmostEqual(mymodule.pi, 3.14159)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u6d4b\u8bd5\u65f6\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python test_mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6a21\u5757\u7684\u53d1\u5e03\u548c\u5206\u53d1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u5206\u4eab\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06\u5176\u53d1\u5e03\u5230Python\u5305\u7d22\u5f15\uff08PyPI\uff09\u3002\u4ee5\u4e0b\u662f\u53d1\u5e03\u6a21\u5757\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7f16\u5199<code>setup.py<\/code>\u6587\u4ef6<\/h4>\n<\/p>\n<p><p><code>setup.py<\/code>\u6587\u4ef6\u5305\u542b\u6a21\u5757\u7684\u5143\u6570\u636e\u548c\u5b89\u88c5\u4fe1\u606f\u3002\u4f8b\u5982\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=&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 custom 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=&#39;&gt;=3.6&#39;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210\u5206\u53d1\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u751f\u6210\u6e90\u5206\u53d1\u5305\u548c\u8f6e\u5b50\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><h4>3\u3001\u4e0a\u4f20\u5230PyPI<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>twine<\/code>\u5de5\u5177\u5c06\u5206\u53d1\u5305\u4e0a\u4f20\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install twine<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u4f20\u6210\u529f\u540e\uff0c\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u9075\u5faaPEP 8\u7f16\u7801\u89c4\u8303<\/h4>\n<\/p>\n<p><p>PEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u9075\u5faa\u8fd9\u4e9b\u89c4\u8303\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff0c\u51fd\u6570\u548c\u53d8\u91cf\u540d\u5e94\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\uff0c\u7c7b\u540d\u5e94\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u4f7f\u7528\u901a\u914d\u7b26\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u901a\u914d\u7b26\u5bfc\u5165\uff08\u5982<code>from module import *<\/code>\uff09\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u548c\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002\u5e94\u660e\u786e\u5bfc\u5165\u9700\u8981\u7684\u51fd\u6570\u548c\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt, pi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6a21\u5757\u7684\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u6a21\u5757\u65f6\u5e94\u5c3d\u91cf\u4f7f\u5176\u5177\u6709\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u901a\u7528\u51fd\u6570\u3001\u9075\u5faa\u5355\u4e00\u804c\u8d23\u539f\u5219\u3001\u7f16\u5199\u6587\u6863\u548c\u6d4b\u8bd5\u4ee3\u7801\u7b49\u65b9\u5f0f\u63d0\u9ad8\u6a21\u5757\u7684\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u793a\u4f8b\u9879\u76ee\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u9879\u76ee\u7ed3\u6784\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ec4\u7ec7\u548c\u7ba1\u7406\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code>myproject\/<\/p>\n<p>    README.md<\/p>\n<p>    setup.py<\/p>\n<p>    requirements.txt<\/p>\n<p>    mypackage\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module1.py<\/p>\n<p>        module2.py<\/p>\n<p>    tests\/<\/p>\n<p>        __init__.py<\/p>\n<p>        test_module1.py<\/p>\n<p>        test_module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u9879\u76ee\u4e2d\uff0c<code>mypackage<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u5305\uff0c<code>tests<\/code>\u76ee\u5f55\u5305\u542b\u6d4b\u8bd5\u4ee3\u7801\uff0c<code>setup.py<\/code>\u6587\u4ef6\u7528\u4e8e\u6a21\u5757\u7684\u53d1\u5e03\uff0c<code>requirements.txt<\/code>\u6587\u4ef6\u7528\u4e8e\u7ba1\u7406\u4f9d\u8d56\uff0c<code>README.md<\/code>\u6587\u4ef6\u5305\u542b\u9879\u76ee\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u4e0a\u8ff0\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u7f16\u5199\u9ad8\u8d28\u91cf\u7684\u6a21\u5757\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4f7f\u9879\u76ee\u7ed3\u6784\u6e05\u6670\u6709\u5e8f\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6a21\u5757\u7684\u5b89\u88c5\u901a\u5e38\u901a\u8fc7\u5305\u7ba1\u7406\u5de5\u5177pip\u8fdb\u884c\u3002\u9996\u5148\uff0c\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>pip install \u6a21\u5757\u540d<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5requests\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install requests<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u901a\u8fc7<code>import \u6a21\u5757\u540d<\/code>\u6765\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u5f00\u59cb\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Python\u6a21\u5757\u6709\u54ea\u4e9b\u5e38\u7528\u7c7b\u578b\uff0c\u9002\u5408\u4ec0\u4e48\u573a\u666f\uff1f<\/strong><br \/>Python\u6a21\u5757\u53ef\u4ee5\u5206\u4e3a\u6807\u51c6\u5e93\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\u548c\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u6807\u51c6\u5e93\u6a21\u5757\u5982os\u3001sys\u548cmath\uff0c\u9002\u5408\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u3001\u7cfb\u7edf\u529f\u80fd\u548c\u6570\u5b66\u8ba1\u7b97\u7b49\u3002\u7b2c\u4e09\u65b9\u6a21\u5757\u5982NumPy\u3001Pandas\u548cFlask\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001Web\u5f00\u53d1\u7b49\u573a\u666f\u3002\u81ea\u5b9a\u4e49\u6a21\u5757\u5219\u662f\u6839\u636e\u9879\u76ee\u9700\u6c42\u7f16\u5199\u7684\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u529f\u80fd\u5c01\u88c5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4fbf\u4e8e\u590d\u7528\u548c\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u6a21\u5757\u53ca\u5176\u7248\u672c\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u5df2\u5b89\u88c5\u7684Python\u6a21\u5757\u53ca\u5176\u7248\u672c\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u8f93\u5165<code>pip list<\/code>\u3002\u8fd9\u5c06\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u6a21\u5757\u7684\u540d\u79f0\u53ca\u5176\u5bf9\u5e94\u7248\u672c\u3002\u82e5\u8981\u67e5\u770b\u7279\u5b9a\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip show \u6a21\u5757\u540d<\/code>\u547d\u4ee4\uff0c\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u6a21\u5757\u7684\u7248\u672c\u3001\u4f5c\u8005\u3001\u8bb8\u53ef\u8bc1\u7b49\u4fe1\u606f\u3002\u8fd9\u5bf9\u4e8e\u7ba1\u7406\u548c\u66f4\u65b0\u6a21\u5757\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4f7f\u7528\u6a21\u5757\u4e3b\u8981\u5305\u62ec\uff1a\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528\u6a21\u5757\u5185\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u3001\u7ba1\u7406\u6a21\u5757\u4f9d\u8d56\u3002 \u5bfc\u5165\u6a21\u5757\u662f\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1024896,"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\/1024883"}],"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=1024883"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024883\/revisions"}],"predecessor-version":[{"id":1024901,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024883\/revisions\/1024901"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024896"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}