{"id":1183682,"date":"2025-01-15T19:17:25","date_gmt":"2025-01-15T11:17:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183682.html"},"modified":"2025-01-15T19:17:28","modified_gmt":"2025-01-15T11:17:28","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e5%86%85%e7%bd%ae-py%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183682.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u5185\u7f6e.py\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133355\/909e95e5-c99d-44fb-a5c4-bba2d2d86f78.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u5185\u7f6e.py\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u67e5\u770bPython\u5185\u7f6e.py\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u8fc7Python\u5b89\u88c5\u76ee\u5f55\u67e5\u627e\u3001\u4f7f\u7528Python\u6807\u51c6\u5e93\u6a21\u5757inspect\u3001\u5229\u7528IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u3002<\/strong>\u5176\u4e2d\uff0c\u5229\u7528IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u662f\u6700\u76f4\u89c2\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002IDE\uff08\u5982PyCharm\u3001VSCode\uff09\u548c\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Sublime Text\u3001Notepad++\uff09\u901a\u5e38\u5177\u5907\u5f3a\u5927\u7684\u6587\u4ef6\u641c\u7d22\u548c\u4ee3\u7801\u5bfc\u822a\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u6253\u5f00\u548c\u67e5\u770bPython\u5185\u7f6e\u7684.py\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u5229\u7528IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u67e5\u770bPython\u5185\u7f6e.py\u6587\u4ef6<\/strong>\u3002\u9996\u5148\uff0c\u6253\u5f00\u4f60\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\uff0c\u7136\u540e\u627e\u5230Python\u7684\u5b89\u88c5\u76ee\u5f55\uff0c\u901a\u5e38\u4f4d\u4e8e<code>C:\\PythonXX\\Lib<\/code>\uff08Windows\uff09\u6216<code>\/usr\/local\/lib\/pythonX.X<\/code>\uff08macOS\/Linux\uff09\u3002\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7684\u6807\u51c6\u5e93\u6a21\u5757\u548c\u5305\u3002\u901a\u8fc7\u641c\u7d22\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u7279\u5b9a\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u5e76\u6253\u5f00\u76f8\u5e94\u7684.py\u6587\u4ef6\u8fdb\u884c\u67e5\u770b\u548c\u7f16\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u901a\u8fc7Python\u5b89\u88c5\u76ee\u5f55\u67e5\u627e<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u627e\u5230Python\u7684\u5b89\u88c5\u76ee\u5f55<\/p>\n<p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0cPython\u901a\u5e38\u5b89\u88c5\u5728<code>C:\\PythonXX<\/code>\u76ee\u5f55\u4e0b\uff0c\u5176\u4e2dXX\u4ee3\u8868Python\u7248\u672c\u53f7\u3002\u5728macOS\u548cLinux\u7cfb\u7edf\u4e0a\uff0cPython\u901a\u5e38\u5b89\u88c5\u5728<code>\/usr\/local\/lib\/pythonX.X<\/code>\u76ee\u5f55\u4e0b\uff0c\u5176\u4e2dX.X\u4ee3\u8868Python\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u67e5\u627e\u5185\u7f6e\u6a21\u5757<\/p>\n<p>\u5728\u4e0a\u8ff0\u76ee\u5f55\u4e0b\uff0c\u8fdb\u5165<code>Lib<\/code>\u6587\u4ef6\u5939\uff0c\u4f60\u4f1a\u770b\u5230\u5f88\u591a.py\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u8fd9\u4e9b\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u5c31\u662fPython\u7684\u6807\u51c6\u5e93\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u540d\u6765\u627e\u5230\u4f60\u611f\u5174\u8da3\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u4f8b\u5982<code>os.py<\/code>\u3001<code>sys.py<\/code>\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u6253\u5f00\u5e76\u67e5\u770b<\/p>\n<p>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982Notepad++\u3001Sublime Text\uff09\u6216IDE\uff08\u5982PyCharm\u3001VSCode\uff09\u6253\u5f00\u8fd9\u4e9b.py\u6587\u4ef6\uff0c\u4f60\u5c31\u53ef\u4ee5\u67e5\u770b\u548c\u7f16\u8f91\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Python\u6807\u51c6\u5e93\u6a21\u5757inspect<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5inspect\u6a21\u5757<\/p>\n<p>inspect\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u627e\u6a21\u5757\u8def\u5f84<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528inspect\u6a21\u5757\u6765\u67e5\u627e\u4efb\u4f55\u5df2\u52a0\u8f7d\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import os<\/p>\n<p>module_path = inspect.getfile(os)<\/p>\n<p>print(module_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6253\u5f00\u5e76\u67e5\u770b<\/p>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u4f1a\u5f97\u5230os\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u8def\u5f84\uff0c\u4ece\u800c\u67e5\u770bos\u6a21\u5757\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528IDE\u6216\u6587\u672c\u7f16\u8f91\u5668<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00IDE\u6216\u6587\u672c\u7f16\u8f91\u5668<\/p>\n<p>\u4f7f\u7528\u4f60\u559c\u6b22\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\uff0c\u5982PyCharm\u3001VSCode\u3001Sublime Text\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u914d\u7f6ePython\u89e3\u91ca\u5668<\/p>\n<p>\u5728IDE\u4e2d\u914d\u7f6ePython\u89e3\u91ca\u5668\u8def\u5f84\uff0c\u4f7f\u5176\u6307\u5411\u4f60\u5b89\u88c5\u7684Python\u7248\u672c\u3002\u8fd9\u4e00\u6b65\u901a\u5e38\u5728IDE\u7684\u8bbe\u7f6e\u6216\u9996\u9009\u9879\u4e2d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u5165\u5e76\u5bfc\u822a\u5230\u6a21\u5757<\/p>\n<p>\u901a\u8fc7IDE\u7684\u5bfc\u822a\u529f\u80fd\uff08\u5982\u67e5\u627e\u5b9a\u4e49\u3001\u8df3\u8f6c\u5230\u6e90\u4ee3\u7801\uff09\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u5e76\u6253\u5f00\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u7801\u3002\u4f8b\u5982\uff0c\u5728PyCharm\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6309\u4f4fCtrl\u952e\u5e76\u70b9\u51fb\u6a21\u5757\u540d\uff0cIDE\u4f1a\u81ea\u52a8\u6253\u5f00\u8be5\u6a21\u5757\u7684\u6e90\u7801\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u67e5\u627e<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528Python\u547d\u4ee4\u884c\u5de5\u5177<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528Python\u547d\u4ee4\u884c\u5de5\u5177\u67e5\u627e\u5185\u7f6e\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -c &quot;import os; print(os.__file__)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u6253\u5f00\u5e76\u67e5\u770b<\/p>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\uff0c\u4f60\u4f1a\u5f97\u5230os\u6a21\u5757\u7684\u6587\u4ef6\u8def\u5f84\u3002\u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u8def\u5f84\uff0c\u4ece\u800c\u67e5\u770bos\u6a21\u5757\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5229\u7528\u5728\u7ebf\u8d44\u6e90<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5728\u7ebf\u4ee3\u7801\u6d4f\u89c8\u5668<\/p>\n<p>\u6709\u4e9b\u5728\u7ebf\u5e73\u53f0\u63d0\u4f9bPython\u6807\u51c6\u5e93\u7684\u6e90\u7801\u6d4f\u89c8\u529f\u80fd\uff0c\u4f8b\u5982GitHub\u4e0a\u7684Python\u5b98\u65b9\u4ed3\u5e93\u3001Python\u5b98\u65b9\u6587\u6863\u7b49\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b\u5728\u7ebf\u5e73\u53f0\u67e5\u770bPython\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u641c\u7d22\u7279\u5b9a\u6a21\u5757<\/p>\n<p>\u5728\u8fd9\u4e9b\u5728\u7ebf\u5e73\u53f0\u4e0a\uff0c\u4f60\u53ef\u4ee5\u641c\u7d22\u7279\u5b9a\u7684\u5185\u7f6e\u6a21\u5757\uff0c\u5e76\u76f4\u63a5\u67e5\u770b\u5176\u6e90\u7801\u3002\u4f8b\u5982\uff0c\u5728GitHub\u4e0a\u641c\u7d22<code>cpython\/Lib\/os.py<\/code>\uff0c\u4f60\u53ef\u4ee5\u627e\u5230os\u6a21\u5757\u7684\u6e90\u7801\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u662f\u67e5\u770bPython\u5185\u7f6e.py\u6587\u4ef6\u7684\u51e0\u79cd\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u67e5\u770b\u548c\u5b66\u4e60\u3002\u901a\u8fc7\u67e5\u770b\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u7801\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3Python\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6c34\u5e73\u548c\u89e3\u51b3\u95ee\u9898\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u627e\u5230Python\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6807\u51c6\u5e93\u3002\u901a\u5e38\uff0c\u5185\u7f6e\u6a21\u5757\u7684\u4f4d\u7f6e\u5728Python\u7684\u5b89\u88c5\u76ee\u5f55\u4e2d\u3002\u4f60\u53ef\u4ee5\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff1a  <\/p>\n<pre><code class=\"language-python\">import sys\nprint(sys.prefix)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u627e\u5230<code>Lib<\/code>\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u4e86\u8bb8\u591a\u5185\u7f6e\u6a21\u5757\u7684<code>.py<\/code>\u6587\u4ef6\u3002\u4f60\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728Python\u7684\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165<code>help(&#39;module_name&#39;)<\/code>\u6765\u4e86\u89e3\u8be5\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\u548c\u6587\u4ef6\u4f4d\u7f6e\u3002<\/p>\n<p><strong>Python\u5185\u7f6e\u6a21\u5757\u7684\u6587\u6863\u5728\u54ea\u91cc\u53ef\u4ee5\u627e\u5230\uff1f<\/strong><br \/>Python\u5b98\u65b9\u6587\u6863\u63d0\u4f9b\u4e86\u5185\u7f6e\u6a21\u5757\u7684\u8be6\u7ec6\u8bf4\u660e\u548c\u4f7f\u7528\u793a\u4f8b\u3002\u4f60\u53ef\u4ee5\u8bbf\u95ee<a href=\"https:\/\/docs.python.org\/3\/library\/index.html\">Python\u5b98\u65b9\u6587\u6863\u7f51\u7ad9<\/a>\u6765\u67e5\u627e\u5177\u4f53\u6a21\u5757\u7684\u6587\u6863\u3002\u8fd9\u4e9b\u6587\u6863\u4e0d\u4ec5\u5305\u542b\u6a21\u5757\u7684\u529f\u80fd\u8bf4\u660e\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4f7f\u7528\u793a\u4f8b\u548c\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u7b54\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u6a21\u5757\u3002<\/p>\n<p><strong>\u67e5\u770bPython\u5185\u7f6e\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u67e5\u770bPython\u5185\u7f6e\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u5408\u9002\u7684\u4ee3\u7801\u7f16\u8f91\u5668\u6216IDE\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6d4f\u89c8\u548c\u641c\u7d22\u4ee3\u7801\u5185\u5bb9\u3002\u8bb8\u591aIDE\uff08\u5982PyCharm\u3001Visual Studio Code\uff09\u5177\u6709\u5185\u7f6e\u7684\u5bfc\u822a\u548c\u641c\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u5230\u51fd\u6570\u548c\u7c7b\u7684\u5b9a\u4e49\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\uff08\u5982Git\uff09\u8ddf\u8e2a\u8fd9\u4e9b\u6587\u4ef6\u7684\u53d8\u5316\u4e5f\u80fd\u5e2e\u52a9\u4f60\u7406\u89e3\u6a21\u5757\u7684\u6f14\u53d8\u548c\u66f4\u65b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u5185\u7f6e.py\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u8fc7Python\u5b89\u88c5\u76ee\u5f55\u67e5\u627e\u3001\u4f7f\u7528Python\u6807\u51c6\u5e93\u6a21\u5757inspec [&hellip;]","protected":false},"author":3,"featured_media":1183694,"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\/1183682"}],"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=1183682"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183682\/revisions"}],"predecessor-version":[{"id":1183698,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183682\/revisions\/1183698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183694"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}