{"id":1179401,"date":"2025-01-15T18:23:09","date_gmt":"2025-01-15T10:23:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179401.html"},"modified":"2025-01-15T18:23:12","modified_gmt":"2025-01-15T10:23:12","slug":"python%e4%b8%8b%e8%bd%bd%e5%a5%bd%e6%a8%a1%e5%9d%97%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179401.html","title":{"rendered":"python\u4e0b\u8f7d\u597d\u6a21\u5757\u5982\u4f55\u5bfc\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113822\/f2f63c3c-2e18-40d1-ac2c-f777ddd11ca5.webp\" alt=\"python\u4e0b\u8f7d\u597d\u6a21\u5757\u5982\u4f55\u5bfc\u5165\" \/><\/p>\n<p><p> <strong>Python\u4e0b\u8f7d\u597d\u7684\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u5bfc\u5165\u3001\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\u3001\u4f7f\u7528\u6b63\u786e\u7684\u6a21\u5757\u540d\u79f0\u5bfc\u5165\u3001\u8bbe\u7f6e\u6b63\u786e\u7684Python\u8def\u5f84\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u65b9\u4fbf\u7ba1\u7406\u4f9d\u8d56<\/strong>\u3002\u5176\u4e2d\uff0c\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\u662f\u6700\u5173\u952e\u7684\u4e00\u70b9\u3002\u8981\u786e\u4fdd\u4f60\u4f7f\u7528\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u4f60\u60f3\u8981\u5bfc\u5165\u7684\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u6765\u5b89\u88c5\u6a21\u5757\uff0c\u8fd9\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u8981\u5bfc\u5165\u540d\u4e3arequests\u7684\u6a21\u5757\uff0c\u4f60\u9700\u8981\u5148\u786e\u4fdd\u5b83\u5df2\u5b89\u88c5\u3002\u4f60\u53ef\u4ee5\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5requests\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u786e\u8ba4\u5b89\u88c5\u6210\u529f\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\u4f7f\u7528import\u8bed\u53e5\u6765\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528requests\u6a21\u5757\u63d0\u4f9b\u7684\u529f\u80fd\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165<\/h2>\n<\/p>\n<p><p>Python\u4e2d\u6700\u57fa\u672c\u7684\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528import\u8bed\u53e5\u3002\u4f7f\u7528import\u8bed\u53e5\u53ef\u4ee5\u5c06\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\u5230\u4f60\u7684Python\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684import\u8bed\u53e5\u5c06\u6574\u4e2a\u6a21\u5757\u5bfc\u5165\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684import math\u8bed\u53e5\u5c06math\u6a21\u5757\u5bfc\u5165\u5230\u5f53\u524d\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5e76\u4e14\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528math\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u5bfc\u5165\u6a21\u5757\u7684\u7279\u5b9a\u90e8\u5206<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<p>print(sqrt(16))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ea\u5bfc\u5165\u4e86math\u6a21\u5757\u4e2d\u7684sqrt\u51fd\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u800c\u65e0\u9700\u6bcf\u6b21\u90fd\u5199math.sqrt\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u4e4b\u524d\uff0c\u5fc5\u987b\u786e\u4fdd\u8be5\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u3002\u5426\u5219\uff0cimport\u8bed\u53e5\u4f1a\u629b\u51faModuleNotFoundError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u4f7f\u7528pip\u5b89\u88c5\u6a21\u5757<\/h3>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u3001\u66f4\u65b0\u548c\u5378\u8f7dPython\u5305\u3002\u4f7f\u7528pip\u5b89\u88c5\u6a21\u5757\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install \u6a21\u5757\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u68c0\u67e5\u6a21\u5757\u662f\u5426\u5df2\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u68c0\u67e5\u67d0\u4e2a\u6a21\u5757\u662f\u5426\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show \u6a21\u5757\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u68c0\u67e5requests\u6a21\u5757\u662f\u5426\u5df2\u5b89\u88c5\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip show requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u5df2\u5b89\u88c5\uff0c\u8be5\u547d\u4ee4\u4f1a\u663e\u793a\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u7248\u672c\u53f7\u3001\u5b89\u88c5\u8def\u5f84\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6b63\u786e\u7684\u6a21\u5757\u540d\u79f0\u5bfc\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u6a21\u5757\u7684\u6b63\u786e\u540d\u79f0\u3002\u6a21\u5757\u7684\u540d\u79f0\u901a\u5e38\u662f\u5305\u7684\u540d\u79f0\u6216.py\u6587\u4ef6\u7684\u540d\u79f0\uff08\u4e0d\u5305\u62ec\u6269\u5c55\u540d\uff09\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u5bfc\u5165\u7b2c\u4e09\u65b9\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u7b2c\u4e09\u65b9\u6a21\u5757\u662f\u6307\u90a3\u4e9b\u4e0d\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\u3002\u5bfc\u5165\u7b2c\u4e09\u65b9\u6a21\u5757\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6a21\u5757\u540d\u79f0\u4e0e\u5b89\u88c5\u65f6\u4f7f\u7528\u7684\u540d\u79f0\u4e00\u81f4\u3002\u4f8b\u5982\uff0c\u5bfc\u5165requests\u6a21\u5757\u65f6\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u81ea\u5df1\u7f16\u5199\u4e86\u4e00\u4e2a\u6a21\u5757\uff0c\u5e76\u4e14\u5e0c\u671b\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u5bfc\u5165\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u6587\u4ef6\u7684\u540d\u79f0\uff08\u4e0d\u5305\u62ec\u6269\u5c55\u540d\uff09\u4f5c\u4e3a\u6a21\u5757\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3amymodule.py\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u8bbe\u7f6e\u6b63\u786e\u7684Python\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u5bfc\u5165\u6a21\u5757\u5931\u8d25\u7684\u95ee\u9898\uff0c\u539f\u56e0\u53ef\u80fd\u662fPython\u7684\u8def\u5f84\u8bbe\u7f6e\u4e0d\u6b63\u786e\u3002Python\u89e3\u91ca\u5668\u4f1a\u5728sys.path\u5217\u8868\u4e2d\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u6a21\u5757\u3002\u5982\u679c\u6a21\u5757\u4e0d\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\uff0c\u5c31\u4f1a\u629b\u51faModuleNotFoundError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u4fee\u6539sys.path<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539sys.path\u5217\u8868\u6765\u6dfb\u52a0\u65b0\u7684\u641c\u7d22\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u6a21\u5757\u4f4d\u4e8e\/home\/user\/mypythonmodules\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u8be5\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/home\/user\/mypythonmodules&#39;)<\/p>\n<p>import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u8bbe\u7f6e\u73af\u5883\u53d8\u91cfPYTHONPATH<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cfPYTHONPATH\u6765\u6307\u5b9a\u989d\u5916\u7684\u641c\u7d22\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=\/home\/user\/mypythonmodules<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cmd\">set PYTHONPATH=C:\\Users\\user\\mypythonmodules<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u540e\uff0cPython\u89e3\u91ca\u5668\u4f1a\u5728\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u641c\u7d22\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u65b9\u4fbf\u7ba1\u7406\u4f9d\u8d56<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4f8b\u5982pip\u3001pipenv\u548cpoetry\u7b49\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u4f7f\u7528pip<\/h3>\n<\/p>\n<p><p>pip\u662f\u6700\u5e38\u7528\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b89\u88c5\u3001\u66f4\u65b0\u548c\u5378\u8f7dPython\u5305\u3002\u4f7f\u7528pip\u5b89\u88c5\u6a21\u5757\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install \u6a21\u5757\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u4f7f\u7528requirements.txt\u6587\u4ef6\u6765\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u5c06\u9879\u76ee\u4f9d\u8d56\u7684\u6240\u6709\u6a21\u5757\u53ca\u5176\u7248\u672c\u53f7\u5199\u5165requirements.txt\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>requests==2.24.0<\/p>\n<p>numpy==1.19.2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u4f7f\u7528pipenv<\/h3>\n<\/p>\n<p><p>pipenv\u662f\u4e00\u4e2a\u96c6\u6210\u4e86pip\u548cvirtualenv\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u865a\u62df\u73af\u5883\u3002\u4f7f\u7528pipenv\u5b89\u88c5\u6a21\u5757\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pipenv install \u6a21\u5757\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pipenv install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pipenv\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u5e76\u5c06\u6a21\u5757\u5b89\u88c5\u5230\u8be5\u865a\u62df\u73af\u5883\u4e2d\u3002\u4f7f\u7528pipenv shell\u547d\u4ee4\u53ef\u4ee5\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff0c\u4f7f\u7528pipenv run\u547d\u4ee4\u53ef\u4ee5\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>5.3 \u4f7f\u7528poetry<\/h3>\n<\/p>\n<p><p>poetry\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u548c\u53d1\u5e03\u8fc7\u7a0b\u3002\u4f7f\u7528poetry\u5b89\u88c5\u6a21\u5757\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry add \u6a21\u5757\u540d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b89\u88c5requests\u6a21\u5757\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">poetry add requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>poetry\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u5e76\u5c06\u6a21\u5757\u5b89\u88c5\u5230\u8be5\u865a\u62df\u73af\u5883\u4e2d\u3002\u4f7f\u7528poetry shell\u547d\u4ee4\u53ef\u4ee5\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff0c\u4f7f\u7528poetry run\u547d\u4ee4\u53ef\u4ee5\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883\u9694\u79bb\u9879\u76ee\u4f9d\u8d56<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5c06\u4e0d\u540c\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u9694\u79bb\u5f00\u6765\uff0c\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\u3002\u5e38\u7528\u7684\u865a\u62df\u73af\u5883\u5de5\u5177\u6709virtualenv\u3001venv\u548cconda\u7b49\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u4f7f\u7528virtualenv<\/h3>\n<\/p>\n<p><p>virtualenv\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u865a\u62df\u73af\u5883\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u3002\u4f7f\u7528virtualenv\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">virtualenv env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cenv\u662f\u865a\u62df\u73af\u5883\u7684\u76ee\u5f55\u540d\u79f0\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source env\/bin\/activate  # Linux\u548cmacOS<\/p>\n<p>env\\Scripts\\activate  # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u6a21\u5757\uff0c\u6a21\u5757\u4f1a\u88ab\u5b89\u88c5\u5230\u865a\u62df\u73af\u5883\u4e2d\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>6.2 \u4f7f\u7528venv<\/h3>\n<\/p>\n<p><p>venv\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u3002\u4f7f\u7528venv\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cenv\u662f\u865a\u62df\u73af\u5883\u7684\u76ee\u5f55\u540d\u79f0\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source env\/bin\/activate  # Linux\u548cmacOS<\/p>\n<p>env\\Scripts\\activate  # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u6a21\u5757\uff0c\u6a21\u5757\u4f1a\u88ab\u5b89\u88c5\u5230\u865a\u62df\u73af\u5883\u4e2d\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>6.3 \u4f7f\u7528conda<\/h3>\n<\/p>\n<p><p>conda\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5305\u7ba1\u7406\u548c\u865a\u62df\u73af\u5883\u7ba1\u7406\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u76f8\u5173\u7684\u5305\u3002\u4f7f\u7528conda\u521b\u5efa\u865a\u62df\u73af\u5883\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda create --name env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cenv\u662f\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">conda activate env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528conda\u6216pip\u5b89\u88c5\u6a21\u5757\uff0c\u6a21\u5757\u4f1a\u88ab\u5b89\u88c5\u5230\u865a\u62df\u73af\u5883\u4e2d\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u73af\u5883\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u6a21\u5757\u5bfc\u5165\u9519\u8bef<\/h2>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u6709\u65f6\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>7.1 ModuleNotFoundError<\/h3>\n<\/p>\n<p><p>ModuleNotFoundError\u662f\u6700\u5e38\u89c1\u7684\u5bfc\u5165\u9519\u8bef\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u6a21\u5757\u672a\u5b89\u88c5\u6216\u8def\u5f84\u8bbe\u7f6e\u4e0d\u6b63\u786e\u5f15\u8d77\u7684\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip show\u547d\u4ee4\u68c0\u67e5\u6a21\u5757\u662f\u5426\u5df2\u5b89\u88c5\u3002<\/li>\n<li>\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u6a21\u5757\u540d\u79f0\u5bfc\u5165\u3002<\/li>\n<li>\u68c0\u67e5sys.path\u5217\u8868\uff0c\u786e\u4fdd\u6a21\u5757\u6240\u5728\u7684\u76ee\u5f55\u5728sys.path\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528\u73af\u5883\u53d8\u91cfPYTHONPATH\u8bbe\u7f6e\u989d\u5916\u7684\u641c\u7d22\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><h3>7.2 ImportError<\/h3>\n<\/p>\n<p><p>ImportError\u901a\u5e38\u662f\u7531\u4e8e\u6a21\u5757\u5185\u90e8\u7684\u547d\u540d\u51b2\u7a81\u6216\u5bfc\u5165\u8def\u5f84\u4e0d\u6b63\u786e\u5f15\u8d77\u7684\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u6a21\u5757\u5185\u90e8\u6ca1\u6709\u547d\u540d\u51b2\u7a81\uff0c\u53ef\u4ee5\u4f7f\u7528\u522b\u540d\u6765\u907f\u514d\u51b2\u7a81\u3002<\/li>\n<li>\u786e\u4fdd\u5bfc\u5165\u8def\u5f84\u6b63\u786e\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u6216\u7edd\u5bf9\u5bfc\u5165\u3002<\/li>\n<li>\u68c0\u67e5\u6a21\u5757\u7684__init__.py\u6587\u4ef6\uff0c\u786e\u4fdd\u6a21\u5757\u53ef\u4ee5\u88ab\u6b63\u786e\u5bfc\u5165\u3002<\/li>\n<\/ul>\n<p><h3>7.3 \u5176\u4ed6\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5176\u4ed6\u9519\u8bef\u53ef\u80fd\u662f\u7531\u4e8e\u6a21\u5757\u672c\u8eab\u7684\u4ee3\u7801\u95ee\u9898\u5f15\u8d77\u7684\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5\u6a21\u5757\u7684\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u786e\u4fdd\u4f7f\u7528\u65b9\u6cd5\u6b63\u786e\u3002<\/li>\n<li>\u68c0\u67e5\u6a21\u5757\u7684\u7248\u672c\uff0c\u786e\u4fdd\u4e0e\u9879\u76ee\u7684\u5176\u4ed6\u4f9d\u8d56\u517c\u5bb9\u3002<\/li>\n<li>\u67e5\u9605\u6a21\u5757\u7684\u6e90\u7801\uff0c\u4e86\u89e3\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h2>\u516b\u3001\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u6a21\u5757\u548c\u7ba1\u7406\u4f9d\u8d56\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>8.1 \u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5c06\u4e0d\u540c\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u9694\u79bb\u5f00\u6765\uff0c\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\u3002\u65e0\u8bba\u662f\u4f7f\u7528virtualenv\u3001venv\u8fd8\u662fconda\uff0c\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>8.2 \u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u4f7f\u7528pipenv\u6216poetry\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u7ba1\u7406\u865a\u62df\u73af\u5883\uff0c\u5e76\u81ea\u52a8\u89e3\u51b3\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>8.3 \u4f7f\u7528requirements.txt\u6216pyproject.toml\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5c06\u9879\u76ee\u4f9d\u8d56\u7684\u6240\u6709\u6a21\u5757\u53ca\u5176\u7248\u672c\u53f7\u5199\u5165requirements.txt\u6216pyproject.toml\u6587\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u66f4\u65b0\u4f9d\u8d56\u5173\u7cfb\u3002\u4f7f\u7528pip install -r requirements.txt\u6216poetry install\u547d\u4ee4\u53ef\u4ee5\u4e00\u6b21\u6027\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>8.4 \u907f\u514d\u5faa\u73af\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u5bfc\u5165\u662f\u6307\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u5bf9\u65b9\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bfc\u5165\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8bbe\u8ba1\u6a21\u5757\u7ed3\u6784\u6216\u4f7f\u7528\u5ef6\u8fdf\u5bfc\u5165\u6765\u907f\u514d\u5faa\u73af\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>8.5 \u9075\u5faaPEP 8\u98ce\u683c\u6307\u5357<\/h3>\n<\/p>\n<p><p>PEP 8\u662fPython\u7684\u98ce\u683c\u6307\u5357\uff0c\u63a8\u8350\u7684\u5bfc\u5165\u987a\u5e8f\u662f\uff1a\u6807\u51c6\u5e93\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u6a21\u5757\u3001\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u6bcf\u4e2a\u90e8\u5206\u4e4b\u95f4\u5e94\u8be5\u6709\u4e00\u4e2a\u7a7a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<p>import requests<\/p>\n<p>import mymodule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9075\u5faaPEP 8\u98ce\u683c\u6307\u5357\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u4e0b\u8f7d\u597d\u7684\u6a21\u5757\u5bfc\u5165\u9700\u8981\u6ce8\u610f\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\u3001\u4f7f\u7528\u6b63\u786e\u7684\u6a21\u5757\u540d\u79f0\u5bfc\u5165\u3001\u8bbe\u7f6e\u6b63\u786e\u7684Python\u8def\u5f84\u4ee5\u53ca\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u65b9\u4fbf\u7ba1\u7406\u4f9d\u8d56\u3002\u901a\u8fc7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u8ba4\u6211\u4e0b\u8f7d\u7684Python\u6a21\u5757\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip list<\/code>\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u5df2\u5b89\u88c5\u7684\u6a21\u5757\u53ca\u5176\u7248\u672c\u3002\u5982\u679c\u4f60\u80fd\u591f\u5728\u5217\u8868\u4e2d\u627e\u5230\u4f60\u4e0b\u8f7d\u7684\u6a21\u5757\u540d\u79f0\uff0c\u8bf4\u660e\u5b83\u5df2\u6210\u529f\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u5bfc\u5165\u6a21\u5757\u6765\u9a8c\u8bc1\uff0c\u4f8b\u5982\u5728Python\u4ea4\u4e92\u5f0f\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>import module_name<\/code>\uff08\u5c06<code>module_name<\/code>\u66ff\u6362\u4e3a\u5b9e\u9645\u6a21\u5757\u540d\uff09\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5e94\u8be5\u600e\u4e48\u89e3\u51b3\uff1f<\/strong><br \/>\u9047\u5230\u5bfc\u5165\u9519\u8bef\u65f6\uff0c\u9996\u5148\u8981\u68c0\u67e5\u6a21\u5757\u662f\u5426\u786e\u5b9e\u5b89\u88c5\u5728\u5f53\u524d\u7684Python\u73af\u5883\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip show module_name<\/code>\u547d\u4ee4\u67e5\u770b\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5982\u679c\u6a21\u5757\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install module_name<\/code>\u6765\u5b89\u88c5\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u6ca1\u6709\u62fc\u5199\u9519\u8bef\uff0c\u4e14Python\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u6b63\u786e\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u672c\u5730\u6a21\u5757\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u8981\u5bfc\u5165\u672c\u5730\u6a21\u5757\uff0c\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u5728\u4f60\u7684Python\u811a\u672c\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5728Python\u8def\u5f84\u4e2d\u3002\u4f7f\u7528<code>import your_module<\/code>\u547d\u4ee4\u5bfc\u5165\u6a21\u5757\u3002\u5982\u679c\u6a21\u5757\u5728\u5b50\u76ee\u5f55\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from subdirectory import your_module<\/code>\u7684\u65b9\u5f0f\u8fdb\u884c\u5bfc\u5165\u3002\u5728\u5bfc\u5165\u672c\u5730\u6a21\u5757\u65f6\uff0c\u6ce8\u610f\u547d\u540d\u51b2\u7a81\uff0c\u786e\u4fdd\u6a21\u5757\u540d\u79f0\u4e0d\u4e0e\u5185\u7f6e\u6a21\u5757\u91cd\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e0b\u8f7d\u597d\u7684\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7import\u8bed\u53e5\u5bfc\u5165\u3001\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\u3001\u4f7f\u7528\u6b63\u786e\u7684\u6a21\u5757\u540d\u79f0\u5bfc\u5165\u3001\u8bbe\u7f6e\u6b63\u786e\u7684P [&hellip;]","protected":false},"author":3,"featured_media":1179403,"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\/1179401"}],"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=1179401"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179401\/revisions"}],"predecessor-version":[{"id":1179404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179401\/revisions\/1179404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179403"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}