{"id":988646,"date":"2024-12-27T08:04:10","date_gmt":"2024-12-27T00:04:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988646.html"},"modified":"2024-12-27T08:04:13","modified_gmt":"2024-12-27T00:04:13","slug":"python%e5%a6%82%e4%bd%95%e5%8a%a0%e8%bd%bd%e6%a8%a1%e5%9d%97%e5%ae%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/988646.html","title":{"rendered":"python\u5982\u4f55\u52a0\u8f7d\u6a21\u5757\u5b9d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064215\/76431862-2bca-4e68-9a63-f45b50effa89.webp\" alt=\"python\u5982\u4f55\u52a0\u8f7d\u6a21\u5757\u5b9d\" \/><\/p>\n<p><p> <strong>\u8981\u52a0\u8f7dPython\u6a21\u5757\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3001<code>from ... import<\/code>\u8bed\u53e5\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56\u3001\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u7b2c\u4e09\u65b9\u5305\u3002<\/strong>\u5728Python\u4e2d\uff0c\u6a21\u5757\u548c\u5305\u662f\u4ee3\u7801\u7ec4\u7ec7\u7684\u57fa\u672c\u5355\u4f4d\uff0c\u4e86\u89e3\u5982\u4f55\u52a0\u8f7d\u548c\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u5305\u662f\u8fdb\u884cPython\u7f16\u7a0b\u7684\u57fa\u7840\u4e4b\u4e00\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u4e0d\u540c\u65b9\u5f0f\u52a0\u8f7d\u6a21\u5757\u5305\uff0c\u5e76\u4e14\u7ba1\u7406\u4f60\u7684Python\u9879\u76ee\u6240\u9700\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u52a0\u8f7d\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>import<\/code>\u8bed\u53e5\u662f\u52a0\u8f7dPython\u6a21\u5757\u6700\u57fa\u7840\u4e5f\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>import<\/code>\u53ef\u4ee5\u5c06\u6a21\u5757\u4e2d\u7684\u6240\u6709\u529f\u80fd\u5f15\u5165\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>math<\/code>\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u52a0\u8f7d\u4e86<code>math<\/code>\u6a21\u5757\uff0c\u4e4b\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5706\u5468\u7387\u7684\u4f59\u5f26\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cosine_value = math.cos(math.pi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u52a0\u8f7d\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6a21\u5757\u540d\u8fdb\u884c\u8c03\u7528\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>from ... import<\/code>\u52a0\u8f7d\u7279\u5b9a\u529f\u80fd<\/p>\n<\/p>\n<p><p><code>from ... import<\/code>\u8bed\u53e5\u5141\u8bb8\u4ece\u6a21\u5757\u4e2d\u52a0\u8f7d\u7279\u5b9a\u7684\u529f\u80fd\uff08\u5982\u51fd\u6570\u3001\u7c7b\u6216\u53d8\u91cf\uff09\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u5047\u8bbe\u6211\u4eec\u53ea\u9700\u8981<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>cos<\/code>\u51fd\u6570\u548c<code>pi<\/code>\u5e38\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import cos, pi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>cos<\/code>\u548c<code>pi<\/code>\u800c\u65e0\u9700\u6307\u5b9a\u6a21\u5757\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cosine_value = cos(pi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u4f46\u8981\u6ce8\u610f\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u7a7a\u95f4\u6c61\u67d3\uff0c\u5c24\u5176\u662f\u5728\u6709\u591a\u4e2a\u6a21\u5757\u65f6\uff0c\u5bb9\u6613\u5f15\u8d77\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406\u4f9d\u8d56<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1Python\u9879\u76ee\u65f6\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\uff08Virtual Environment\uff09\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u5b9e\u8df5\uff0c\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u4e4b\u95f4\u7684\u4f9d\u8d56\u3002Python\u81ea\u5e26\u7684<code>venv<\/code>\u6a21\u5757\u53ef\u4ee5\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a\uff1a\n<pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728Unix\u6216MacOS\u4e0a\uff1a\n<pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install module_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5b8c\u6bd5\u540e\uff0c\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u865a\u62df\u73af\u5883\u80fd\u591f\u786e\u4fdd\u5728\u4e0d\u540c\u9879\u76ee\u4e2d\u4f7f\u7528\u4e0d\u540c\u7248\u672c\u7684\u5e93\u800c\u4e0d\u4f1a\u76f8\u4e92\u5e72\u6270\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5305<\/p>\n<\/p>\n<p><p><code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002\u4f7f\u7528<code>pip<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u5230Python\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u786e\u4fdd<code>pip<\/code>\u5df2\u5b89\u88c5\uff0c\u5e76\u5347\u7ea7\u5230\u6700\u65b0\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5378\u8f7d\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5217\u51fa\u5df2\u5b89\u88c5\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>pip<\/code>\u53ef\u4ee5\u8f7b\u677e\u5730\u5b89\u88c5\u548c\u7ba1\u7406Python\u7684\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u8fd9\u5bf9\u4e8e\u5f00\u53d1\u73b0\u4ee3Python\u5e94\u7528\u975e\u5e38\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6a21\u5757\u7684\u8def\u5f84\u548c\u641c\u7d22<\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u52a0\u8f7d\u6a21\u5757\u65f6\uff0cPython\u89e3\u91ca\u5668\u4f1a\u5728\u4e00\u7cfb\u5217\u76ee\u5f55\u4e2d\u641c\u7d22\u6a21\u5757\u6587\u4ef6\u3002\u641c\u7d22\u8def\u5f84\u5b58\u50a8\u5728<code>sys<\/code>\u6a21\u5757\u7684<code>sys.path<\/code>\u53d8\u91cf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u8fd9\u4e2a\u53d8\u91cf\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u67e5\u770b\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sys.path.append(&#39;\/path\/to\/my\/modules&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\uff0c\u53ef\u4ee5\u8ba9Python\u89e3\u91ca\u5668\u8bc6\u522b\u548c\u52a0\u8f7d\u81ea\u5b9a\u4e49\u8def\u5f84\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u521b\u5efa\u4e0e\u52a0\u8f7d<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u6a21\u5757\uff0cPython\u8fd8\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u81ea\u5df1\u7684\u6a21\u5757\u3002\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u5c06\u76f8\u5173\u7684\u529f\u80fd\u5c01\u88c5\u5230\u6a21\u5757\u4e2d\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2aPython\u6a21\u5757\u6587\u4ef6\uff1a<\/p>\n<p>\u5047\u8bbe\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u52a0\u8f7d\u8be5\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<p>print(my_module.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>from ... import<\/code>\u52a0\u8f7d\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_module import greet<\/p>\n<p>print(greet(&quot;Bob&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u52a0\u8f7d\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u4ece\u800c\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5305\u7684\u521b\u5efa\u4e0e\u52a0\u8f7d<\/p>\n<\/p>\n<p><p>\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u5e76\u4e14\u5fc5\u987b\u5305\u542b\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff08\u53ef\u4ee5\u662f\u7a7a\u6587\u4ef6\uff09\uff0c\u7528\u4e8e\u6807\u8bc6\u8be5\u76ee\u5f55\u662f\u4e00\u4e2aPython\u5305\u3002\u5305\u7684\u4f7f\u7528\u4f7f\u5f97\u6a21\u5757\u7684\u5c42\u6b21\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u5305\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>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<\/li>\n<li>\n<p>\u5728<code>module1.py<\/code>\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function1():<\/p>\n<p>    return &quot;Function 1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728<code>module2.py<\/code>\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function2():<\/p>\n<p>    return &quot;Function 2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u52a0\u8f7d\u5305\u4e2d\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package import module1, module2<\/p>\n<p>print(module1.function1())<\/p>\n<p>print(module2.function2())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5305\u7684\u4f7f\u7528\u80fd\u591f\u66f4\u597d\u5730\u7ec4\u7ec7\u5927\u578b\u9879\u76ee\u4e2d\u7684\u6a21\u5757\uff0c\u4f7f\u5f97\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u52a8\u6001\u52a0\u8f7d\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u72b6\u6001\u52a8\u6001\u5730\u52a0\u8f7d\u6a21\u5757\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>importlib<\/code>\u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>importlib<\/code>\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\uff1a\n<pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &quot;math&quot;<\/p>\n<p>math_module = importlib.import_module(module_name)<\/p>\n<p>print(math_module.sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u52a0\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u6839\u636e\u7528\u6237\u8f93\u5165\u6216\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u4e0d\u540c\u6a21\u5757\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u52a0\u8f7d\u6a21\u5757\u548c\u5305\u662f\u8fdb\u884c\u6709\u6548\u7f16\u7a0b\u7684\u57fa\u7840\u3002\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u548c<code>from ... import<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u8f7d\u548c\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u529f\u80fd\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u548c<code>pip<\/code>\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\u3002\u540c\u65f6\uff0c\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u548c\u5305\uff0c\u5e76\u5229\u7528\u52a8\u6001\u52a0\u8f7d\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u66f4\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u6280\u5de7\u7684\u638c\u63e1\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u9a7e\u9a6dPython\u8bed\u8a00\uff0c\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u6e38\u5203\u6709\u4f59\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\u52a0\u8f7d\u81ea\u5b9a\u4e49\u6a21\u5757\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\uff08\u4ee5.py\u4e3a\u6269\u5c55\u540d\uff09\u4e0e\u60a8\u7684\u4e3b\u7a0b\u5e8f\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5c06\u6a21\u5757\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u8def\u5f84\u4e2d\u3002\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u8f7b\u677e\u5f15\u5165\u6a21\u5757\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import my_module\n<\/code><\/pre>\n<p>\u5982\u679c\u60a8\u53ea\u60f3\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">from my_module import my_function\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6a21\u5757\u53ef\u4ee5\u52a0\u8f7d\uff1f<\/strong><br \/>Python\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u8bb8\u591a\u5e38\u7528\u6a21\u5757\uff0c\u5982<code>os<\/code>\u3001<code>sys<\/code>\u3001<code>math<\/code>\u3001<code>datetime<\/code>\u7b49\u3002\u6bcf\u4e2a\u6a21\u5757\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6570\u5b66\u76f8\u5173\u7684\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u800c<code>os<\/code>\u6a21\u5757\u5219\u7528\u4e8e\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u8fd9\u4e9b\u6a21\u5757\uff0c\u5e76\u5229\u7528\u5176\u529f\u80fd\u6765\u7b80\u5316\u60a8\u7684\u7f16\u7a0b\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6a21\u5757\u52a0\u8f7d\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u52a0\u8f7d\u6a21\u5757\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u9519\u8bef\uff0c\u4f8b\u5982\u6a21\u5757\u672a\u627e\u5230\u6216\u5bfc\u5165\u5931\u8d25\u3002\u4e3a\u4e86\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    import my_module\nexcept ImportError:\n    print(&quot;\u6a21\u5757\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6a21\u5757\u540d\u79f0\u6216\u8def\u5f84\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u89e3\u51b3\u52a0\u8f7d\u6a21\u5757\u65f6\u9047\u5230\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u52a0\u8f7dPython\u6a21\u5757\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u3001from &#8230; import\u8bed\u53e5\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56 [&hellip;]","protected":false},"author":3,"featured_media":988662,"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\/988646"}],"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=988646"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988646\/revisions"}],"predecessor-version":[{"id":988664,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988646\/revisions\/988664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988662"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}