{"id":1185356,"date":"2025-01-15T19:39:04","date_gmt":"2025-01-15T11:39:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185356.html"},"modified":"2025-01-15T19:39:14","modified_gmt":"2025-01-15T11:39:14","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%a8%a1%e5%9d%97%e7%9a%84%e5%90%8d%e7%a7%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185356.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134522\/2fa28270-fd39-40b2-9762-f81000cccebc.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\uff1a\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u3002<\/strong> \u5728\u8fd9\u4e4b\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u5c5e\u6027<code>__name__<\/code>\uff0c\u5b83\u8868\u793a\u6a21\u5757\u7684\u540d\u79f0\u3002\u5982\u679c\u8be5\u6a21\u5757\u662f\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u542f\u52a8\u7684\uff0c\u5219<code>__name__<\/code>\u7684\u503c\u4e3a<code>&quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;<\/code>\uff1b\u5982\u679c\u8be5\u6a21\u5757\u662f\u88ab\u5bfc\u5165\u7684\uff0c\u5219<code>__name__<\/code>\u7684\u503c\u4e3a\u6a21\u5757\u7684\u5b9e\u9645\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u6a21\u5757\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a<code>__name__<\/code>\u7684\u5c5e\u6027\uff0c\u5b83\u7528\u4e8e\u6307\u793a\u6a21\u5757\u7684\u540d\u79f0\u3002\u5f53\u4e00\u4e2a\u6a21\u5757\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a<code>__main__<\/code>\uff1b\u5f53\u8be5\u6a21\u5757\u88ab\u5bfc\u5165\u5230\u5176\u4ed6\u6a21\u5757\u4e2d\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a\u6a21\u5757\u7684\u5b9e\u9645\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>print(&quot;Module name:&quot;, __name__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Module name: __main__<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165<code>mymodule<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5e76\u8fd0\u884c<code>main.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Module name: mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u83b7\u53d6\u6709\u5173\u6a21\u5757\u3001\u7c7b\u3001\u65b9\u6cd5\u3001\u51fd\u6570\u3001\u56de\u6eaf\u5bf9\u8c61\u7b49\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528<code>inspect.getmodule<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def get_module_name(obj):<\/p>\n<p>    module = inspect.getmodule(obj)<\/p>\n<p>    return module.__name__ if module else None<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>import math<\/p>\n<p>print(get_module_name(math))  # Output: math<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>sys<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>sys<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u5bfc\u5165\u7684\u6240\u6709\u6a21\u5757\u3002<code>sys.modules<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u5305\u542b\u4e86\u5f53\u524d\u6240\u6709\u5bfc\u5165\u7684\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u8fd9\u4e2a\u5b57\u5178\u6765\u627e\u5230\u6a21\u5757\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def get_module_name(obj):<\/p>\n<p>    for name, module in sys.modules.items():<\/p>\n<p>        if module is obj:<\/p>\n<p>            return name<\/p>\n<p>    return None<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>import math<\/p>\n<p>print(get_module_name(math))  # Output: math<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u4e86\u89e3\u5f53\u524d\u6a21\u5757\u7684\u540d\u79f0\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u7684\u6267\u884c\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u53ef\u4ee5\u5728\u65e5\u5fd7\u4e2d\u8bb0\u5f55\u54ea\u4e2a\u6a21\u5757\u4ea7\u751f\u4e86\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logger = logging.getLogger(__name__)<\/p>\n<p>def some_function():<\/p>\n<p>    logger.info(&quot;This is a log message from some_function&quot;)<\/p>\n<p>some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6a21\u5757\u540d\u79f0\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>module_name = &quot;math&quot;<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<p>print(module.sqrt(16))  # Output: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u4e86\u89e3\u6a21\u5757\u7684\u540d\u79f0\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7ec4\u7ec7\u548c\u7ba1\u7406\u6d4b\u8bd5\u7528\u4f8b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u6a21\u5757\u540d\u79f0\u52a8\u6001\u52a0\u8f7d\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class TestMathModule(unittest.TestCase):<\/p>\n<p>    def test_sqrt(self):<\/p>\n<p>        import math<\/p>\n<p>        self.assertEqual(math.sqrt(16), 4)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6df1\u5165\u7406\u89e3\u6a21\u5757\u7684\u5c5e\u6027<\/h3>\n<\/p>\n<p><h4>1\u3001\u6a21\u5757\u7684<code>__file__<\/code>\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>__name__<\/code>\u5c5e\u6027\uff0c\u6a21\u5757\u8fd8\u6709\u4e00\u4e2a<code>__file__<\/code>\u5c5e\u6027\uff0c\u5b83\u8868\u793a\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002\u8fd9\u5728\u8c03\u8bd5\u548c\u5206\u6790\u4ee3\u7801\u65f6\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.__file__)  # Output: \/usr\/lib\/python3.8\/lib-dynload\/math.cpython-38-x86_64-linux-gnu.so<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u5757\u7684<code>__package__<\/code>\u5c5e\u6027<\/h4>\n<\/p>\n<p><p><code>__package__<\/code>\u5c5e\u6027\u8868\u793a\u6a21\u5757\u6240\u5c5e\u7684\u5305\u3002\u5bf9\u4e8e\u9876\u7ea7\u6a21\u5757\uff0c\u8be5\u5c5e\u6027\u7684\u503c\u4e3a<code>None<\/code>\u3002\u5728\u5305\u5185\u7684\u6a21\u5757\u4e2d\uff0c<code>__package__<\/code>\u5c5e\u6027\u8868\u793a\u5305\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># In a package structure like this:<\/p>\n<h2><strong>mypackage\/<\/strong><\/h2>\n<h2><strong>\u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 submodule.py<\/strong><\/h2>\n<h2><strong>submodule.py<\/strong><\/h2>\n<p>print(__package__)  # Output: mypackage<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6a21\u5757\u7684<code>__doc__<\/code>\u5c5e\u6027<\/h4>\n<\/p>\n<p><p><code>__doc__<\/code>\u5c5e\u6027\u8868\u793a\u6a21\u5757\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u6587\u6863\u6216\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.__doc__)  # Output: the docstring of the math module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u7684\u751f\u547d\u5468\u671f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6a21\u5757\u7684\u5bfc\u5165\u548c\u521d\u59cb\u5316<\/h4>\n<\/p>\n<p><p>\u5f53\u4e00\u4e2a\u6a21\u5757\u7b2c\u4e00\u6b21\u88ab\u5bfc\u5165\u65f6\uff0cPython\u4f1a\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u7f13\u5b58<\/strong>\uff1a\u68c0\u67e5<code>sys.modules<\/code>\u5b57\u5178\u4e2d\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u8be5\u6a21\u5757\u3002\u5982\u679c\u5b58\u5728\uff0c\u76f4\u63a5\u8fd4\u56de\u8be5\u6a21\u5757\u3002<\/li>\n<li><strong>\u67e5\u627e\u6a21\u5757<\/strong>\uff1a\u5982\u679c\u6a21\u5757\u4e0d\u5728\u7f13\u5b58\u4e2d\uff0cPython\u4f1a\u6839\u636e\u6a21\u5757\u7684\u540d\u79f0\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67e5\u627e\u76f8\u5e94\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u52a0\u8f7d\u6a21\u5757<\/strong>\uff1a\u627e\u5230\u6a21\u5757\u6587\u4ef6\u540e\uff0cPython\u4f1a\u52a0\u8f7d\u8be5\u6a21\u5757\u7684\u4ee3\u7801\u5e76\u6267\u884c\u3002<\/li>\n<li><strong>\u7f13\u5b58\u6a21\u5757<\/strong>\uff1a\u6267\u884c\u5b8c\u6210\u540e\uff0cPython\u4f1a\u5c06\u6a21\u5757\u6dfb\u52a0\u5230<code>sys.modules<\/code>\u5b57\u5178\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5bfc\u5165\u65f6\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u3002<\/li>\n<\/ol>\n<p><h4>2\u3001\u6a21\u5757\u7684\u91cd\u8f7d<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u8f7d\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528<code>importlib.reload<\/code>\u51fd\u6570\u6765\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>import mymodule<\/p>\n<h2><strong>Modify mymodule.py file...<\/strong><\/h2>\n<p>importlib.reload(mymodule)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6a21\u5757\u7684\u7ec4\u7ec7\u548c\u547d\u540d<\/h3>\n<\/p>\n<p><h4>1\u3001\u6a21\u5757\u7684\u547d\u540d\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u9075\u5faaPEP 8\u7684\u547d\u540d\u89c4\u8303\uff0c\u6a21\u5757\u7684\u540d\u79f0\u5e94\u8be5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Good module names<\/p>\n<p>mymodule.py<\/p>\n<p>my_module.py<\/p>\n<h2><strong>Bad module names<\/strong><\/h2>\n<p>MyModule.py<\/p>\n<p>myModule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u5757\u7684\u7ec4\u7ec7\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u591a\u4e2a\u6a21\u5757\u548c\u5305\u662f\u4e00\u4e2a\u597d\u7684\u5b9e\u8df5\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">myproject\/<\/p>\n<p>\u251c\u2500\u2500 __init__.py<\/p>\n<p>\u251c\u2500\u2500 module1.py<\/p>\n<p>\u251c\u2500\u2500 module2.py<\/p>\n<p>\u2514\u2500\u2500 subpackage\/<\/p>\n<p>    \u251c\u2500\u2500 __init__.py<\/p>\n<p>    \u2514\u2500\u2500 submodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>__init__.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5bfc\u5165\u5305\u5185\u7684\u6a21\u5757\uff0c\u4ee5\u4fbf\u5916\u90e8\u4ee3\u7801\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528\u5305\u4e2d\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># __init__.py<\/p>\n<p>from .module1 import some_function<\/p>\n<p>from .module2 import another_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u3001<code>inspect<\/code>\u6a21\u5757\u548c<code>sys<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6Python\u6a21\u5757\u7684\u540d\u79f0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u8c03\u8bd5\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u548c\u5355\u5143\u6d4b\u8bd5\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u7406\u89e3\u6a21\u5757\u7684\u5c5e\u6027\u548c\u751f\u547d\u5468\u671f\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u9075\u5faa\u6a21\u5757\u7684\u547d\u540d\u89c4\u8303\u548c\u7ec4\u7ec7\u7ed3\u6784\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a8\u6001\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>__name__<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\u3002\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code>\u7684\u503c\u4e3a<code>__main__<\/code>\uff1b\u5982\u679c\u6a21\u5757\u88ab\u5bfc\u5165\uff0c\u5219<code>__name__<\/code>\u7684\u503c\u4e3a\u6a21\u5757\u7684\u540d\u79f0\u3002\u4f7f\u7528\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">print(__name__)\n<\/code><\/pre>\n<p>\u5982\u679c\u8fd9\u4e2a\u4ee3\u7801\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u6267\u884c\uff0c\u8f93\u51fa\u5c06\u662f\u6a21\u5757\u7684\u540d\u79f0\u3002\u5982\u679c\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u6267\u884c\uff0c\u8f93\u51fa\u5c06\u662f<code>__main__<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u6a21\u5757\u7684\u540d\u79f0\u662f\u5426\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>sys.modules<\/code>\u6765\u68c0\u67e5\u5f53\u524d\u5df2\u52a0\u8f7d\u6a21\u5757\u7684\u540d\u79f0\u3002\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u5df2\u52a0\u8f7d\u6a21\u5757\u7684\u5b57\u5178\uff0c\u5e76\u4e14\u53ef\u4ee5\u904d\u5386\u8fd9\u4e9b\u6a21\u5757\u4ee5\u67e5\u627e\u7279\u5b9a\u540d\u79f0\u7684\u6a21\u5757\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\n\nfor module_name in sys.modules.keys():\n    if &quot;my_module&quot; in module_name:\n        print(module_name)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5305\u542b\u201cmy_module\u201d\u5b57\u6837\u7684\u6a21\u5757\u540d\u79f0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u6267\u884c\u6a21\u5757\u7684\u5b8c\u6574\u8def\u5f84\uff1f<\/strong><br \/>\u5229\u7528<code>__file__<\/code>\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6a21\u5757\u7684\u5b8c\u6574\u8def\u5f84\u3002\u8fd9\u4e2a\u5c5e\u6027\u8fd4\u56de\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5305\u62ec\u6587\u4ef6\u540d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nmodule_path = os.path.abspath(__file__)\nprint(module_path)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5f53\u524d\u6a21\u5757\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u65b9\u4fbf\u5f00\u53d1\u8005\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6587\u4ef6\u64cd\u4f5c\u6216\u8def\u5f84\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\uff1a\u4f7f\u7528__name__\u5c5e\u6027\u3001\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528sys [&hellip;]","protected":false},"author":3,"featured_media":1185373,"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\/1185356"}],"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=1185356"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185356\/revisions"}],"predecessor-version":[{"id":1185374,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185356\/revisions\/1185374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}