{"id":925685,"date":"2024-12-26T15:35:20","date_gmt":"2024-12-26T07:35:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/925685.html"},"modified":"2024-12-26T15:35:23","modified_gmt":"2024-12-26T07:35:23","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/925685.html","title":{"rendered":"python\u5982\u4f55\u5f15\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062637\/7cffffb4-c98c-44a6-9f06-d0c003e6dadc.webp\" alt=\"python\u5982\u4f55\u5f15\u5165\" \/><\/p>\n<p><p> <strong>Python\u5f15\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528as\u91cd\u547d\u540d\u6a21\u5757\u3001\u4f7f\u7528importlib\u52a8\u6001\u5bfc\u5165\u7b49\u3002<\/strong>\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001IMPORT\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5f15\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u3002\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u5c06\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u4ece\u800c\u8bbf\u95ee\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u7684\u57fa\u672c\u5f62\u5f0f\u662f<code>import module_name<\/code>\uff0c\u5176\u4e2d<code>module_name<\/code>\u662f\u8981\u5bfc\u5165\u7684\u6a21\u5757\u7684\u540d\u79f0\u3002\u5bfc\u5165\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>module_name.attribute<\/code>\u7684\u5f62\u5f0f\u6765\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.pi)  # \u8f93\u51fa\u5706\u5468\u7387<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u591a\u4e2a\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u4e00\u6761<code>import<\/code>\u8bed\u53e5\u4e2d\u5bfc\u5165\u591a\u4e2a\u6a21\u5757\uff0c\u6a21\u5757\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os, sys<\/p>\n<p>print(os.name)<\/p>\n<p>print(sys.version)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001FROM&#8230;IMPORT\u8bed\u53e5<\/p>\n<\/p>\n<p><p><code>from...import<\/code>\u8bed\u53e5\u5141\u8bb8\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u7684\u5c5e\u6027\uff08\u5982\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u7b49\uff09\uff0c\u4ece\u800c\u65e0\u9700\u6bcf\u6b21\u8bbf\u95ee\u65f6\u90fd\u6307\u5b9a\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u7279\u5b9a\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>from module_name import attribute_name<\/code>\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import pi<\/p>\n<p>print(pi)  # \u76f4\u63a5\u4f7f\u7528pi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u591a\u4e2a\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u4e00\u6761<code>from...import<\/code>\u8bed\u53e5\u4e2d\u5bfc\u5165\u591a\u4e2a\u5c5e\u6027\uff0c\u5c5e\u6027\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sin, cos<\/p>\n<p>print(sin(0))<\/p>\n<p>print(cos(0))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001AS\u91cd\u547d\u540d\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u5bf9\u5bfc\u5165\u7684\u6a21\u5757\u6216\u5c5e\u6027\u8fdb\u884c\u91cd\u547d\u540d\u3002\u8fd9\u5728\u6a21\u5757\u540d\u6216\u5c5e\u6027\u540d\u8fc7\u957f\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u91cd\u547d\u540d\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import module_name as new_name<\/code>\u53ef\u4ee5\u4e3a\u6a21\u5757\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u547d\u540d\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>from module_name import attribute_name as new_name<\/code>\u53ef\u4ee5\u91cd\u547d\u540d\u5bfc\u5165\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import pi as circle_constant<\/p>\n<p>print(circle_constant)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001IMPORTLIB\u52a8\u6001\u5bfc\u5165<\/p>\n<\/p>\n<p><p><code>importlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u7684\u52a8\u6001\u5bfc\u5165\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u6a21\u5757\u540d\u79f0\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>importlib.import_module(module_name)<\/code>\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><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))  # \u8f93\u51fa4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u5bfc\u5165\u7684\u4f18\u52bf<\/strong><\/p>\n<\/p>\n<p><p>\u52a8\u6001\u5bfc\u5165\u7684\u4e00\u4e2a\u4f18\u52bf\u662f\u53ef\u4ee5\u6839\u636e\u7a0b\u5e8f\u903b\u8f91\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u529f\u80fd\u6269\u5c55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u52a0\u8f7d\u7279\u5b9a\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = &quot;os&quot;<\/p>\n<p>os_module = importlib.import_module(user_input)<\/p>\n<p>print(os_module.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001PYTHONPATH\u548c\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u65f6\uff0cPython\u4f1a\u6839\u636e\u4e00\u7cfb\u5217\u641c\u7d22\u8def\u5f84\u6765\u67e5\u627e\u6a21\u5757\u3002\u8fd9\u4e9b\u8def\u5f84\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5f53\u524d\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>Python\u9996\u5148\u4f1a\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u67e5\u627e\u6a21\u5757\u3002\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5219\u7ee7\u7eed\u67e5\u627e\u5176\u4ed6\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>PYTHONPATH\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p><code>PYTHONPATH<\/code>\u662f\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u8def\u5f84\uff08\u4ee5\u7cfb\u7edf\u5206\u9694\u7b26\u5206\u9694\uff09\u3002\u8fd9\u4e9b\u8def\u5f84\u4f1a\u88ab\u6dfb\u52a0\u5230\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6807\u51c6\u5e93\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u76ee\u5f55\u5305\u542b\u4e86\u8bb8\u591a\u5185\u7f6e\u6a21\u5757\uff0cPython\u4f1a\u81ea\u52a8\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u67e5\u627e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>site-packages\u76ee\u5f55<\/strong><\/p>\n<\/p>\n<p><p><code>site-packages<\/code>\u76ee\u5f55\u662f\u7b2c\u4e09\u65b9\u6a21\u5757\u7684\u9ed8\u8ba4\u5b89\u88c5\u4f4d\u7f6e\u3002\u901a\u8fc7\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\uff08\u5982pip\uff09\u5b89\u88c5\u7684\u6a21\u5757\u901a\u5e38\u4f1a\u653e\u5728\u8fd9\u91cc\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>sys.path<\/code>\u6765\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<p><p>\u516d\u3001\u521b\u5efa\u548c\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5bfc\u5165Python\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u6a21\u5757\u5916\uff0c\u8fd8\u53ef\u4ee5\u521b\u5efa\u548c\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u7f16\u5199\u9700\u8981\u7684\u4ee3\u7801\uff0c\u7136\u540e\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>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><strong>\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import my_module<\/code>\u6765\u5bfc\u5165\u81ea\u5b9a\u4e49\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<\/ol>\n<p><p>\u4e03\u3001PACKAGE\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5305\uff08Package\uff09\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\u3002\u5305\u7684\u5f15\u5165\u4f7f\u5f97\u6a21\u5757\u7684\u7ec4\u7ec7\u66f4\u52a0\u7075\u6d3b\u548c\u5c42\u6b21\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u5305\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u5e76\u4e14\u76ee\u5f55\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>__init__.py<\/code>\u7684\u6587\u4ef6\uff0c\u901a\u5e38\u8fd9\u4e2a\u6587\u4ef6\u662f\u7a7a\u7684\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5305\u542b\u5305\u7684\u4e00\u4e9b\u521d\u59cb\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_package<\/code>\u7684\u5305\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><strong>\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>import package_name.module_name<\/code>\u7684\u5f62\u5f0f\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_package.module1<\/p>\n<h2><strong>\u4f7f\u7528module1\u4e2d\u7684\u529f\u80fd<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from my_package import module2<\/p>\n<h2><strong>\u4f7f\u7528module2\u4e2d\u7684\u529f\u80fd<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u6a21\u5757\u7684\u91cd\u8f7d\u548c\u91cd\u65b0\u5bfc\u5165<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757\u8fdb\u884c\u4fee\u6539\u5e76\u91cd\u65b0\u52a0\u8f7d\u3002Python\u63d0\u4f9b\u4e86<code>importlib.reload()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6a21\u5757\u7684\u91cd\u8f7d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u91cd\u8f7d\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>importlib.reload(module)<\/code>\u53ef\u4ee5\u91cd\u65b0\u52a0\u8f7d\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import my_module<\/p>\n<p>import importlib<\/p>\n<h2><strong>\u4fee\u6539my_module\u4e2d\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>importlib.reload(my_module)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u91cd\u8f7d\u6a21\u5757\u65f6\uff0c\u6a21\u5757\u4e2d\u7684\u5168\u5c40\u72b6\u6001\u53ef\u80fd\u4f1a\u88ab\u91cd\u7f6e\uff0c\u56e0\u6b64\u9700\u8981\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9bPython\u5f15\u5165\u6a21\u5757\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ec4\u7ec7\u4ee3\u7801\u3001\u91cd\u7528\u73b0\u6709\u529f\u80fd\uff0c\u5e76\u6839\u636e\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\uff0c\u4ece\u800c\u589e\u5f3aPython\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5f15\u5165\u6a21\u5757\u3002\u6bd4\u5982\uff0c\u60f3\u8981\u5f15\u5165\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import math<\/code>\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u8bbf\u95ee<code>math<\/code>\u6a21\u5757\u4e2d\u7684\u5404\u79cd\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\u4e86\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>from<\/code>\u8bed\u53e5\u76f4\u63a5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u4f8b\u5982<code>from math import sqrt<\/code>\uff0c\u8fd9\u5c06\u76f4\u63a5\u5bfc\u5165<code>sqrt<\/code>\u51fd\u6570\uff0c\u65b9\u4fbf\u8c03\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ba1\u7406\u548c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\u6765\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5e93\u3002\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install &lt;\u5e93\u540d&gt;<\/code>\uff0c\u5c31\u53ef\u4ee5\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import &lt;\u5e93\u540d&gt;<\/code>\u5728\u4ee3\u7801\u4e2d\u5f15\u5165\uff0c\u5e76\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u529f\u80fd\u3002\u5728\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528<code>requirements.txt<\/code>\u6587\u4ef6\u8bb0\u5f55\u6240\u9700\u7684\u5e93\u53ca\u5176\u7248\u672c\uff0c\u65b9\u4fbf\u56e2\u961f\u534f\u4f5c\u548c\u90e8\u7f72\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5f15\u5165\u672c\u5730\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5f15\u5165\u672c\u5730\u7684Python\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6216\u7edd\u5bf9\u8def\u5f84\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\uff0c\u4f8b\u5982<code>import my_module<\/code>\uff0c\u5176\u4e2d<code>my_module.py<\/code>\u662f\u4f60\u7684\u672c\u5730\u6587\u4ef6\u540d\u3002\u786e\u4fdd\u8be5\u6587\u4ef6\u4e0e\u4e3b\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5728Python\u8def\u5f84\u4e2d\u3002\u5982\u679c\u6587\u4ef6\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path.append()<\/code>\u6765\u6dfb\u52a0\u8def\u5f84\uff0c\u786e\u4fddPython\u80fd\u591f\u627e\u5230\u9700\u8981\u5f15\u5165\u7684\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f15\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528as\u91cd [&hellip;]","protected":false},"author":3,"featured_media":925689,"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\/925685"}],"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=925685"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/925685\/revisions"}],"predecessor-version":[{"id":925690,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/925685\/revisions\/925690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/925689"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=925685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=925685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=925685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}