{"id":1034300,"date":"2024-12-31T11:49:10","date_gmt":"2024-12-31T03:49:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1034300.html"},"modified":"2024-12-31T11:49:13","modified_gmt":"2024-12-31T03:49:13","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%85%b6%e4%bb%96%e6%a8%a1%e5%9d%97%e4%b8%ad%e7%9a%84%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1034300.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/022a10b1-a600-4238-abd4-887294f4bcbe.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf<\/strong>\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u5e76\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u53d8\u91cf\u3001\u4f7f\u7528\u6a21\u5757\u7684\u522b\u540d\u6765\u7b80\u5316\u8bbf\u95ee<\/strong>\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u793a\u4f8b\u6765\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001IMPORT\u8bed\u53e5\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u5757\u540d\u8bbf\u95ee\u5176\u4e2d\u7684\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8bbf\u95ee\u6a21\u5757\u4e2d\u591a\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6a21\u5757<code>module_a.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf<code>variable_a<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_a.py<\/p>\n<p>variable_a = 42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165<code>module_a<\/code>\u5e76\u8bbf\u95ee<code>variable_a<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import module_a<\/p>\n<p>print(module_a.variable_a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u5728<code>main.py<\/code>\u4e2d\u4f7f\u7528\u4e86<code>module_a<\/code>\u4e2d\u7684\u53d8\u91cf<code>variable_a<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001FROM&#8230;IMPORT\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u907f\u514d\u5728\u8bbf\u95ee\u53d8\u91cf\u65f6\u91cd\u590d\u4f7f\u7528\u6a21\u5757\u540d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u8fd8\u662f\u4ee5<code>module_a.py<\/code>\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_a.py<\/p>\n<p>variable_a = 42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165<code>variable_a<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module_a import variable_a<\/p>\n<p>print(variable_a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>variable_a<\/code>\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6a21\u5757\u7684\u522b\u540d<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6a21\u5757\u540d\u53ef\u80fd\u6bd4\u8f83\u957f\u6216\u4e0d\u6613\u8bb0\u5fc6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u6a21\u5757\u8d77\u4e00\u4e2a\u7b80\u77ed\u7684\u522b\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6a21\u5757<code>long_module_name.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf<code>variable_long<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># long_module_name.py<\/p>\n<p>variable_long = 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6a21\u5757<code>long_module_name<\/code>\u8d77\u4e00\u4e2a\u522b\u540d<code>lm<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import long_module_name as lm<\/p>\n<p>print(lm.variable_long)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u7b80\u77ed\u7684\u522b\u540d<code>lm<\/code>\u6765\u8bbf\u95ee<code>variable_long<\/code>\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bfc\u5165\u591a\u4e2a\u53d8\u91cf\u6216\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u591a\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\uff0c\u53ef\u4ee5\u5728<code>from...import<\/code>\u8bed\u53e5\u4e2d\u5217\u51fa\u6240\u6709\u9700\u8981\u5bfc\u5165\u7684\u5185\u5bb9\uff0c\u6216\u8005\u4f7f\u7528<code>*<\/code>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u5728<code>module_b.py<\/code>\u4e2d\u5b9a\u4e49\u4e86\u591a\u4e2a\u53d8\u91cf\u548c\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_b.py<\/p>\n<p>variable_b1 = 10<\/p>\n<p>variable_b2 = 20<\/p>\n<p>def function_b():<\/p>\n<p>    return variable_b1 + variable_b2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165\u591a\u4e2a\u53d8\u91cf\u548c\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module_b import variable_b1, variable_b2, function_b<\/p>\n<p>print(variable_b1)<\/p>\n<p>print(variable_b2)<\/p>\n<p>print(function_b())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>*<\/code>\u5bfc\u5165\u6240\u6709\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from module_b import *<\/p>\n<p>print(variable_b1)<\/p>\n<p>print(variable_b2)<\/p>\n<p>print(function_b())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>*<\/code>\u5bfc\u5165\u6240\u6709\u5185\u5bb9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u7279\u522b\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5c3d\u91cf\u660e\u786e\u5217\u51fa\u9700\u8981\u5bfc\u5165\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u8fd0\u884c\u65f6\u7684\u6761\u4ef6\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u6216\u53d8\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u52a8\u6001\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e24\u4e2a\u6a21\u5757<code>module_c1.py<\/code>\u548c<code>module_c2.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_c1.py<\/p>\n<p>variable_c = &quot;Module C1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># module_c2.py<\/p>\n<p>variable_c = &quot;Module C2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u52a8\u6001\u5bfc\u5165\u4e0d\u540c\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>module_name = &quot;module_c1&quot;  # \u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6539\u53d8<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<p>print(module.variable_c)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c<code>main.py<\/code>\u4f1a\u6839\u636e<code>module_name<\/code>\u7684\u503c\u52a8\u6001\u5bfc\u5165\u5bf9\u5e94\u7684\u6a21\u5757\uff0c\u5e76\u8bbf\u95ee\u5176\u4e2d\u7684\u53d8\u91cf<code>variable_c<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528__init__.py\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u5305\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>__init__.py<\/code>\u6587\u4ef6\u6765\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757\uff0c\u5e76\u7edf\u4e00\u5bfc\u5165\u5305\u4e2d\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5305<code>my_package<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e24\u4e2a\u6a21\u5757<code>module_d1.py<\/code>\u548c<code>module_d2.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/module_d1.py<\/p>\n<p>variable_d1 = &quot;Variable D1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/module_d2.py<\/p>\n<p>variable_d2 = &quot;Variable D2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>my_package<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/__init__.py<\/p>\n<p>from .module_d1 import variable_d1<\/p>\n<p>from .module_d2 import variable_d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165<code>my_package<\/code>\u5e76\u8bbf\u95ee\u5176\u4e2d\u7684\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from my_package import variable_d1, variable_d2<\/p>\n<p>print(variable_d1)<\/p>\n<p>print(variable_d2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u5305\u4e2d\u7684\u6a21\u5757\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6a21\u5757\u91cd\u8f7d<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u8fd0\u884c\u65f6\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\uff0c\u4ee5\u4fbf\u53cd\u6620\u6a21\u5757\u6587\u4ef6\u4e2d\u7684\u6700\u65b0\u66f4\u6539\u3002\u53ef\u4ee5\u4f7f\u7528<code>importlib.reload<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6a21\u5757\u91cd\u8f7d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6a21\u5757<code>module_e.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_e.py<\/p>\n<p>variable_e = &quot;Initial Value&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757<code>module_e<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import importlib<\/p>\n<p>import module_e<\/p>\n<p>print(module_e.variable_e)<\/p>\n<h2><strong>\u5047\u8bbe\u6b64\u65f6\u4fee\u6539\u4e86module_e.py\u4e2d\u7684variable_e\u503c<\/strong><\/h2>\n<p>module_e = importlib.reload(module_e)<\/p>\n<p>print(module_e.variable_e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8bbf\u95ee\u7684\u662f\u6a21\u5757\u4e2d\u7684\u6700\u65b0\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u53d8\u91cf\u3001\u4f7f\u7528\u6a21\u5757\u7684\u522b\u540d\u7b80\u5316\u8bbf\u95ee\u3001\u5bfc\u5165\u591a\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528<code>__init__.py<\/code>\u6587\u4ef6\u7ec4\u7ec7\u5305\u3001\u4ee5\u53ca\u6a21\u5757\u91cd\u8f7d\u7b49\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5bfc\u5165\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u548c\u6613\u7ef4\u62a4\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u7684\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>module1.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf<code>my_variable<\/code>\uff0c\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u8fd9\u4e2a\u53d8\u91cf\uff1a<\/p>\n<pre><code class=\"language-python\">from module1 import my_variable\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>my_variable<\/code>\uff0c\u800c\u65e0\u9700\u524d\u7f00\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u6a21\u5757\u53d8\u91cf\u65f6\uff0c\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u7684\u53d8\u91cf\u65f6\uff0c\u9700\u786e\u4fdd\u6a21\u5757\u5df2\u7ecf\u88ab\u6b63\u786e\u5bfc\u5165\u3002\u5982\u679c\u5bfc\u5165\u5931\u8d25\uff0cPython\u4f1a\u629b\u51fa\u4e00\u4e2a<code>ImportError<\/code>\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u53d8\u91cf\u5728\u6a21\u5757\u4e2d\u5df2\u88ab\u5b9a\u4e49\uff0c\u5426\u5219\u4f1a\u51fa\u73b0<code>NameError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u5efa\u8bae\u5728\u5bfc\u5165\u65f6\u4e3a\u53d8\u91cf\u4f7f\u7528\u522b\u540d\uff1a<\/p>\n<pre><code class=\"language-python\">from module1 import my_variable as mv\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u901a\u8fc7<code>mv<\/code>\u6765\u8bbf\u95ee<code>my_variable<\/code>\uff0c\u964d\u4f4e\u4e0e\u5176\u4ed6\u53d8\u91cf\u547d\u540d\u51b2\u7a81\u7684\u98ce\u9669\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u53d8\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import module1\nprint(dir(module1))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5217\u51fa<code>module1<\/code>\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ec\u53d8\u91cf\u3002\u7ed3\u5408<code>help(module1)<\/code>\u53ef\u4ee5\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u6587\u6863\u4fe1\u606f\uff0c\u5e2e\u52a9\u7406\u89e3\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u5176\u4ed6\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u5e76\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165 [&hellip;]","protected":false},"author":3,"featured_media":1034306,"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\/1034300"}],"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=1034300"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034300\/revisions"}],"predecessor-version":[{"id":1034311,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034300\/revisions\/1034311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1034306"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1034300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1034300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1034300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}