{"id":1152371,"date":"2025-01-13T17:24:57","date_gmt":"2025-01-13T09:24:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152371.html"},"modified":"2025-01-13T17:25:00","modified_gmt":"2025-01-13T09:25:00","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%a8%a1%e5%9d%97%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152371.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u6a21\u5757\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182404\/d1079eea-8563-4ee3-9767-d2d24033b6d7.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u6a21\u5757\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u663e\u793a\u6a21\u5757\u4ee3\u7801\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5916\u90e8\u5de5\u5177\u3002\u5177\u4f53\u65b9\u6cd5\u6709import\u5bfc\u5165\u6a21\u5757\u3001\u4f7f\u7528inspect\u6a21\u5757\u3001\u901a\u8fc7help()\u51fd\u6570\u3001\u67e5\u770b\u6e90\u6587\u4ef6\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001IMPORT\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>import<\/code>\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u53ef\u4ee5\u67e5\u770b\u6a21\u5757\u4e2d\u5305\u542b\u7684\u6240\u6709\u5bf9\u8c61\uff08\u5305\u62ec\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u7b49\uff09\u3002\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u800c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u4e86\u89e3\u6a21\u5757\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(dir(math))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230<code>math<\/code>\u6a21\u5757\u4e2d\u6240\u6709\u53ef\u7528\u7684\u51fd\u6570\u548c\u5e38\u91cf\u540d\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u663e\u793a\u8fd9\u4e9b\u5bf9\u8c61\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001INSPECT\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>inspect<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u83b7\u53d6\u6709\u5173\u52a8\u6001\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u663e\u793a\u6a21\u5757\u3001\u51fd\u6570\u3001\u7c7b\u7b49\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import math<\/p>\n<p>print(inspect.getsource(math))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>inspect.getsource()<\/code>\u53ea\u80fd\u5bf9Python\u7f16\u5199\u7684\u6a21\u5757\u6709\u6548\uff0c\u5bf9\u4e8e\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u5185\u7f6e\u6a21\u5757\u5219\u65e0\u6cd5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001HELP()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>help()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u6a21\u5757\u3001\u51fd\u6570\u3001\u7c7b\u3001\u65b9\u6cd5\u7b49\u7684\u5e2e\u52a9\u4fe1\u606f\u3002\u901a\u8fc7<code>help()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u6a21\u5757\u7684\u7b80\u8981\u63cf\u8ff0\u3001\u51fd\u6570\u7b7e\u540d\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>help(math)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5feb\u901f\u83b7\u53d6\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u7b80\u8981\u8bf4\u660e\uff0c\u4f46\u4e0d\u4f1a\u63d0\u4f9b\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u67e5\u770b\u6e90\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u81ea\u5df1\u7f16\u5199\u7684\u6a21\u5757\u6216\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u76f4\u63a5\u67e5\u770b\u6e90\u6587\u4ef6\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u6587\u672c\u7f16\u8f91\u5668\u6216\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6253\u5f00\u6a21\u5757\u7684\u6e90\u6587\u4ef6\uff0c\u67e5\u770b\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5b89\u88c5\u4e86\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u627e\u5230\u6a21\u5757\u7684\u8def\u5f84\uff0c\u7136\u540e\u6253\u5f00\u6e90\u6587\u4ef6\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import some_module<\/p>\n<p>print(os.path.dirname(some_module.__file__))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5229\u7528IDE\u548c\u5f00\u53d1\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u67e5\u770b\u6a21\u5757\u4ee3\u7801\u7684\u529f\u80fd\u3002\u53f3\u952e\u70b9\u51fb\u6a21\u5757\u540d\uff0c\u7136\u540e\u9009\u62e9\u201c\u8f6c\u5230\u5b9a\u4e49\u201d\u6216\u7c7b\u4f3c\u9009\u9879\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u6a21\u5757\u7684\u5b9e\u73b0\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u901a\u8fc7\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5b98\u65b9\u6587\u6863\u901a\u5e38\u4f1a\u63d0\u4f9b\u6a21\u5757\u7684\u8be6\u7ec6\u8bf4\u660e\u548c\u4f7f\u7528\u4f8b\u5b50\uff0c\u6709\u4e9b\u8fd8\u4f1a\u63d0\u4f9b\u90e8\u5206\u5b9e\u73b0\u4ee3\u7801\u3002\u67e5\u770b\u5b98\u65b9\u6587\u6863\u662f\u4e00\u79cd\u83b7\u53d6\u6a21\u5757\u4fe1\u606f\u7684\u6709\u6548\u9014\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001REPL\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728REPL\uff08Read-Eval-Print Loop\uff09\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u52a8\u6001\u5bfc\u5165\u6a21\u5757\u5e76\u67e5\u770b\u5176\u5185\u5bb9\u3002\u4f7f\u7528<code>dir()<\/code>\u548c<code>help()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u6a21\u5757\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">&gt;&gt;&gt; import math<\/p>\n<p>&gt;&gt;&gt; dir(math)<\/p>\n<p>&gt;&gt;&gt; help(math)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93\uff0c\u5982<code>IPython<\/code>\u3001<code>Jupyter Notebook<\/code>\u7b49\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u529f\u80fd\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u67e5\u770b\u6a21\u5757\u4ee3\u7801\u548c\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><p>\u5728<code>IPython<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>?<\/code>\u548c<code>??<\/code>\u67e5\u770b\u6a21\u5757\u548c\u51fd\u6570\u7684\u6587\u6863\u548c\u6e90\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">In [1]: import math<\/p>\n<p>In [2]: math.sin?<\/p>\n<p>In [3]: math.sin??<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u53cd\u7f16\u8bd1\u5b57\u8282\u7801<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6ca1\u6709\u6e90\u4ee3\u7801\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u53cd\u7f16\u8bd1\u5de5\u5177\uff0c\u5982<code>uncompyle6<\/code>\uff0c\u5c06\u5b57\u8282\u7801\u53cd\u7f16\u8bd1\u56de\u6e90\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7279\u6b8a\u573a\u666f\uff0c\u4f46\u53cd\u7f16\u8bd1\u7684\u4ee3\u7801\u53ef\u80fd\u4e0d\u5b8c\u5168\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h4>\u4f8b\u5b50\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install uncompyle6<\/p>\n<p>uncompyle6 -o . some_module.pyc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u67e5\u770bPython\u6a21\u5757\u7684\u4ee3\u7801\u548c\u6587\u6863\u3002<strong>\u4f7f\u7528import\u5bfc\u5165\u6a21\u5757\u3001inspect\u6a21\u5757\u3001help()\u51fd\u6570\u3001\u67e5\u770b\u6e90\u6587\u4ef6\u7b49\u65b9\u6cd5\uff0c\u90fd\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5229\u7528Python\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u4e86\u89e3\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u52a0\u9ad8\u6548\u5730\u5f00\u53d1\u548c\u8c03\u8bd5Python\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u5df2\u5b89\u88c5\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u5df2\u5b89\u88c5\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u3002\u53ea\u9700\u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528<code>inspect.getsource()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u60a8\u60f3\u8981\u67e5\u770b\u7684\u51fd\u6570\u6216\u7c7b\u3002\u4f8b\u5982\uff0c<code>inspect.getsource(module_name)<\/code>\u5c06\u663e\u793a\u8be5\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u3002\u786e\u4fdd\u60a8\u6709\u76f8\u5e94\u7684\u6743\u9650\uff0c\u4e14\u6a21\u5757\u662f\u4ee5\u53ef\u8bbf\u95ee\u7684\u65b9\u5f0f\u5b89\u88c5\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\u6216IDE\u6765\u8f7b\u677e\u67e5\u770bPython\u6a21\u5757\u7684\u4ee3\u7801\uff1f<\/strong><br \/>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u548c\u6587\u672c\u7f16\u8f91\u5668\u63d0\u4f9b\u4e86\u67e5\u770b\u6a21\u5757\u6e90\u4ee3\u7801\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cPyCharm\u548cVS Code\u5141\u8bb8\u60a8\u901a\u8fc7\u70b9\u51fb\u51fd\u6570\u6216\u7c7b\u540d\u76f4\u63a5\u8df3\u8f6c\u5230\u5176\u5b9a\u4e49\u3002\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528Jupyter Notebook\uff0c\u8f93\u5165<code>?<\/code>\u6216<code>??<\/code>\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u6e90\u4ee3\u7801\u6216\u6587\u6863\u3002<\/p>\n<p><strong>\u5982\u679c\u6a21\u5757\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5982\u4f55\u67e5\u770b\u5176\u6e90\u4ee3\u7801\uff1f<\/strong><br \/>\u5bf9\u4e8e\u7528C\u8bed\u8a00\u7f16\u5199\u7684Python\u6a21\u5757\uff0c\u6e90\u4ee3\u7801\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5728Python\u73af\u5883\u4e2d\u663e\u793a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u67e5\u627e\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u5e93\uff0c\u901a\u5e38\u53ef\u4ee5\u5728GitHub\u6216\u76f8\u5e94\u7684\u9879\u76ee\u7f51\u7ad9\u4e0a\u627e\u5230\u3002\u4e0b\u8f7d\u6e90\u4ee3\u7801\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u6253\u5f00\u5e76\u67e5\u770b\u5176\u4e2d\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u627e\u67d0\u4e2a\u6a21\u5757\u7684\u6587\u6863\u548c\u4f7f\u7528\u793a\u4f8b\uff1f<\/strong><br \/>\u6bcf\u4e2aPython\u6a21\u5757\u901a\u5e38\u90fd\u6709\u5b98\u65b9\u6587\u6863\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u6a21\u5757\u7684\u5b98\u65b9\u7f51\u7ad9\u6216\u5728Python Package Index (PyPI)\u4e0a\u627e\u5230\u76f8\u5173\u4fe1\u606f\u3002\u6587\u6863\u901a\u5e38\u5305\u542b\u5b89\u88c5\u8bf4\u660e\u3001API\u53c2\u8003\u53ca\u4f7f\u7528\u793a\u4f8b\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8be5\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u663e\u793a\u6a21\u5757\u4ee3\u7801\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5916\u90e8\u5de5\u5177\u3002\u5177\u4f53\u65b9\u6cd5\u6709import\u5bfc\u5165\u6a21\u5757\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1152378,"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\/1152371"}],"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=1152371"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152371\/revisions"}],"predecessor-version":[{"id":1152379,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152371\/revisions\/1152379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152378"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}