{"id":1017438,"date":"2024-12-27T12:28:05","date_gmt":"2024-12-27T04:28:05","guid":{"rendered":""},"modified":"2024-12-27T12:28:20","modified_gmt":"2024-12-27T04:28:20","slug":"%e5%a6%82%e4%bd%95%e5%ad%a6%e4%bc%9apython%e6%a8%a1%e5%9d%97%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1017438.html","title":{"rendered":"\u5982\u4f55\u5b66\u4f1aPython\u6a21\u5757\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25160326\/f1e141b9-ec03-4cb6-aa76-3870aabcc5a9.webp\" alt=\"\u5982\u4f55\u5b66\u4f1aPython\u6a21\u5757\u529f\u80fd\" \/><\/p>\n<p><p> <strong>\u8981\u5b66\u4f1aPython\u6a21\u5757\u529f\u80fd\uff0c\u4f60\u9700\u8981\uff1a\u4e86\u89e3Python\u6a21\u5757\u7684\u57fa\u7840\u6982\u5ff5\u3001\u638c\u63e1\u6a21\u5757\u7684\u5bfc\u5165\u4e0e\u4f7f\u7528\u65b9\u6cd5\u3001\u719f\u6089\u5e38\u7528\u7684\u6807\u51c6\u5e93\u6a21\u5757\u3001\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u3001\u7406\u89e3\u6a21\u5757\u7684\u5305\u7ba1\u7406\u673a\u5236\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u638c\u63e1\u6a21\u5757\u7684\u5bfc\u5165\u4e0e\u4f7f\u7528\u65b9\u6cd5<\/strong>\u662f\u975e\u5e38\u5173\u952e\u7684\u4e00\u6b65\u3002Python\u6a21\u5757\u662f\u4ee5\u6587\u4ef6\u5f62\u5f0f\u7ec4\u7ec7\u7684Python\u4ee3\u7801\u96c6\u5408\uff0c\u901a\u5e38\u5305\u542b\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u901a\u8fc7\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u590d\u7528\u4ee3\u7801\u3001\u7ec4\u7ec7\u66f4\u4e3a\u590d\u6742\u7684\u7a0b\u5e8f\u5e76\u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u3002\u5b66\u4e60\u6a21\u5757\u9996\u5148\u8981\u5b66\u4f1a\u5982\u4f55\u5bfc\u5165\u5b83\u4eec\uff1a\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u800c<code>from ... import ...<\/code>\u8bed\u53e5\u5219\u53ef\u4ee5\u4ece\u6a21\u5757\u4e2d\u9009\u62e9\u6027\u5730\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\u3002\u7406\u89e3\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b9e\u8df5\u5e94\u7528\u53bb\u52a0\u6df1\u5bf9\u6a21\u5757\u529f\u80fd\u7684\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4e86\u89e3Python\u6a21\u5757\u7684\u57fa\u7840\u6982\u5ff5<\/p>\n<\/p>\n<p><p>Python\u6a21\u5757\u662f\u6307\u4e00\u4e2a\u5305\u542bPython\u5b9a\u4e49\u548c\u8bed\u53e5\u7684\u6587\u4ef6\uff0c\u5176\u6587\u4ef6\u540d\u5c31\u662f\u6a21\u5757\u540d\u52a0\u4e0a.py\u540e\u7f00\u3002\u6a21\u5757\u7684\u6700\u5927\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u7ec4\u7ec7\u4ee3\u7801\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u590d\u7528\u3002\u6a21\u5757\u53ef\u4ee5\u88ab\u5176\u4ed6Python\u7a0b\u5e8f\u5bfc\u5165\u5e76\u4f7f\u7528\uff0c\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684\u5e93\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u6a21\u5757\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u76ee\u5f55\u3002\u4e00\u4e2a\u76ee\u5f55\u4e0b\u6709\u591a\u4e2a\u6a21\u5757\u6587\u4ef6\uff0c\u5e76\u4e14\u76ee\u5f55\u4e2d\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\u65f6\uff0c\u8fd9\u4e2a\u76ee\u5f55\u5c31\u53ef\u4ee5\u88ab\u770b\u4f5c\u4e00\u4e2a\u5305\u3002\u5305\u7684\u5b58\u5728\u4f7f\u5f97Python\u6a21\u5757\u53ef\u4ee5\u6309\u5c42\u6b21\u7ed3\u6784\u7ec4\u7ec7\uff0c\u4f7f\u5f97\u5927\u578b\u8f6f\u4ef6\u9879\u76ee\u4e2d\u7684\u4ee3\u7801\u66f4\u52a0\u6709\u6761\u7406\u3002\u6a21\u5757\u4e2d\u53ef\u4ee5\u5305\u542b\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\uff0c\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u6765\u8fdb\u884c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u638c\u63e1\u6a21\u5757\u7684\u5bfc\u5165\u4e0e\u4f7f\u7528\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u5b83\u4eec\u3002\u6700\u7b80\u5355\u7684\u5bfc\u5165\u65b9\u6cd5\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\uff0c\u8fd9\u4f1a\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\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>\u8fd9\u6bb5\u4ee3\u7801\u5bfc\u5165\u4e86\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u4e86\u5176\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u6765\u8ba1\u7b9716\u7684\u5e73\u65b9\u6839\u3002\u5bfc\u5165\u6a21\u5757\u540e\uff0c\u6a21\u5757\u540d\u53ef\u4ee5\u7528\u4f5c\u547d\u540d\u7a7a\u95f4\uff0c\u5e2e\u52a9\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5bfc\u5165\u65b9\u6cd5\u662f\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4ece\u6a21\u5757\u4e2d\u9009\u62e9\u6027\u5730\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\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\u5bfc\u5165\u4e86<code>sqrt<\/code>\u51fd\u6570\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u800c\u4e0d\u5fc5\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u3002\u5982\u679c\u9700\u8981\u5bfc\u5165\u591a\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u9017\u53f7\u5206\u9694\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><p>\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u5bfc\u5165\u7684\u6a21\u5757\u6216\u5bf9\u8c61\u8d77\u4e00\u4e2a\u522b\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math as m<\/p>\n<p>print(m.sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u6a21\u5757\u540d\u8fc7\u957f\u6216\u5bb9\u6613\u6df7\u6dc6\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u719f\u6089\u5e38\u7528\u7684\u6807\u51c6\u5e93\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6a21\u5757\uff0c\u6db5\u76d6\u4e86\u5e38\u89c1\u7684\u7f16\u7a0b\u4efb\u52a1\u3002\u719f\u6089\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><code>os<\/code>\u6a21\u5757\uff1a\u7528\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5982\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(os.getcwd())  # \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><code>sys<\/code>\u6a21\u5757\uff1a\u63d0\u4f9b\u4e86\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.version)  # \u8f93\u51faPython\u7248\u672c\u4fe1\u606f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><code>datetime<\/code>\u6a21\u5757\uff1a\u7528\u4e8e\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>print(datetime.now())  # \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><code>json<\/code>\u6a21\u5757\uff1a\u7528\u4e8e\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 30}<\/p>\n<p>json_data = json.dumps(data)<\/p>\n<p>print(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><code>random<\/code>\u6a21\u5757\uff1a\u63d0\u4f9b\u751f\u6210\u968f\u673a\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>print(random.randint(1, 10))  # \u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u548c\u5b9e\u8df5\u7f16\u5199\u4ee3\u7801\uff0c\u6df1\u5165\u7406\u89e3\u8fd9\u4e9b\u6a21\u5757\u7684\u529f\u80fd\u548c\u7528\u6cd5\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u5c06\u76f8\u5173\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u5199\u5165\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5728\u5176\u4ed6\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\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><p>\u7136\u540e\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\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><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u4ee3\u7801\u62c6\u5206\u6210\u66f4\u5c0f\u3001\u66f4\u53ef\u590d\u7528\u7684\u6a21\u5757\u3002\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5b58\u653e\u5728Python\u89e3\u91ca\u5668\u7684\u641c\u7d22\u8def\u5f84\u4e2d\uff0c\u4ee5\u4fbf\u80fd\u591f\u6210\u529f\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7406\u89e3\u6a21\u5757\u7684\u5305\u7ba1\u7406\u673a\u5236<\/p>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6a21\u5757\u901a\u5e38\u88ab\u7ec4\u7ec7\u6210\u5305\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u5fc5\u987b\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff08\u53ef\u4ee5\u662f\u7a7a\u6587\u4ef6\uff09\uff0c\u7528\u6765\u8868\u793a\u8fd9\u4e2a\u76ee\u5f55\u662f\u4e00\u4e2a\u5305\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\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>\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5305\u7684\u516c\u5171\u63a5\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mypackage\/__init__.py<\/p>\n<p>from .module1 import func1<\/p>\n<p>from .module2 import func2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u5728\u5176\u4ed6\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage import func1, func2<\/p>\n<p>func1()<\/p>\n<p>func2()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5305\u53ef\u4ee5\u6709\u6548\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u5e76\u4e14\u6e05\u6670\u5730\u8868\u8fbe\u6a21\u5757\u4e4b\u95f4\u7684\u5c42\u6b21\u5173\u7cfb\u3002\u7406\u89e3\u5e76\u5e94\u7528\u6a21\u5757\u7684\u5305\u7ba1\u7406\u673a\u5236\uff0c\u662f\u63d0\u5347Python\u9879\u76ee\u7ec4\u7ec7\u80fd\u529b\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5\u548c\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u548c\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u4f7f\u7528Python\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u547d\u540d\u89c4\u8303<\/strong>\uff1a\u6a21\u5757\u540d\u5e94\u8be5\u5168\u5c0f\u5199\uff0c\u5e76\u5c3d\u91cf\u4f7f\u7528\u7b80\u5355\u660e\u4e86\u7684\u540d\u5b57\u3002\u5305\u540d\u4e5f\u9075\u5faa\u8fd9\u4e2a\u89c4\u5219\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u5faa\u73af\u4f9d\u8d56<\/strong>\uff1a\u6a21\u5757\u4e4b\u95f4\u7684\u5faa\u73af\u4f9d\u8d56\u4f1a\u5bfc\u81f4\u5bfc\u5165\u9519\u8bef\u3002\u5728\u8bbe\u8ba1\u6a21\u5757\u65f6\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u5faa\u73af\u5bfc\u5165\uff0c\u901a\u8fc7\u91cd\u6784\u4ee3\u7801\u6216\u4f7f\u7528\u5ef6\u8fdf\u5bfc\u5165\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165<\/strong>\uff1a\u5728\u5305\u5185\u90e8\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff08\u4ee5\u70b9\u53f7\u8868\u793a\uff09\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002\u4f8b\u5982\uff0c<code>from .module1 import func1<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u6863\u548c\u6ce8\u91ca<\/strong>\uff1a\u4e3a\u6a21\u5757\u7f16\u5199\u8be6\u7ec6\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u548c\u6ce8\u91ca\uff0c\u4f7f\u5f97\u6a21\u5757\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u6a21\u5757<\/strong>\uff1a\u4e3a\u6a21\u5757\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\uff0c\u786e\u4fdd\u529f\u80fd\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u81ea\u5e26\u7684<code>unittest<\/code>\u6a21\u5757\u6216\u5176\u4ed6\u6d4b\u8bd5\u6846\u67b6\u5982<code>pytest<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5b9e\u8df5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u4f7f\u7528Python\u6a21\u5757\uff0c\u7f16\u5199\u51fa\u66f4\u4f18\u8d28\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u6df1\u5165\u5b66\u4e60Python\u6a21\u5757\u673a\u5236<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/strong>\uff1a\u5f53\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u5728\u4e00\u7cfb\u5217\u76ee\u5f55\u4e2d\u641c\u7d22\u8fd9\u4e2a\u6a21\u5757\u3002\u8fd9\u4e9b\u76ee\u5f55\u88ab\u79f0\u4e3a\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys.path<\/code>\u67e5\u770b\u548c\u4fee\u6539\u3002\u7406\u89e3\u641c\u7d22\u8def\u5f84\u6709\u52a9\u4e8e\u89e3\u51b3\u6a21\u5757\u5bfc\u5165\u5931\u8d25\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u7684\u7f13\u5b58\u673a\u5236<\/strong>\uff1aPython\u4f1a\u7f13\u5b58\u5df2\u5bfc\u5165\u7684\u6a21\u5757\uff0c\u5b58\u50a8\u5728<code>sys.modules<\/code>\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u6e05\u7a7a<code>sys.modules<\/code>\u4e2d\u7684\u67d0\u4e2a\u6a21\u5757\u6765\u5f3a\u5236\u91cd\u65b0\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u5bfc\u5165<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u8fd0\u884c\u65f6\u6761\u4ef6\u6765\u51b3\u5b9a\u5bfc\u5165\u54ea\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u5b9e\u73b0\u52a8\u6001\u5bfc\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &#39;math&#39;<\/p>\n<p>math_module = importlib.import_module(module_name)<\/p>\n<p>print(math_module.sqrt(25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u6df1\u5165\u7406\u89e3\u8fd9\u4e9b\u673a\u5236\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u4f7f\u7528Python\u6a21\u5757\uff0c\u89e3\u51b3\u66f4\u590d\u6742\u7684\u7f16\u7a0b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u638c\u63e1Python\u6a21\u5757\u7684\u529f\u80fd\u662f\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u7684\u91cd\u8981\u9014\u5f84\u3002\u901a\u8fc7\u5b66\u4e60\u6a21\u5757\u7684\u5bfc\u5165\u4e0e\u4f7f\u7528\u3001\u719f\u6089\u6807\u51c6\u5e93\u6a21\u5757\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u4ee5\u53ca\u7406\u89e3\u5305\u7ba1\u7406\u673a\u5236\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u6df1\u5165\u7406\u89e3Python\u7684\u6a21\u5757\u673a\u5236\uff0c\u5c06\u4f7f\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u6e38\u5203\u6709\u4f59\u3002\u4e0d\u65ad\u5730\u5b9e\u8df5\u548c\u603b\u7ed3\u7ecf\u9a8c\uff0c\u662f\u638c\u63e1Python\u6a21\u5757\u529f\u80fd\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u6a21\u5757\u7684\u529f\u80fd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python\u6a21\u5757\u662f\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u5b83\u4eec\u53ef\u4ee5\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u5305\u542b\u53ef\u6267\u884c\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u6a21\u5757\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5c06\u4ee3\u7801\u7ec4\u7ec7\u5f97\u66f4\u52a0\u9ad8\u6548\uff0c\u4fc3\u8fdb\u4ee3\u7801\u7684\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6a21\u5757\u4f7f\u5f97\u5728\u5927\u578b\u9879\u76ee\u4e2d\u7ba1\u7406\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u6765\u4f7f\u7528\u5176\u4ed6\u5f00\u53d1\u8005\u6216\u81ea\u5df1\u7f16\u5199\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u6211\u7684Python\u6a21\u5757\u8fdb\u884c\u5b66\u4e60\uff1f<\/strong><br \/>\u9009\u62e9\u9002\u5408\u7684Python\u6a21\u5757\u5b66\u4e60\uff0c\u9996\u5148\u9700\u8981\u6839\u636e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u5174\u8da3\u8fdb\u884c\u5224\u65ad\u3002\u5982\u679c\u4f60\u60f3\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u8003\u8651\u5b66\u4e60NumPy\u548cPandas\u6a21\u5757\uff1b\u5982\u679c\u4f60\u5bf9\u7f51\u7edc\u5f00\u53d1\u611f\u5174\u8da3\uff0cFlask\u548cDjango\u662f\u5f88\u597d\u7684\u9009\u62e9\u3002\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4e0d\u59a8\u5148\u4e86\u89e3\u6bcf\u4e2a\u6a21\u5757\u7684\u529f\u80fd\u548c\u5e94\u7528\u573a\u666f\uff0c\u518d\u51b3\u5b9a\u5b66\u4e60\u7684\u4f18\u5148\u7ea7\u3002<\/p>\n<p><strong>\u5728\u5b66\u4e60Python\u6a21\u5757\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5b66\u4e60\u8d44\u6e90\u63a8\u8350\uff1f<\/strong><br \/>\u5b66\u4e60Python\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9\u591a\u79cd\u8d44\u6e90\u3002\u5b98\u65b9\u6587\u6863\u662f\u6700\u6743\u5a01\u7684\u8d44\u6599\uff0c\u63d0\u4f9b\u8be6\u7ec6\u7684\u6a21\u5757\u4f7f\u7528\u8bf4\u660e\u548c\u793a\u4f8b\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u5728\u7ebf\u8bfe\u7a0b\u5e73\u53f0\u5982Coursera\u3001Udemy\u7b49\u63d0\u4f9b\u7cfb\u7edf\u7684\u5b66\u4e60\u8def\u5f84\u3002YouTube\u4e0a\u7684\u6559\u7a0b\u89c6\u9891\u4e5f\u975e\u5e38\u76f4\u89c2\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u590d\u6742\u7684\u6982\u5ff5\u3002\u53c2\u4e0e\u5f00\u6e90\u9879\u76ee\u548c\u793e\u533a\u8ba8\u8bba\u4e5f\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5b9e\u8df5\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u52a0\u6df1\u5bf9\u6a21\u5757\u529f\u80fd\u7684\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b66\u4f1aPython\u6a21\u5757\u529f\u80fd\uff0c\u4f60\u9700\u8981\uff1a\u4e86\u89e3Python\u6a21\u5757\u7684\u57fa\u7840\u6982\u5ff5\u3001\u638c\u63e1\u6a21\u5757\u7684\u5bfc\u5165\u4e0e\u4f7f\u7528\u65b9\u6cd5\u3001\u719f\u6089\u5e38\u7528\u7684\u6807\u51c6\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1017462,"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\/1017438"}],"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=1017438"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017438\/revisions"}],"predecessor-version":[{"id":1017463,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017438\/revisions\/1017463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1017462"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1017438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1017438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1017438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}