{"id":160028,"date":"2019-10-12T11:45:58","date_gmt":"2019-10-12T03:45:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=160028"},"modified":"2019-09-27T14:47:43","modified_gmt":"2019-09-27T06:47:43","slug":"python-version","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-version.html","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u68c0\u67e5 Python \u7248\u672c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6559\u7a0b\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u547d\u4ee4\u884c\u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002\u8fd9\u5728\u5b89\u88c5\u7528 Python \u7f16\u5199\u7684\u9700\u8981\u7279\u5b9a\u7248\u672c Python \u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Python \u662f\u4e16\u754c\u4e0a\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\u3002\u5b83\u7528\u4e8e\u5f00\u53d1\u7f51\u7ad9\uff0c\u7f16\u5199\u811a\u672c\uff0c\u673a\u5668\u5b66\u4e60\uff0c\u5206\u6790\u6570\u636e\u7b49\u3002<\/p>\n

\u672c\u6559\u7a0b\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u547d\u4ee4\u884c\u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002\u8fd9\u5728\u5b89\u88c5\u7528 Python \u7f16\u5199\u7684\u9700\u8981\u7279\u5b9a\u7248\u672c Python \u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n

\u6211\u4eec\u8fd8\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u786e\u5b9a\u8fd0\u884c Python \u811a\u672c\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002\u4f8b\u5982\uff0c\u5728\u7f16\u5199 Python \u811a\u672c\u65f6\uff0c\u60a8\u9700\u8981\u786e\u5b9a\u811a\u672c\u662f\u5426\u652f\u6301\u7528\u6237\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002<\/p>\n

<\/p>\n

\u68c0\u67e5 Python \u7248\u672c<\/strong><\/div>\n

<\/strong><\/p>\n

Python \u5df2\u9884\u5148\u5b89\u88c5\u5728\u5927\u591a\u6570 Linux \u53d1\u884c\u7248\u548c macOS \u4e0a\u3002<\/p>\n

\u8981\u627e\u51fa\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684\u9ed8\u8ba4\u7684 Python \u7248\u672c\uff0c\u8bf7\u8fd0\u884c python --version \u6216\u8005 python -V \u547d\u4ee4\uff1a<\/p>\n

[linuxidc@localhost www.linuxidc.com]$ python --version\r\nPython 2.7.5<\/pre>\n

\u8be5\u547d\u4ee4\u5c06\u6253\u5370\u9ed8\u8ba4\u7684 Python \u7248\u672c\uff0c\u672c\u4f8b\u4e2d\u7684\u7248\u672c\u662f 2.7.5\uff0c\u60a8\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684\u7248\u672c\u53ef\u80fd\u4e0d\u540c\u3002<\/p>\n

\"\"<\/p>\n

\u5728\u811a\u672c\u7684 shebang \u884c \/usr\/bin\/python \u4e2d\u8bbe\u7f6e\u4e3a\u89e3\u91ca\u5668\u7684\u6240\u6709\u811a\u672c\u90fd\u5c06\u4f7f\u7528\u9ed8\u8ba4\u7248\u672c\u7684 Python\u00a0 \u3002<\/p>\n

\u4e00\u4e9b Linux \u53d1\u884c\u7248\u540c\u65f6\u5b89\u88c5\u4e86\u591a\u4e2a\u7248\u672c\u7684 Python \u3002\u4e00\u822c\u6765\u8bf4\uff0c Python 3 \u4e2d\u7684\u53ef\u6267\u884c\u6587\u4ef6\u547d\u540d\u662f python3\uff0c \u800c Python \u7684\u53ef\u6267\u884c\u6587\u4ef6\u547d\u540d\u662f python \u6216 python2 \uff0c\u4f46\u4e8b\u5b9e\u53ef\u80fd\u5e76\u975e\u603b\u662f\u5982\u6b64\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u952e\u5165\u4ee5\u4e0b\u5185\u5bb9\u6765\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u4e86 Python 3 \uff1a<\/p>\n

[linuxidc@localhost www.linuxidc.com]$ python3 --version\r\nPython 3.7.3<\/pre>\n

\"\"<\/p>\n

\u867d\u7136 Python 2 \u5f97\u5230\u4e86\u5f88\u597d\u7684\u652f\u6301\uff0c\u4f46 Python 3 \u88ab\u8ba4\u4e3a\u662f\u8be5\u8bed\u8a00\u7684\u73b0\u5728\u548c\u672a\u6765\u3002<\/p>\n

\u5728\u64b0\u5199\u672c\u6587\u65f6\uff0c Python \u7684\u6700\u65b0\u4e3b\u8981\u7248\u672c\u662f 3.7.x \u7248\u3002\u60a8\u53ef\u80fd\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86\u65e7\u7248\u672c\u7684 Python 3 \u3002\u5982\u679c\u8981\u5b89\u88c5\u6700\u65b0\u7248\u672c\uff0c\u5219\u8be5\u8fc7\u7a0b\u53d6\u51b3\u4e8e\u60a8\u8fd0\u884c\u7684\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n

<\/p>\n

\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u68c0\u67e5 Python \u7248\u672c<\/strong><\/div>\n

<\/strong><\/p>\n

Python 2 \u548c Python 3 \u4ece\u6839\u672c\u4e0a\u662f\u4e0d\u540c\u7684\u3002\u7528 Python 2.x \u7f16\u5199\u7684\u4ee3\u7801\u53ef\u80fd\u65e0\u6cd5\u5728 Python 3.x \u4e2d\u4f7f\u7528\u3002<\/p>\n

sys \u6a21\u5757\u5728\u6240\u6709 Python \u7248\u672c\u4e2d\u90fd\u53ef\u7528\uff0c\u4ed6\u63d0\u4f9b\u7279\u5b9a\u4e8e\u7cfb\u7edf\u7684\u53c2\u6570\u548c\u529f\u80fd\u3002 sys.version_info \u5141\u8bb8\u60a8\u786e\u5b9a\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002\u5b83\u662f\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u7248\u672c\u53f7\u7684\u5143\u7ec4\uff1a major \uff0c minor \uff0c micro \uff0c releaselevel \u548c serial \u3002<\/p>\n

\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u81f3\u5c11\u9700\u8981 Python 3.5 \u7248\u7684\u811a\u672c\uff0c\u5e76\u4e14\u60a8\u60f3\u8981\u68c0\u67e5\u7cfb\u7edf\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u5730\u68c0\u67e5 major \u548c minor \u7248\u672c\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n

import sys\r\n\r\nif not sys.version_info.major == 3 and sys.version_info.minor >= 5:\r\n\u00a0 \u00a0 print(\"This script requires Python 3.5 or higher!\")\r\n\u00a0 \u00a0 print(\"You are using Python {}.{}.\".format(sys.version_info.major, sys.version_info.minor))\r\n\u00a0 \u00a0 sys.exit(1)<\/pre>\n

\"\"<\/p>\n

\u5982\u679c\u4f7f\u7528\u5c0f\u4e8e 3.5 \u7684 Python \u7248\u672c\u8fd0\u884c\u811a\u672c<\/p>\n

[linuxidc@localhost www.linuxidc.com]$ python linuxidc.com.py<\/pre>\n

\u5219\u4f1a\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

This script requires Python 3.5 or higher!\r\nYou are using Python 2.7.<\/pre>\n

\"\"<\/p>\n

\u8981\u7f16\u5199\u5728 Python 3 \u548c 2 \u4e0b\u90fd\u80fd\u8fd0\u884c\u7684 Python \u4ee3\u7801\uff0c\u8bf7\u4f7f\u7528\u8be5 future \u6a21\u5757\u3002\u5b83\u5141\u8bb8\u60a8\u5728 Python 2 \u4e0b\u8fd0\u884c Python 3.x \u7684\u517c\u5bb9\u4ee3\u7801\u3002<\/p>\n

<\/p>\n

\u7ed3\u8bba<\/strong><\/div>\n

<\/strong><\/p>\n

\u67e5\u770b\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a<\/p>\n

python --version<\/pre>\n","protected":false},"excerpt":{"rendered":"

Python \u662f\u4e16\u754c\u4e0a\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\u3002\u5b83\u7528\u4e8e\u5f00\u53d1\u7f51\u7ad9\uff0c\u7f16\u5199\u811a\u672c\uff0c\u673a\u5668\u5b66\u4e60\uff0c\u5206\u6790\u6570\u636e\u7b49\u3002 \u672c\u6559\u7a0b\u4ecb\u7ecd\u5982\u4f55\u4f7f […]<\/p>\n","protected":false},"author":63,"featured_media":160034,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-160028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=160028"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160028\/revisions"}],"predecessor-version":[{"id":160248,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160028\/revisions\/160248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160034"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=160028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=160028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=160028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}