{"id":955771,"date":"2024-12-27T02:21:53","date_gmt":"2024-12-26T18:21:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/955771.html"},"modified":"2024-12-27T02:21:56","modified_gmt":"2024-12-26T18:21:56","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e5%8c%85%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/955771.html","title":{"rendered":"python\u5982\u4f55\u627e\u5230\u5305\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095624\/52477b6e-8c70-4507-9282-958462c33ddc.webp\" alt=\"python\u5982\u4f55\u627e\u5230\u5305\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u627e\u5230\u5305\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>pip show<\/code>\u547d\u4ee4\u3001\u901a\u8fc7<code>sys.path<\/code>\u67e5\u770b\u8def\u5f84\u3001\u4f7f\u7528<code>importlib.util.find_spec<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>pip show<\/code>\u547d\u4ee4\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u5305\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u9002\u7528\u4e8e\u5df2\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\uff1b\u901a\u8fc7<code>sys.path<\/code>\u67e5\u770b\u8def\u5f84\u53ef\u4ee5\u4e86\u89e3Python\u89e3\u91ca\u5668\u5bfb\u627e\u6a21\u5757\u7684\u8def\u5f84\u5217\u8868\uff1b\u4f7f\u7528<code>importlib.util.find_spec<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u67e5\u627e\u5305\u7684\u4f4d\u7f6e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PIP SHOW\u547d\u4ee4<\/p>\n<\/p>\n<p><p><code>pip<\/code>\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406Python\u5305\u7684\u5b89\u88c5\u3001\u66f4\u65b0\u548c\u5378\u8f7d\u3002<code>pip show<\/code>\u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5305\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u5305\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4<code>pip show package_name<\/code>\u53ef\u4ee5\u67e5\u770b\u6307\u5b9a\u5305\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec\u5305\u7684\u5b89\u88c5\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u5c06\u8f93\u51fa<code>numpy<\/code>\u5305\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec<code>Location<\/code>\u5b57\u6bb5\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u6307\u793a\u4e86\u5305\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4f4d\u5305\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e86\u5b89\u88c5\u8def\u5f84\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u8def\u5f84\u5b9a\u4f4d\u5230\u5177\u4f53\u7684\u5305\u6587\u4ef6\u5939\u4e2d\uff0c\u67e5\u770b\u5305\u7684\u5177\u4f53\u6587\u4ef6\u7ed3\u6784\u3002\u8fd9\u5728\u9700\u8981\u624b\u52a8\u4fee\u6539\u6216\u8c03\u8bd5\u5305\u7684\u6e90\u7801\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u901a\u8fc7SYS.PATH\u67e5\u770b\u8def\u5f84<\/p>\n<\/p>\n<p><p>Python\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u4f1a\u5728<code>sys.path<\/code>\u4e2d\u5217\u51fa\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b<code>sys.path<\/code>\u7684\u5185\u5bb9\u6765\u5224\u65adPython\u4f1a\u5728\u54ea\u4e9b\u76ee\u5f55\u4e2d\u5bfb\u627e\u5305\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770bsys.path<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7684\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u67e5\u770b<code>sys.path<\/code>\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>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u51faPython\u5f53\u524d\u4f1a\u641c\u7d22\u7684\u76ee\u5f55\u3002\u901a\u5e38\u5305\u62ec\u5f53\u524d\u76ee\u5f55\u3001\u6807\u51c6\u5e93\u76ee\u5f55\u548c\u7b2c\u4e09\u65b9\u5305\u76ee\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8ba9Python\u5728\u5176\u4ed6\u76ee\u5f55\u4e2d\u5bfb\u627e\u5305\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u4fee\u6539<code>sys.path<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/your\/package&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u4e5f\u4f1a\u5728\u65b0\u589e\u7684\u76ee\u5f55\u4e2d\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528IMPORTLIB.UTIL.FIND_SPEC\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>importlib.util.find_spec<\/code>\u662fPython 3.4\u5f15\u5165\u7684\u4e00\u79cd\u67e5\u627e\u6a21\u5757\u89c4\u8303\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u6a21\u5757\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u627e\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>find_spec<\/code>\u65b9\u6cd5\u53ef\u4ee5\u67e5\u627e\u6a21\u5757\u7684\u4f4d\u7f6e\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.util<\/p>\n<p>spec = importlib.util.find_spec(&#39;numpy&#39;)<\/p>\n<p>print(spec.origin)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u6a21\u5757\u7684\u6e90\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a8\u6001\u67e5\u627e<\/strong><\/p>\n<\/p>\n<p><p><code>find_spec<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u67e5\u627e\u5df2\u7ecf\u5b89\u88c5\u7684\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u67e5\u627e\u672a\u5b89\u88c5\u4f46\u5728\u67d0\u4e9b\u8def\u5f84\u4e2d\u5b58\u5728\u7684\u6a21\u5757\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u67e5\u627e\u5e76\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p><code>PYTHONPATH<\/code>\u662f\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\uff0c\u7528\u4e8e\u6307\u5b9aPython\u89e3\u91ca\u5668\u641c\u7d22\u6a21\u5757\u7684\u8def\u5f84\u3002\u901a\u8fc7\u8bbe\u7f6e<code>PYTHONPATH<\/code>\uff0c\u53ef\u4ee5\u8ba9Python\u5728\u6307\u5b9a\u76ee\u5f55\u4e2d\u67e5\u627e\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6ePYTHONPATH<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u8bbe\u7f6e<code>PYTHONPATH<\/code>\uff0c\u4f8b\u5982\u5728Linux\u6216macOS\u4e0a\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=\/path\/to\/your\/package<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e0a\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">set PYTHONPATH=C:\\path\\to\\your\\package<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u540e\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u901a\u8fc7\u67e5\u770b<code>sys.path<\/code>\u9a8c\u8bc1\u662f\u5426\u751f\u6548\u3002<code>PYTHONPATH<\/code>\u4e2d\u7684\u8def\u5f84\u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u5230<code>sys.path<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5229\u7528IDE\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684IDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u901a\u5e38\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5e2e\u52a9\u5f00\u53d1\u8005\u67e5\u627e\u548c\u7ba1\u7406\u5305\u6587\u4ef6\u3002\u4f8b\u5982\uff0cPyCharm\u3001VSCode\u7b49IDE\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u63d2\u4ef6\u76f4\u63a5\u67e5\u770b\u548c\u5b9a\u4f4d\u5305\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528PyCharm<\/strong><\/p>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9879\u76ee\u7ed3\u6784\u89c6\u56fe\u76f4\u63a5\u67e5\u770b\u9879\u76ee\u4e2d\u7684\u6240\u6709\u5305\u6587\u4ef6\u3002\u6b64\u5916\uff0cPyCharm\u7684\u201cNavigate to File\u201d\u529f\u80fd\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u5230\u67d0\u4e2a\u5305\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528VSCode<\/strong><\/p>\n<\/p>\n<p><p>VSCode\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63d2\u4ef6\u652f\u6301\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5b89\u88c5Python\u63d2\u4ef6\u6765\u589e\u5f3aPython\u5f00\u53d1\u4f53\u9a8c\u3002\u901a\u8fc7\u201cExplorer\u201d\u9762\u677f\uff0c\u53ef\u4ee5\u67e5\u770b\u5de5\u4f5c\u533a\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u627e\u5230\u5305\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u5305\u67e5\u627e\uff0c\u4f7f\u7528<code>pip show<\/code>\u548c<code>sys.path<\/code>\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\uff1b\u5bf9\u4e8e\u52a8\u6001\u67e5\u627e\u548c\u9ad8\u7ea7\u5e94\u7528\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>importlib.util.find_spec<\/code>\u548c\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u3002\u5229\u7528IDE\u7684\u529f\u80fd\u4e5f\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406Python\u5305\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u627e\u5230\u5df2\u5b89\u88c5\u7684\u5305\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u67e5\u627e\u5df2\u5b89\u88c5\u7684\u5305\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pip<\/code>\u5de5\u5177\u3002\u8fd0\u884c\u547d\u4ee4<code>pip show \u5305\u540d<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u5305\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u5b89\u88c5\u8def\u5f84\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u8f93\u5165\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0c\u60a8\u5c06\u80fd\u83b7\u53d6\u5230\u8be5\u5305\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770bPython\u5305\u7684\u6e90\u4ee3\u7801\uff1f<\/strong><br \/>\u5728\u627e\u5230\u5305\u7684\u5b89\u88c5\u8def\u5f84\u540e\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5bfc\u822a\u5230\u8be5\u76ee\u5f55\u67e5\u770b\u6e90\u4ee3\u7801\u3002\u901a\u5e38\uff0cPython\u5305\u7684\u6e90\u4ee3\u7801\u4f1a\u4fdd\u5b58\u5728\u5176\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684<code>site-packages<\/code>\u6587\u4ef6\u5939\u5185\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216IDE\u6253\u5f00\u8fd9\u4e9b\u6587\u4ef6\u8fdb\u884c\u67e5\u770b\u548c\u4fee\u6539\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3\u627e\u4e0d\u5230Python\u5305\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5728\u5bfc\u5165\u5305\u65f6\u9047\u5230\u201c\u627e\u4e0d\u5230\u5305\u201d\u7684\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a\u786e\u8ba4\u5305\u662f\u5426\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u68c0\u67e5\uff1b\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u8bbe\u7f6e\u6b63\u786e\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528\u865a\u62df\u73af\u5883\u65f6\uff1b\u5982\u679c\u5305\u5b89\u88c5\u5728\u7cfb\u7edf\u7684Python\u7248\u672c\u4e2d\u800c\u60a8\u5728\u4f7f\u7528\u5176\u4ed6\u7248\u672c\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u65b0\u5b89\u88c5\u5305\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u627e\u5230\u5305\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528pip show\u547d\u4ee4\u3001\u901a\u8fc7sys.path\u67e5\u770b\u8def\u5f84\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":955775,"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\/955771"}],"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=955771"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955771\/revisions"}],"predecessor-version":[{"id":955776,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955771\/revisions\/955776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/955775"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=955771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=955771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=955771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}