{"id":1152615,"date":"2025-01-13T17:27:47","date_gmt":"2025-01-13T09:27:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152615.html"},"modified":"2025-01-13T17:27:49","modified_gmt":"2025-01-13T09:27:49","slug":"python-%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85%e5%ba%93sys","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152615.html","title":{"rendered":"python \u5982\u4f55\u5b89\u88c5\u5e93sys"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182541\/290ae638-3b98-4940-b6e1-1f81b40cab3f.webp\" alt=\"python \u5982\u4f55\u5b89\u88c5\u5e93sys\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4e0d\u9700\u8981\u5b89\u88c5\u5e93sys<\/strong>\uff0c<strong>\u56e0\u4e3asys\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00<\/strong>\u3002<strong>sys\u5e93\u5305\u542b\u4e86\u4e00\u4e9b\u7528\u4e8e\u4e0ePython\u89e3\u91ca\u5668\u8fdb\u884c\u4ea4\u4e92\u7684\u51fd\u6570\u548c\u53d8\u91cf<\/strong>\uff0c\u4f8b\u5982\uff1a\u8bbf\u95ee\u547d\u4ee4\u884c\u53c2\u6570\u3001\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u3001\u89e3\u91ca\u5668\u7684\u76f8\u5173\u4fe1\u606f\u7b49\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdsys\u5e93\u7684\u4e00\u4e9b\u5e38\u89c1\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001sys\u6a21\u5757\u7684\u6982\u8ff0<\/h2>\n<\/p>\n<p><p>Python \u7684 sys \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u63a7 Python \u8fd0\u884c\u65f6\u73af\u5883\u7684\u51fd\u6570\u548c\u53d8\u91cf\u3002\u5b83\u5141\u8bb8\u4f60\u4e0e\u89e3\u91ca\u5668\u8fdb\u884c\u4ea4\u4e92\uff0c\u5e76\u83b7\u53d6\u6709\u5173\u89e3\u91ca\u5668\u7684\u5404\u79cd\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>sys.argv<\/strong><\/li>\n<\/ol>\n<p><p>sys.argv \u662f\u4e00\u4e2a\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5217\u8868\u3002\u5b83\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u7684\u540d\u79f0\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u811a\u672c\u540d\u79f0:&quot;, sys.argv[0])<\/p>\n<p>print(&quot;\u53c2\u6570\u5217\u8868:&quot;, sys.argv[1:])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>sys.exit()<\/strong><\/li>\n<\/ol>\n<p><p>sys.exit() \u51fd\u6570\u7528\u4e8e\u7ec8\u6b62\u5f53\u524d\u7684 Python \u89e3\u91ca\u5668\u3002\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u9000\u51fa\u72b6\u6001\u7801\u7ed9\u5b83\uff0c0 \u901a\u5e38\u8868\u793a\u6b63\u5e38\u9000\u51fa\uff0c\u5176\u4ed6\u6570\u503c\u8868\u793a\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if len(sys.argv) &lt; 2:<\/p>\n<p>    print(&quot;\u7f3a\u5c11\u53c2\u6570&quot;)<\/p>\n<p>    sys.exit(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>sys.path<\/strong><\/li>\n<\/ol>\n<p><p>sys.path \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u6307\u793a Python \u89e3\u91ca\u5668\u5230\u54ea\u91cc\u53bb\u5bfb\u627e\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 sys.path \u6765\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;, sys.path)<\/p>\n<p>sys.path.append(&#39;\/my\/custom\/path&#39;)<\/p>\n<p>print(&quot;\u4fee\u6539\u540e\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;, sys.path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>sys.stdin, sys.stdout, sys.stderr<\/strong><\/li>\n<\/ol>\n<p><p>sys.stdin, sys.stdout \u548c sys.stderr \u5206\u522b\u5bf9\u5e94\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u6d41\u3002\u4f60\u53ef\u4ee5\u91cd\u5b9a\u5411\u8fd9\u4e9b\u6d41\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u8f93\u5165\u8f93\u51fa\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = open(&#39;output.txt&#39;, &#39;w&#39;)<\/p>\n<p>print(&quot;\u8fd9\u5c06\u88ab\u5199\u5165\u5230output.txt\u6587\u4ef6\u4e2d&quot;)<\/p>\n<p>sys.stdout.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001sys\u6a21\u5757\u7684\u5e38\u7528\u529f\u80fd\u8be6\u89e3<\/h2>\n<\/p>\n<p><h3>1\u3001\u547d\u4ee4\u884c\u53c2\u6570\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u811a\u672c\u4e2d\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0csys.argv \u63d0\u4f9b\u4e86\u8fd9\u79cd\u529f\u80fd\u3002sys.argv \u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u547d\u4ee4\u884c\u53c2\u6570\u3002\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\uff0c\u540e\u7eed\u5143\u7d20\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u811a\u672c <code>example.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u811a\u672c\u540d\u79f0:&quot;, sys.argv[0])<\/p>\n<p>for i in range(1, len(sys.argv)):<\/p>\n<p>    print(f&quot;\u53c2\u6570 {i}:&quot;, sys.argv[i])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u5e76\u4f20\u9012\u4e00\u4e9b\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python example.py arg1 arg2 arg3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>\u811a\u672c\u540d\u79f0: example.py<\/p>\n<p>\u53c2\u6570 1: arg1<\/p>\n<p>\u53c2\u6570 2: arg2<\/p>\n<p>\u53c2\u6570 3: arg3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u9000\u51fa\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>sys.exit() \u51fd\u6570\u7528\u4e8e\u7ec8\u6b62\u5f53\u524d\u7684 Python \u89e3\u91ca\u5668\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u9519\u8bef\u6216\u7279\u5b9a\u6761\u4ef6\u65f6\u9000\u51fa\u7a0b\u5e8f\u3002\u4f60\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u9000\u51fa\u72b6\u6001\u7801\u30020 \u901a\u5e38\u8868\u793a\u6b63\u5e38\u9000\u51fa\uff0c\u5176\u4ed6\u6570\u503c\u8868\u793a\u5f02\u5e38\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    if len(sys.argv) &lt; 2:<\/p>\n<p>        print(&quot;\u7f3a\u5c11\u53c2\u6570&quot;)<\/p>\n<p>        sys.exit(1)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u53c2\u6570\u5145\u8db3\uff0c\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/h3>\n<\/p>\n<p><p>sys.path \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u6307\u793a Python \u89e3\u91ca\u5668\u5230\u54ea\u91cc\u53bb\u5bfb\u627e\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 sys.path \u6765\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;, sys.path)<\/p>\n<p>sys.path.append(&#39;\/my\/custom\/path&#39;)<\/p>\n<p>print(&quot;\u4fee\u6539\u540e\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84:&quot;, sys.path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u8f93\u5165\u8f93\u51fa\u91cd\u5b9a\u5411<\/h3>\n<\/p>\n<p><p>sys.stdin, sys.stdout \u548c sys.stderr \u5206\u522b\u5bf9\u5e94\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u6d41\u3002\u4f60\u53ef\u4ee5\u91cd\u5b9a\u5411\u8fd9\u4e9b\u6d41\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u8f93\u5165\u8f93\u51fa\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = open(&#39;output.txt&#39;, &#39;w&#39;)<\/p>\n<p>print(&quot;\u8fd9\u5c06\u88ab\u5199\u5165\u5230output.txt\u6587\u4ef6\u4e2d&quot;)<\/p>\n<p>sys.stdout.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u83b7\u53d6Python\u89e3\u91ca\u5668\u76f8\u5173\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>sys\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u83b7\u53d6Python\u89e3\u91ca\u5668\u76f8\u5173\u4fe1\u606f\u7684\u5c5e\u6027\uff0c\u5982sys.version, sys.platform, sys.executable\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Python \u7248\u672c:&quot;, sys.version)<\/p>\n<p>print(&quot;\u64cd\u4f5c\u7cfb\u7edf\u5e73\u53f0:&quot;, sys.platform)<\/p>\n<p>print(&quot;Python \u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84:&quot;, sys.executable)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>sys.exc_info() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\uff0c\u5206\u522b\u662f\u5f02\u5e38\u7c7b\u578b\u3001\u5f02\u5e38\u503c\u548c\u8ffd\u8e2a\u5bf9\u8c61\u3002\u8fd9\u5728\u5f02\u5e38\u5904\u7406\u8fc7\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>try:<\/p>\n<p>    1 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    exc_type, exc_value, exc_traceback = sys.exc_info()<\/p>\n<p>    print(&quot;\u5f02\u5e38\u7c7b\u578b:&quot;, exc_type)<\/p>\n<p>    print(&quot;\u5f02\u5e38\u503c:&quot;, exc_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7\u3001\u5783\u573e\u56de\u6536<\/h3>\n<\/p>\n<p><p>sys.getrefcount() \u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5bf9\u4e8e\u8c03\u8bd5\u548c\u4f18\u5316\u5185\u5b58\u7ba1\u7406\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>a = []<\/p>\n<p>print(&quot;\u5f15\u7528\u8ba1\u6570:&quot;, sys.getrefcount(a))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001sys\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u6355\u83b7\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u6355\u83b7\u7a0b\u5e8f\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002sys\u6a21\u5757\u5141\u8bb8\u4f60\u91cd\u5b9a\u5411\u8fd9\u4e9b\u6d41\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u6355\u83b7\u5b83\u4eec\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import io<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32IO\u5bf9\u8c61<\/strong><\/h2>\n<p>output = io.StringIO()<\/p>\n<h2><strong>\u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = output<\/p>\n<p>print(&quot;\u8fd9\u662f\u4e00\u6761\u6d4b\u8bd5\u6d88\u606f&quot;)<\/p>\n<h2><strong>\u6062\u590d\u6807\u51c6\u8f93\u51fa<\/strong><\/h2>\n<p>sys.stdout = sys.__stdout__<\/p>\n<h2><strong>\u83b7\u53d6\u6355\u83b7\u7684\u8f93\u51fa<\/strong><\/h2>\n<p>captured_output = output.getvalue()<\/p>\n<p>print(&quot;\u6355\u83b7\u7684\u8f93\u51fa:&quot;, captured_output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u52a8\u6001\u52a0\u8f7d\u6a21\u5757<\/h3>\n<\/p>\n<p><p>sys.modules \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u5f53\u524d\u5df2\u52a0\u8f7d\u7684\u6a21\u5757\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u52a8\u6001\u52a0\u8f7d\u6a21\u5757\u6216\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import importlib<\/p>\n<h2><strong>\u52a8\u6001\u52a0\u8f7d\u6a21\u5757<\/strong><\/h2>\n<p>module_name = &#39;math&#39;<\/p>\n<p>if module_name not in sys.modules:<\/p>\n<p>    math_module = importlib.import_module(module_name)<\/p>\n<p>    print(&quot;\u6a21\u5757\u5df2\u52a0\u8f7d:&quot;, math_module)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u6a21\u5757\u5df2\u5b58\u5728:&quot;, sys.modules[module_name])<\/p>\n<h2><strong>\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757<\/strong><\/h2>\n<p>importlib.reload(math_module)<\/p>\n<p>print(&quot;\u6a21\u5757\u5df2\u91cd\u65b0\u52a0\u8f7d:&quot;, math_module)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u672a\u6355\u83b7\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>sys.excepthook \u662f\u4e00\u4e2a\u5904\u7406\u672a\u6355\u83b7\u5f02\u5e38\u7684\u94a9\u5b50\u3002\u4f60\u53ef\u4ee5\u91cd\u5199\u5b83\u4ee5\u81ea\u5b9a\u4e49\u672a\u6355\u83b7\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def custom_excepthook(exc_type, exc_value, exc_traceback):<\/p>\n<p>    print(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406:&quot;)<\/p>\n<p>    print(&quot;\u7c7b\u578b:&quot;, exc_type)<\/p>\n<p>    print(&quot;\u503c:&quot;, exc_value)<\/p>\n<p>    print(&quot;\u8ffd\u8e2a:&quot;, exc_traceback)<\/p>\n<p>sys.excepthook = custom_excepthook<\/p>\n<h2><strong>\u5f15\u53d1\u672a\u6355\u83b7\u5f02\u5e38<\/strong><\/h2>\n<p>raise ValueError(&quot;\u8fd9\u662f\u4e00\u4e2a\u672a\u6355\u83b7\u7684\u5f02\u5e38&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001sys\u6a21\u5757\u7684\u5176\u4ed6\u529f\u80fd<\/h2>\n<\/p>\n<p><h3>1\u3001\u68c0\u67e5Python\u7248\u672c<\/h3>\n<\/p>\n<p><p>sys.version_info \u662f\u4e00\u4e2a\u5305\u542bPython\u7248\u672c\u4fe1\u606f\u7684\u5143\u7ec4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u68c0\u67e5\u5f53\u524d\u8fd0\u884c\u7684Python\u7248\u672c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>if sys.version_info.major == 3 and sys.version_info.minor &gt;= 6:<\/p>\n<p>    print(&quot;Python \u7248\u672c\u7b26\u5408\u8981\u6c42&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u4f7f\u7528Python 3.6\u6216\u66f4\u9ad8\u7248\u672c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u83b7\u53d6\u5f53\u524d\u9012\u5f52\u9650\u5236<\/h3>\n<\/p>\n<p><p>sys.getrecursionlimit() \u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u9012\u5f52\u9650\u5236\uff0csys.setrecursionlimit() \u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e\u65b0\u7684\u9012\u5f52\u9650\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u5f53\u524d\u9012\u5f52\u9650\u5236:&quot;, sys.getrecursionlimit())<\/p>\n<p>sys.setrecursionlimit(2000)<\/p>\n<p>print(&quot;\u65b0\u7684\u9012\u5f52\u9650\u5236:&quot;, sys.getrecursionlimit())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u83b7\u53d6\u5f53\u524d\u6d6e\u70b9\u6570\u7684\u6700\u5927\u503c<\/h3>\n<\/p>\n<p><p>sys.float_info \u662f\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u7cfb\u7edf\u6d6e\u70b9\u6570\u4fe1\u606f\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u6d6e\u70b9\u6570\u6700\u5927\u503c:&quot;, sys.float_info.max)<\/p>\n<p>print(&quot;\u6d6e\u70b9\u6570\u6700\u5c0f\u503c:&quot;, sys.float_info.min)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u68c0\u67e5\u53ef\u7528\u7ebf\u7a0b\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u5f53\u524d\u7cfb\u7edf\u7684\u53ef\u7528\u7ebf\u7a0b\u6570\u3002sys.thread_info \u63d0\u4f9b\u4e86\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u7ebf\u7a0b\u4fe1\u606f:&quot;, sys.thread_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u68c0\u67e5\u5f53\u524d\u5b57\u8282\u5e8f<\/h3>\n<\/p>\n<p><p>sys.byteorder \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u8282\u5e8f\uff08&#39;little&#39; \u6216 &#39;big&#39;\uff09\u3002\u8fd9\u5728\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;\u5f53\u524d\u5b57\u8282\u5e8f:&quot;, sys.byteorder)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u83b7\u53d6Python\u6807\u51c6\u5e93\u8def\u5f84<\/h3>\n<\/p>\n<p><p>sys.prefix \u548c sys.exec_prefix \u8fd4\u56dePython\u6807\u51c6\u5e93\u7684\u5b89\u88c5\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(&quot;Python \u6807\u51c6\u5e93\u8def\u5f84:&quot;, sys.prefix)<\/p>\n<p>print(&quot;Python \u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84:&quot;, sys.exec_prefix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>sys\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u4e0ePython\u89e3\u91ca\u5668\u4ea4\u4e92\u7684\u591a\u79cd\u529f\u80fd\u3002\u901a\u8fc7sys\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3001\u9000\u51fa\u7a0b\u5e8f\u3001\u7ba1\u7406\u6a21\u5757\u641c\u7d22\u8def\u5f84\u3001\u91cd\u5b9a\u5411\u8f93\u5165\u8f93\u51fa\u3001\u83b7\u53d6\u89e3\u91ca\u5668\u76f8\u5173\u4fe1\u606f\u7b49\u3002\u4e86\u89e3\u548c\u638c\u63e1sys\u6a21\u5757\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7f16\u5199\u548c\u8c03\u8bd5Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u867d\u7136<code>sys<\/code>\u5e93\u662fPython\u7684\u5185\u7f6e\u5e93\uff0c\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff0c\u4f46\u5982\u679c\u4f60\u60f3\u5b89\u88c5\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\u3002\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install \u5e93\u540d<\/code>\uff0c\u4f8b\u5982<code>pip install requests<\/code>\uff0c\u5c31\u80fd\u8f7b\u677e\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002<\/p>\n<p><strong>\u5b89\u88c5\u5e93\u65f6\u9047\u5230\u6743\u9650\u95ee\u9898\u8be5\u600e\u4e48\u529e\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5b89\u88c5Python\u5e93\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5982\u679c\u5728\u4f7f\u7528<code>pip<\/code>\u65f6\u9047\u5230\u6743\u9650\u9519\u8bef\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08\u9002\u7528\u4e8eLinux\u548cMac\uff09\uff0c\u6216\u5728Windows\u4e2d\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pip install --user \u5e93\u540d<\/code>\u547d\u4ee4\u5c06\u5e93\u5b89\u88c5\u5230\u7528\u6237\u76ee\u5f55\u4e2d\uff0c\u4ece\u800c\u907f\u514d\u6743\u9650\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u8ba4\u5e93\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u5e93\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\u5bfc\u5165\u8be5\u5e93\u6765\u786e\u8ba4\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>import requests<\/code>\uff08\u5047\u8bbe\u4f60\u5b89\u88c5\u7684\u662frequests\u5e93\uff09\uff0c\u5982\u679c\u6ca1\u6709\u62a5\u9519\u4fe1\u606f\uff0c\u8bf4\u660e\u5e93\u5df2\u6210\u529f\u5b89\u88c5\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip list<\/code>\u67e5\u770b\u5df2\u5b89\u88c5\u7684\u5e93\u53ca\u5176\u7248\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4e0d\u9700\u8981\u5b89\u88c5\u5e93sys\uff0c\u56e0\u4e3asys\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002sys\u5e93\u5305\u542b\u4e86\u4e00\u4e9b\u7528\u4e8e\u4e0ePython\u89e3 [&hellip;]","protected":false},"author":3,"featured_media":1152618,"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\/1152615"}],"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=1152615"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152615\/revisions"}],"predecessor-version":[{"id":1152620,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152615\/revisions\/1152620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152618"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}