{"id":1170003,"date":"2025-01-15T16:17:18","date_gmt":"2025-01-15T08:17:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170003.html"},"modified":"2025-01-15T16:17:21","modified_gmt":"2025-01-15T08:17:21","slug":"python2-%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170003.html","title":{"rendered":"python2 \u5982\u4f55\u5bfc\u5165\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070831\/dd157b46-671b-422f-8f1f-12a86b79186b.webp\" alt=\"python2 \u5982\u4f55\u5bfc\u5165\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5728Python2\u4e2d\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528import\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528from&#8230;import\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3001\u4f7f\u7528as\u7ed9\u6a21\u5757\u8d77\u522b\u540d\u3001\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\u5e76\u5bfc\u5165\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5bfc\u5165\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>Python2\u662f\u4e00\u4e2a\u5df2\u88ab\u6dd8\u6c70\u7684\u7248\u672c\uff0c\u4f46\u5728\u4e00\u4e9b\u65e7\u7cfb\u7edf\u4e2d\u4ecd\u7136\u88ab\u4f7f\u7528\u3002\u4e86\u89e3\u5982\u4f55\u5728Python2\u4e2d\u5bfc\u5165\u6a21\u5757\u5bf9\u4e8e\u7ef4\u62a4\u8fd9\u4e9b\u7cfb\u7edf\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4f8b\u5982<code>import math<\/code>\u3002\u8fd9\u5c06\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u7684\u6240\u6709\u529f\u80fd\u3002\u5176\u6b21\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u4f8b\u5982<code>from math import sqrt<\/code>\uff0c\u8fd9\u5c06\u53ea\u5bfc\u5165<code>sqrt<\/code>\u51fd\u6570\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u7ed9\u6a21\u5757\u8d77\u522b\u540d\uff0c\u4f8b\u5982<code>import numpy as np<\/code>\uff0c\u8fd9\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528import\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6211\u4eec\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u6240\u6709\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>math<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u4e86\u5176\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u8ba1\u7b97\u4e8616\u7684\u5e73\u65b9\u6839\u3002\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u7684\u597d\u5904\u662f\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u3002\u4f46\u540c\u65f6\uff0c\u8fd9\u4e5f\u53ef\u80fd\u4f1a\u589e\u52a0\u5185\u5b58\u4f7f\u7528\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u5185\u5bb9\u90fd\u4f1a\u88ab\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528from&#8230;import\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u7279\u5b9a\u529f\u80fd\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u90e8\u5206\u3002\u4f8b\u5982\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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u5bfc\u5165\u4e86<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u7684\u4f7f\u7528\uff0c\u56e0\u4e3a\u53ea\u5bfc\u5165\u4e86\u6211\u4eec\u9700\u8981\u7684\u90e8\u5206\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u8986\u76d6\u6389\u540c\u540d\u7684\u672c\u5730\u51fd\u6570\u6216\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528as\u7ed9\u6a21\u5757\u8d77\u522b\u540d<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6a21\u5757\u7684\u540d\u5b57\u53ef\u80fd\u6bd4\u8f83\u957f\uff0c\u6216\u8005\u6211\u4eec\u5e0c\u671b\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e00\u4e2a\u66f4\u7b80\u77ed\u7684\u540d\u5b57\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u7ed9\u6a21\u5757\u8d77\u522b\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([1, 2, 3])<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>numpy<\/code>\u6a21\u5757\u5e76\u7ed9\u5b83\u8d77\u4e86\u522b\u540d<code>np<\/code>\u3002\u8fd9\u6837\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u4f7f\u7528<code>np<\/code>\u6765\u4ee3\u66ff<code>numpy<\/code>\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\u5e76\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>Python2\u4e5f\u652f\u6301\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\u3002\u901a\u5e38\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u6765\u5b89\u88c5\u8fd9\u4e9b\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5<code>requests<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8fd0\u884c\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;http:\/\/www.example.com&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86<code>requests<\/code>\u6a21\u5757\u5e76\u4f7f\u7528\u5b83\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7406\u89e3\u6a21\u5757\u7684\u67e5\u627e\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u6309\u7167\u4e00\u5b9a\u7684\u987a\u5e8f\u67e5\u627e\u6a21\u5757\u3002\u901a\u5e38\u4f1a\u5148\u5728\u5f53\u524d\u76ee\u5f55\u67e5\u627e\uff0c\u7136\u540e\u5728<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\u67e5\u627e\uff0c\u6700\u540e\u5728Python\u7684\u6807\u51c6\u5e93\u76ee\u5f55\u4e2d\u67e5\u627e\u3002\u7406\u89e3\u8fd9\u4e2a\u67e5\u627e\u8def\u5f84\u6709\u52a9\u4e8e\u89e3\u51b3\u6a21\u5757\u5bfc\u5165\u5931\u8d25\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5bfc\u5165\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u4e2d\u7684\u6a21\u5757\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bfc\u5165\u81ea\u5df1\u7f16\u5199\u7684\u6a21\u5757\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757<code>mymodule.py<\/code>\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>myfunc<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def myfunc():<\/p>\n<p>    print(&quot;Hello from mymodule!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2aPython\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5bfc\u5165\u5e76\u4f7f\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>mymodule.myfunc()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ea\u8981<code>mymodule.py<\/code>\u6587\u4ef6\u5728\u5f53\u524d\u76ee\u5f55\u6216<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\uff0cPython\u89e3\u91ca\u5668\u5c31\u80fd\u627e\u5230\u5e76\u5bfc\u5165\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6a21\u5757\u53ef\u80fd\u4f1a\u7ec4\u7ec7\u5728\u591a\u4e2a\u5305\uff08\u76ee\u5f55\uff09\u4e2d\u3002Python2\u652f\u6301\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><p><strong>\u7edd\u5bf9\u5bfc\u5165<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u7edd\u5bf9\u5bfc\u5165\u662f\u4ece\u9879\u76ee\u7684\u6839\u76ee\u5f55\u5f00\u59cb\u5bfc\u5165\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    package\/<\/p>\n<p>        __init__.py<\/p>\n<p>        module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>module2.py<\/code>\u4e2d\u5bfc\u5165<code>module1<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>import module1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u76f8\u5bf9\u5bfc\u5165<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u662f\u57fa\u4e8e\u5f53\u524d\u6a21\u5757\u7684\u4f4d\u7f6e\u8fdb\u884c\u5bfc\u5165\u3002\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u9879\u76ee\u7ed3\u6784\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728<code>module2.py<\/code>\u4e2d\u76f8\u5bf9\u5bfc\u5165<code>module1<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module2.py<\/p>\n<p>from .. import module1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6a21\u5757\u5316\uff0c\u4f46\u5728\u7406\u89e3\u548c\u7ef4\u62a4\u65f6\u53ef\u80fd\u66f4\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u907f\u514d\u5faa\u73af\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e24\u4e2a\u6a21\u5757\u53ef\u80fd\u4f1a\u76f8\u4e92\u5bfc\u5165\uff0c\u5bfc\u81f4\u5faa\u73af\u5bfc\u5165\u95ee\u9898\u3002\u8fd9\u4f1a\u5f15\u53d1<code>ImportError<\/code>\uff0c\u56e0\u4e3aPython\u89e3\u91ca\u5668\u65e0\u6cd5\u786e\u5b9a\u54ea\u4e2a\u6a21\u5757\u5e94\u8be5\u5148\u88ab\u5bfc\u5165\u3002\u8981\u907f\u514d\u5faa\u73af\u5bfc\u5165\uff0c\u53ef\u4ee5\u91cd\u6784\u4ee3\u7801\uff0c\u5c06\u5171\u7528\u7684\u529f\u80fd\u653e\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\u4e2d\uff0c\u6216\u8005\u4f7f\u7528\u5ef6\u8fdf\u5bfc\u5165\uff08\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6a21\u5757\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5ef6\u8fdf\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5ef6\u8fdf\u5bfc\u5165\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u6837\u53ea\u6709\u5728\u8c03\u7528\u51fd\u6570\u65f6\u624d\u4f1a\u5bfc\u5165\u6a21\u5757\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u5faa\u73af\u5bfc\u5165\uff0c\u5e76\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u542f\u52a8\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def myfunc():<\/p>\n<p>    import math<\/p>\n<p>    print(math.sqrt(16))<\/p>\n<p>myfunc()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>math<\/code>\u6a21\u5757\u53ea\u6709\u5728\u8c03\u7528<code>myfunc<\/code>\u51fd\u6570\u65f6\u624d\u4f1a\u88ab\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528__future__\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__future__<\/code>\u6a21\u5757\u5bfc\u5165\u4e00\u4e9b\u5728Python3\u4e2d\u5f15\u5165\u7684\u65b0\u7279\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bfc\u5165Python3\u7684\u9664\u6cd5\u884c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from __future__ import division<\/p>\n<p>print(5 \/ 2)  # \u8f93\u51fa2.5\uff0c\u800c\u4e0d\u662f2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>__future__<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u9010\u6b65\u9002\u5e94Python3\u7684\u65b0\u7279\u6027\uff0c\u4e3a\u8fc1\u79fb\u5230Python3\u505a\u51c6\u5907\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5bfc\u5165\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u3001\u66f4\u6613\u7ef4\u62a4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u987a\u5e8f<\/strong>\uff1a\u6309\u7167\u6807\u51c6\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u548c\u81ea\u5b9a\u4e49\u6a21\u5757\u7684\u987a\u5e8f\u5bfc\u5165\u6a21\u5757\uff0c\u5e76\u5728\u6bcf\u4e2a\u90e8\u5206\u4e4b\u95f4\u7559\u4e00\u884c\u7a7a\u884c\u3002<\/li>\n<li><strong>\u907f\u514d\u4f7f\u7528\u901a\u914d\u7b26\u5bfc\u5165<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u4f7f\u7528<code>from module import *<\/code>\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u5165\u6240\u6709\u5185\u5bb9\uff0c\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002<\/li>\n<li><strong>\u4f7f\u7528\u522b\u540d<\/strong>\uff1a\u4e3a\u957f\u540d\u5b57\u7684\u6a21\u5757\u4f7f\u7528\u522b\u540d\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/li>\n<li><strong>\u7ec4\u7ec7\u5bfc\u5165\u8bed\u53e5<\/strong>\uff1a\u5c06\u6240\u6709\u5bfc\u5165\u8bed\u53e5\u653e\u5728\u6587\u4ef6\u7684\u9876\u90e8\uff0c\u4fbf\u4e8e\u67e5\u627e\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python2\u4e2d\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>import<\/code>\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528<code>from...import<\/code>\u5bfc\u5165\u7279\u5b9a\u90e8\u5206\u3001\u4f7f\u7528<code>as<\/code>\u7ed9\u6a21\u5757\u8d77\u522b\u540d\u3001\u5b89\u88c5\u7b2c\u4e09\u65b9\u6a21\u5757\u5e76\u5bfc\u5165\u3001\u7406\u89e3\u6a21\u5757\u7684\u67e5\u627e\u8def\u5f84\u3001\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3001\u76f8\u5bf9\u5bfc\u5165\u548c\u7edd\u5bf9\u5bfc\u5165\u3001\u907f\u514d\u5faa\u73af\u5bfc\u5165\u3001\u5ef6\u8fdf\u5bfc\u5165\u3001\u4f7f\u7528<code>__future__<\/code>\u6a21\u5757\u4ee5\u53ca\u9075\u5faa\u5bfc\u5165\u6a21\u5757\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u53ef\u4ee5\u4f7f\u4f60\u5728Python2\u73af\u5883\u4e2d\u9ad8\u6548\u5730\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\uff0c\u7ef4\u62a4\u65e7\u7cfb\u7edf\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1Python2\u5df2\u7ecf\u88ab\u6dd8\u6c70\uff0c\u4f46\u4e86\u89e3\u8fd9\u4e9b\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u4ecd\u7136\u662f\u7ef4\u62a4\u65e7\u7cfb\u7edf\u548c\u8fc1\u79fb\u5230Python3\u7684\u91cd\u8981\u6280\u80fd\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 2\u4e2d\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python 2\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u6a21\u5757\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.py<\/code>\uff09\u4e0e\u4e3b\u7a0b\u5e8f\u6587\u4ef6\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\u7136\u540e\uff0c\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u4f7f\u7528<code>import mymodule<\/code>\u6765\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7<code>mymodule.function_name()<\/code>\u6765\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728Python 2\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6a21\u5757\u5bfc\u5165\u65f6\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5f53\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u5982\u679c\u6a21\u5757\u4e0d\u5b58\u5728\u6216\u8def\u5f84\u4e0d\u6b63\u786e\uff0cPython\u5c06\u629b\u51fa<code>ImportError<\/code>\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    import mymodule\nexcept ImportError:\n    print(&quot;\u6a21\u5757\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6a21\u5757\u540d\u548c\u8def\u5f84\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u6a21\u5757\u5bfc\u5165\u5931\u8d25\u800c\u5d29\u6e83\uff0c\u5e76\u53ef\u4ee5\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>Python 2\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u5f0f\u6765\u5bfc\u5165\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python 2\u4e2d\uff0c\u5bfc\u5165\u6a21\u5757\u6709\u591a\u79cd\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>import module_name<\/code>\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4f7f\u7528<code>from module_name import function_name<\/code>\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u6216\u4f7f\u7528<code>from module_name import *<\/code>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u3002\u6ce8\u610f\uff0c\u4f7f\u7528<code>import *<\/code>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u56e0\u6b64\u5728\u5927\u578b\u9879\u76ee\u4e2d\u4e0d\u63a8\u8350\u4f7f\u7528\u3002\u9009\u62e9\u5408\u9002\u7684\u5bfc\u5165\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python2\u4e2d\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528import\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528from&#8230;imp [&hellip;]","protected":false},"author":3,"featured_media":1170011,"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\/1170003"}],"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=1170003"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170003\/revisions"}],"predecessor-version":[{"id":1170014,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170003\/revisions\/1170014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170011"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}