{"id":1035904,"date":"2024-12-31T12:02:43","date_gmt":"2024-12-31T04:02:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035904.html"},"modified":"2024-12-31T12:02:45","modified_gmt":"2024-12-31T04:02:45","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8bpython%e6%98%af%e5%90%a6%e6%8c%89%e7%85%a7%e4%ba%86%e6%9f%90%e4%ba%9b%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035904.html","title":{"rendered":"\u5982\u4f55\u68c0\u6d4bpython\u662f\u5426\u6309\u7167\u4e86\u67d0\u4e9b\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/02f4be08-f904-4c04-8a85-0c7570725067.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u68c0\u6d4bpython\u662f\u5426\u6309\u7167\u4e86\u67d0\u4e9b\u5e93\" \/><\/p>\n<p><p> <strong>\u68c0\u6d4bPython\u662f\u5426\u5b89\u88c5\u4e86\u67d0\u4e9b\u5e93\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u811a\u672c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u786e\u5b9a\u6240\u9700\u7684\u5e93\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u6709\u6548\u5730\u68c0\u6d4bPython\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff1a<\/strong><br \/>\u547d\u4ee4\u884c\u5de5\u5177\u662f\u68c0\u6d4bPython\u5e93\u5b89\u88c5\u60c5\u51b5\u7684\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u6765\u68c0\u67e5\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u3002\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u6307\u5b9a\u5e93\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show &lt;\u5e93\u540d&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5e93\u5df2\u5b89\u88c5\uff0c\u547d\u4ee4\u884c\u4f1a\u663e\u793a\u5e93\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u7248\u672c\u53f7\u3001\u4f5c\u8005\u7b49\u3002\u5982\u679c\u5e93\u672a\u5b89\u88c5\uff0c\u5219\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u811a\u672c\uff1a<\/strong><br \/>\u4f60\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\u6765\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff0c\u7528\u4e8e\u68c0\u6d4b\u591a\u4e2a\u5e93\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>libraries = [&#39;numpy&#39;, &#39;pandas&#39;, &#39;scipy&#39;]<\/p>\n<p>for lib in libraries:<\/p>\n<p>    try:<\/p>\n<p>        importlib.import_module(lib)<\/p>\n<p>        print(f&#39;{lib} is installed.&#39;)<\/p>\n<p>    except ImportError:<\/p>\n<p>        print(f&#39;{lib} is not installed.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u4f7f\u7528\u4e86<code>importlib<\/code>\u6a21\u5757\u6765\u5c1d\u8bd5\u5bfc\u5165\u6bcf\u4e2a\u5e93\u3002\u5982\u679c\u5bfc\u5165\u6210\u529f\uff0c\u5219\u8868\u793a\u5e93\u5df2\u5b89\u88c5\uff1b\u5426\u5219\uff0c\u6355\u83b7<code>ImportError<\/code>\u5f02\u5e38\u5e76\u8f93\u51fa\u5e93\u672a\u5b89\u88c5\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff1a<\/strong><br \/>\u8bb8\u591aIDE\uff08\u5982PyCharm\u3001VSCode\uff09\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u5305\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u548c\u7ba1\u7406\u5df2\u5b89\u88c5\u7684\u5e93\u3002\u5728\u8fd9\u4e9bIDE\u4e2d\uff0c\u4f60\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6216\u9879\u76ee\u9762\u677f\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\u5217\u8868\uff0c\u5e76\u641c\u7d22\u7279\u5b9a\u7684\u5e93\u4ee5\u786e\u5b9a\u5176\u5b89\u88c5\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u4f7f\u7528Python\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u811a\u672c\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u7075\u6d3b\u5b9a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u66f4\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4e86\u89e3\u548c\u5e94\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u6d4b\u5355\u4e2a\u5e93\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>def check_library(lib_name):<\/p>\n<p>    try:<\/p>\n<p>        importlib.import_module(lib_name)<\/p>\n<p>        return True<\/p>\n<p>    except ImportError:<\/p>\n<p>        return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u68c0\u6d4b\u591a\u4e2a\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">libraries = [&#39;numpy&#39;, &#39;pandas&#39;, &#39;scipy&#39;, &#39;matplotlib&#39;, &#39;sklearn&#39;]<\/p>\n<p>for lib in libraries:<\/p>\n<p>    if check_library(lib):<\/p>\n<p>        print(f&#39;{lib} is installed.&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;{lib} is not installed.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u811a\u672c\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u6269\u5c55\u548c\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u68c0\u6d4b\u5e93\u7684\u540c\u65f6\u6267\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff08\u5982\u5b89\u88c5\u7f3a\u5931\u7684\u5e93\uff09\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def install_library(lib_name):<\/p>\n<p>    subprocess.check_call([sys.executable, &#39;-m&#39;, &#39;pip&#39;, &#39;install&#39;, lib_name])<\/p>\n<p>for lib in libraries:<\/p>\n<p>    if check_library(lib):<\/p>\n<p>        print(f&#39;{lib} is installed.&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;{lib} is not installed. Installing...&#39;)<\/p>\n<p>        install_library(lib)<\/p>\n<p>        print(f&#39;{lib} installation complete.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u68c0\u6d4b\u5e93\u7684\u540c\u65f6\u81ea\u52a8\u5b89\u88c5\u7f3a\u5931\u7684\u5e93\uff0c\u63d0\u9ad8\u811a\u672c\u7684\u81ea\u52a8\u5316\u7a0b\u5ea6\u548c\u5b9e\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528pip\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u51e0\u4e4e\u6240\u6709\u7684Python\u73af\u5883\u90fd\u652f\u6301pip\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u6d4b\u7279\u5b9a\u5e93\u662f\u5426\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show &lt;\u5e93\u540d&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8981\u68c0\u6d4b\u662f\u5426\u5b89\u88c5\u4e86numpy\u5e93\uff0c\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5e93\u5df2\u5b89\u88c5\uff0c\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: numpy<\/p>\n<p>Version: 1.21.2<\/p>\n<p>Summary: NumPy is the fundamental package for array computing with Python.<\/p>\n<p>Home-page: https:\/\/www.numpy.org<\/p>\n<p>Author: Travis E. Oliphant et al.<\/p>\n<p>Author-em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l: numpy-discussion@python.org<\/p>\n<p>License: BSD<\/p>\n<p>Location: \/usr\/local\/lib\/python3.9\/site-packages<\/p>\n<p>Requires: <\/p>\n<p>Required-by: scipy, pandas, matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5e93\u672a\u5b89\u88c5\uff0c\u5219\u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528pip list\u547d\u4ee4<\/strong><\/p>\n<\/p>\n<p><p>pip list\u547d\u4ee4\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u8f93\u51fa\u7ed3\u679c\u6765\u67e5\u627e\u7279\u5b9a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list | grep &lt;\u5e93\u540d&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8981\u67e5\u627e\u662f\u5426\u5b89\u88c5\u4e86pandas\u5e93\uff0c\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list | grep pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5e93\u5df2\u5b89\u88c5\uff0c\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code>pandas    1.3.3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7Python\u811a\u672c\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u811a\u672c\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u66f4\u52a0\u7075\u6d3b\u548c\u81ea\u52a8\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u68c0\u6d4b\u5355\u4e2a\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u5c1d\u8bd5\u5bfc\u5165\u7279\u5b9a\u5e93\uff0c\u5e76\u6355\u83b7\u5f02\u5e38\u6765\u5224\u65ad\u5e93\u662f\u5426\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p>def check_library(lib_name):<\/p>\n<p>    try:<\/p>\n<p>        importlib.import_module(lib_name)<\/p>\n<p>        print(f&#39;{lib_name} is installed.&#39;)<\/p>\n<p>    except ImportError:<\/p>\n<p>        print(f&#39;{lib_name} is not installed.&#39;)<\/p>\n<p>check_library(&#39;numpy&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u68c0\u6d4b\u591a\u4e2a\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6269\u5c55\u4e0a\u8ff0\u811a\u672c\uff0c\u68c0\u6d4b\u591a\u4e2a\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">libraries = [&#39;numpy&#39;, &#39;pandas&#39;, &#39;scipy&#39;]<\/p>\n<p>for lib in libraries:<\/p>\n<p>    check_library(lib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u81ea\u52a8\u5b89\u88c5\u7f3a\u5931\u7684\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u68c0\u6d4b\u5e93\u7684\u540c\u65f6\uff0c\u81ea\u52a8\u5b89\u88c5\u7f3a\u5931\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import sys<\/p>\n<p>def install_library(lib_name):<\/p>\n<p>    subprocess.check_call([sys.executable, &#39;-m&#39;, &#39;pip&#39;, &#39;install&#39;, lib_name])<\/p>\n<p>libraries = [&#39;numpy&#39;, &#39;pandas&#39;, &#39;scipy&#39;]<\/p>\n<p>for lib in libraries:<\/p>\n<p>    if check_library(lib):<\/p>\n<p>        print(f&#39;{lib} is installed.&#39;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&#39;{lib} is not installed. Installing...&#39;)<\/p>\n<p>        install_library(lib)<\/p>\n<p>        print(f&#39;{lib} installation complete.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u5305\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u548c\u7ba1\u7406\u5df2\u5b89\u88c5\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528IDE\u7684\u64cd\u4f5c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>1. PyCharm<\/strong><\/p>\n<\/p>\n<p><p>PyCharm\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python IDE\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5305\u7ba1\u7406\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00PyCharm\u9879\u76ee\u3002<\/li>\n<li>\u5728\u5de6\u4fa7\u7684\u9879\u76ee\u89c6\u56fe\u4e2d\uff0c\u627e\u5230\u5e76\u70b9\u51fb\u201cExternal Libraries\u201d\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u5305\u7ba1\u7406\u5668\u7a97\u53e3\u4e2d\uff0c\u641c\u7d22\u5e76\u67e5\u770b\u7279\u5b9a\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><p><strong>2. Visual Studio Code (VSCode)<\/strong><\/p>\n<\/p>\n<p><p>VSCode\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python IDE\uff0c\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u5305\u7ba1\u7406\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00VSCode\u9879\u76ee\u3002<\/li>\n<li>\u5728\u5de6\u4fa7\u6d3b\u52a8\u680f\u4e2d\uff0c\u70b9\u51fb\u201c\u6269\u5c55\u201d\u56fe\u6807\u3002<\/li>\n<li>\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u201cPython\u201d\uff0c\u627e\u5230\u5e76\u5b89\u88c5Python\u6269\u5c55\u3002<\/li>\n<li>\u5728VSCode\u7ec8\u7aef\u4e2d\uff0c\u8fd0\u884c<code>pip list<\/code>\u547d\u4ee4\uff0c\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><p><strong>3. Jupyter Notebook<\/strong><\/p>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u4ea4\u4e92\u5f0f\u5f00\u53d1\u73af\u5883\uff0c\u7279\u522b\u9002\u5408\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00Jupyter Notebook\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Notebook\u6587\u4ef6\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u5355\u5143\u4e2d\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">!pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e93\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u5728\u5176\u4e2d\u67e5\u627e\u7279\u5b9a\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u68c0\u6d4b\u5e93\u7684\u5b89\u88c5\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662fPython\u5f00\u53d1\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\uff0c\u80fd\u591f\u4e3a\u6bcf\u4e2a\u9879\u76ee\u63d0\u4f9b\u72ec\u7acb\u7684\u5305\u548c\u4f9d\u8d56\u7ba1\u7406\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u51b2\u7a81\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>myenv<\/code>\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u4e0a\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728macOS\u548cLinux\u4e0a\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u4f60\u7684\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u5b89\u88c5\u548c\u68c0\u6d4b\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b89\u88c5\u548c\u68c0\u6d4b\u5e93\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p>pip show numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u9000\u51fa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9000\u51fa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">deactivate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u68c0\u6d4bPython\u662f\u5426\u5b89\u88c5\u4e86\u67d0\u4e9b\u5e93\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001Python\u811a\u672c\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4ee5\u53ca\u865a\u62df\u73af\u5883\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u547d\u4ee4\u884c\u5de5\u5177\uff1a<\/strong> \u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5feb\u901f\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><p><strong>Python\u811a\u672c\uff1a<\/strong> \u7075\u6d3b\u53ef\u5b9a\u5236\uff0c\u9002\u5408\u81ea\u52a8\u5316\u68c0\u6d4b\u548c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p><strong>\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff1a<\/strong> \u754c\u9762\u53cb\u597d\uff0c\u9002\u5408\u67e5\u770b\u548c\u7ba1\u7406\u5df2\u5b89\u88c5\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>\u865a\u62df\u73af\u5883\uff1a<\/strong> \u63d0\u4f9b\u72ec\u7acb\u7684\u5305\u548c\u4f9d\u8d56\u7ba1\u7406\uff0c\u9002\u5408\u9879\u76ee\u9694\u79bb\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u68c0\u6d4bPython\u5e93\u7684\u5b89\u88c5\u60c5\u51b5\uff0c\u786e\u4fdd\u5f00\u53d1\u73af\u5883\u7684\u7a33\u5b9a\u6027\u548c\u4e00\u81f4\u6027\u3002\u65e0\u8bba\u4f60\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u5c06\u5bf9\u4f60\u7684Python\u5f00\u53d1\u5de5\u4f5c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u7684\u5e93\u5217\u8868\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u7684\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>pip list<\/code>\uff0c\u8fd9\u5c06\u663e\u793a\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5305\u53ca\u5176\u7248\u672c\u4fe1\u606f\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pip freeze<\/code>\u547d\u4ee4\uff0c\u8fd9\u5c06\u4ee5\u7279\u5b9a\u7684\u683c\u5f0f\u5217\u51fa\u6240\u6709\u5e93\uff0c\u9002\u5408\u4e8e\u751f\u6210<code>requirements.txt<\/code>\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4ee3\u7801\u4e2d\u5982\u4f55\u68c0\u67e5\u67d0\u4e2a\u5e93\u662f\u5426\u5df2\u5b89\u88c5\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u5bfc\u5165\u8be5\u5e93\u6765\u68c0\u67e5\u5176\u662f\u5426\u5df2\u5b89\u88c5\u3002\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\uff0c\u5982\u679c\u5e93\u5bfc\u5165\u6210\u529f\uff0c\u5219\u8bf4\u660e\u5df2\u5b89\u88c5\uff1b\u5982\u679c\u629b\u51fa<code>ImportError<\/code>\u5f02\u5e38\uff0c\u5219\u8868\u793a\u672a\u5b89\u88c5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    import some_library\n    print(&quot;\u5e93\u5df2\u5b89\u88c5&quot;)\nexcept ImportError:\n    print(&quot;\u5e93\u672a\u5b89\u88c5&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u53d1\u73b0\u67d0\u4e2a\u5e93\u6ca1\u6709\u5b89\u88c5\uff0c\u8be5\u5982\u4f55\u5b89\u88c5\u5b83\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>pip install \u5e93\u540d<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u7f3a\u5931\u7684\u5e93\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165\u8fd9\u6761\u547d\u4ee4\uff0c\u66ff\u6362\u201c\u5e93\u540d\u201d\u4e3a\u4f60\u9700\u8981\u5b89\u88c5\u7684\u5e93\u7684\u5b9e\u9645\u540d\u79f0\u3002\u5982\u679c\u4f60\u9700\u8981\u7279\u5b9a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install \u5e93\u540d==\u7248\u672c\u53f7<\/code>\u7684\u683c\u5f0f\u8fdb\u884c\u5b89\u88c5\u3002\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u4ee5\u4fbf\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u68c0\u6d4bPython\u662f\u5426\u5b89\u88c5\u4e86\u67d0\u4e9b\u5e93\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u3001\u4f7f\u7528Python\u811a\u672c\u3001\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08 [&hellip;]","protected":false},"author":3,"featured_media":1035911,"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\/1035904"}],"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=1035904"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035904\/revisions"}],"predecessor-version":[{"id":1035912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035904\/revisions\/1035912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}