{"id":1172608,"date":"2025-01-15T16:51:46","date_gmt":"2025-01-15T08:51:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172608.html"},"modified":"2025-01-15T16:51:48","modified_gmt":"2025-01-15T08:51:48","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%96%87%e6%9c%ac%e4%b8%ad%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172608.html","title":{"rendered":"python\u5982\u4f55\u5728\u6587\u672c\u4e2d\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074636\/cc5c4977-9430-49f2-b35b-52aba299e51c.webp\" alt=\"python\u5982\u4f55\u5728\u6587\u672c\u4e2d\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fd0\u884c\u6587\u672c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528exec()\u51fd\u6570\u3001\u4f7f\u7528eval()\u51fd\u6570\u3001\u4f7f\u7528subprocess\u6a21\u5757\u3001\u4f7f\u7528Jupyter Notebook\u3001\u4f7f\u7528Python Shell\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528exec()\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>exec()\u51fd\u6570<\/strong> \u662fPython\u5185\u7f6e\u51fd\u6570\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u6267\u884c\u50a8\u5b58\u5728\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u4e2d\u7684Python\u4ee3\u7801\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u6587\u672c\u4e2d\u8fd0\u884cPython\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>code = &quot;&quot;&quot;<\/p>\n<p>def hello_world():<\/p>\n<p>    print(&#39;Hello, world!&#39;)<\/p>\n<p>hello_world()<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u5b57\u7b26\u4e32<code>code<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6765\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\u3002\u6267\u884c\u7ed3\u679c\u5c06\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u201cHello, world!\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528exec()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>exec()<\/code>\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u50a8\u5b58\u5728\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u4e2d\u7684Python\u4ee3\u7801\uff0c\u8fd9\u5728\u9700\u8981\u52a8\u6001\u6267\u884c\u4ee3\u7801\u65f6\u7279\u522b\u6709\u7528\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">exec(object[, globals[, locals]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>object<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u6709\u6548Python\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\u6216\u4ee3\u7801\u5bf9\u8c61\u3002<\/li>\n<li><code>globals<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<li><code>locals<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">code = &quot;&quot;&quot;<\/p>\n<p>x = 10<\/p>\n<p>y = 20<\/p>\n<p>print(x + y)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>exec()<\/code>\u51fd\u6570\u6267\u884c\u4e86<code>code<\/code>\u5b57\u7b26\u4e32\u4e2d\u7684\u4ee3\u7801\uff0c\u5e76\u8f93\u51fa\u4e86\u7ed3\u679c30\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>exec()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5728\u811a\u672c\u7684\u9876\u5c42\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u8fd9\u53ef\u4ee5\u8ba9\u4f60\u52a8\u6001\u5730\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dynamic_exec(code_str):<\/p>\n<p>    exec(code_str)<\/p>\n<p>code = &quot;&quot;&quot;<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&#39;Hello, {name}!&#39;)<\/p>\n<p>greet(&#39;Alice&#39;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>dynamic_exec(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>dynamic_exec<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u4ee3\u7801\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6267\u884c\u8be5\u4ee3\u7801\u3002\u7ed3\u679c\u5c06\u4f1a\u8f93\u51fa\u201cHello, Alice!\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>eval()<\/code>\u51fd\u6570\u7528\u4e8e\u6267\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u5e76\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">eval(expression[, globals[, locals]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>expression<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u4e00\u4e2a\u6709\u6548\u7684Python\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><code>globals<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<li><code>locals<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<p><p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">expr = &quot;2 + 3 * 4&quot;<\/p>\n<p>result = eval(expr)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>eval()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86\u8868\u8fbe\u5f0f<code>expr<\/code>\u7684\u503c\uff0c\u5e76\u8f93\u51fa\u4e86\u7ed3\u679c14\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>eval()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dynamic_eval(expr):<\/p>\n<p>    return eval(expr)<\/p>\n<p>expression = &quot;10 \/ 2&quot;<\/p>\n<p>result = dynamic_eval(expression)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>dynamic_eval<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>eval()<\/code>\u51fd\u6570\u8ba1\u7b97\u8be5\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u7ed3\u679c\u5c06\u4f1a\u8f93\u51fa5.0\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5b50\u8fdb\u7a0b\uff0c\u5e76\u5141\u8bb8\u4f60\u4e0e\u5b83\u4eec\u8fdb\u884c\u901a\u4fe1\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u6765\u6267\u884c\u548c\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>code = &quot;&quot;&quot;<\/p>\n<p>print(&#39;Hello from subprocess!&#39;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>process = subprocess.Popen([&#39;python&#39;, &#39;-c&#39;, code], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>stdout, stderr = process.communicate()<\/p>\n<p>print(stdout.decode())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>subprocess.Popen<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\uff0c\u5e76\u6267\u884cPython\u4ee3\u7801\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u4f1a\u662f\u201cHello from subprocess!\u201d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def run_code_in_subprocess(code_str):<\/p>\n<p>    process = subprocess.Popen([&#39;python&#39;, &#39;-c&#39;, code_str], stdout=subprocess.PIPE, stderr=subprocess.PIPE)<\/p>\n<p>    stdout, stderr = process.communicate()<\/p>\n<p>    return stdout.decode(), stderr.decode()<\/p>\n<p>code = &quot;&quot;&quot;<\/p>\n<p>print(&#39;Running in subprocess!&#39;)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>stdout, stderr = run_code_in_subprocess(code)<\/p>\n<p>print(stdout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>run_code_in_subprocess<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u4ee3\u7801\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u8be5\u4ee3\u7801\u3002\u7ed3\u679c\u5c06\u4f1a\u8f93\u51fa\u201cRunning in subprocess!\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Jupyter Notebook<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u8ba1\u7b97\u73af\u5883\uff0c\u5141\u8bb8\u4f60\u521b\u5efa\u548c\u5171\u4eab\u5305\u542b\u4ee3\u7801\u3001\u65b9\u7a0b\u3001\u53ef\u89c6\u5316\u548c\u6587\u672c\u7684\u6587\u6863\u3002\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u636e\u6e05\u7406\u548c\u8f6c\u6362\u3001\u6570\u503c\u6a21\u62df\u3001\u7edf\u8ba1\u5efa\u6a21\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u5355\u5143\u683c\u4e2d\u8f93\u5165\u548c\u8fd0\u884cPython\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&#39;Hello, {name}!&#39;<\/p>\n<p>print(greet(&#39;Jupyter&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728Jupyter Notebook\u5355\u5143\u683c\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>greet<\/code>\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u5b83\u6765\u8f93\u51fa\u201cHello, Jupyter!\u201d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u9b54\u6cd5\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u8fd8\u652f\u6301\u4e00\u4e9b\u7279\u6b8a\u7684\u9b54\u6cd5\u547d\u4ee4\uff0c\u53ef\u4ee5\u7528\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u3001\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u4ee3\u7801\u6267\u884c\u65f6\u95f4<\/p>\n<p>%timeit [x2 for x in range(1000)]<\/p>\n<h2><strong>\u6267\u884c\u7cfb\u7edf\u547d\u4ee4<\/strong><\/h2>\n<p>!ls<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>%timeit<\/code>\u9b54\u6cd5\u547d\u4ee4\u7528\u4e8e\u6d4b\u91cf\u5217\u8868\u751f\u6210\u5f0f\u7684\u6267\u884c\u65f6\u95f4\uff0c\u800c<code>!ls<\/code>\u547d\u4ee4\u7528\u4e8e\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Python Shell<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Python Shell\uff08\u4e5f\u79f0\u4e3aREPL\uff0cRead-Eval-Print Loop\uff09\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684Python\u89e3\u91ca\u5668\uff0c\u5141\u8bb8\u4f60\u9010\u884c\u8f93\u5165\u548c\u6267\u884cPython\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python<\/code>\u547d\u4ee4\u6765\u542f\u52a8Python Shell\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">$ python<\/p>\n<p>Python 3.8.5 (default, Jul 28 2020, 12:59:40)<\/p>\n<p>[GCC 9.3.0] on linux<\/p>\n<p>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<\/p>\n<p>&gt;&gt;&gt; print(&#39;Hello from Python Shell!&#39;)<\/p>\n<p>Hello from Python Shell!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728Python Shell\u4e2d\u8f93\u5165\u5e76\u6267\u884c\u4e86<code>print<\/code>\u8bed\u53e5\uff0c\u8f93\u51fa\u4e86\u201cHello from Python Shell!\u201d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6267\u884c\u591a\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Python Shell\u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8f93\u5165\u5e76\u6267\u884c\u591a\u884c\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&gt;&gt;&gt; def greet(name):<\/p>\n<p>...     return f&#39;Hello, {name}!&#39;<\/p>\n<p>...<\/p>\n<p>&gt;&gt;&gt; print(greet(&#39;Python Shell&#39;))<\/p>\n<p>Hello, Python Shell!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728Python Shell\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>greet<\/code>\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u5b83\u6765\u8f93\u51fa\u201cHello, Python Shell!\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u591a\u79cd\u5728Python\u4e2d\u8fd0\u884c\u6587\u672c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u3001<code>eval()<\/code>\u51fd\u6570\u3001<code>subprocess<\/code>\u6a21\u5757\u3001Jupyter Notebook\u548cPython Shell\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u6267\u884c\u548c\u7ba1\u7406Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u5404\u79cd\u73af\u5883\u4e2d\u52a8\u6001\u5730\u6267\u884cPython\u4ee3\u7801\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c<code>exec()<\/code>\u548c<code>eval()<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u7528\u4e8e\u52a8\u6001\u6267\u884c\u4ee3\u7801\u7247\u6bb5\uff0c\u800c<code>subprocess<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u6765\u7ba1\u7406\u5b50\u8fdb\u7a0b\u3002Jupyter Notebook\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002Python Shell\u5219\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u9010\u884c\u8f93\u5165\u548c\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5730\u8fd0\u884c\u548c\u7ba1\u7406Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u6587\u672c\u6587\u4ef6\u4e2d\u6267\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u8981\u5728\u6587\u672c\u6587\u4ef6\u4e2d\u6267\u884cPython\u4ee3\u7801\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u6709\u6548\u7684Python\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u6765\u6267\u884c\u6587\u672c\u4e2d\u7684\u4ee3\u7801\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;your_file.txt&#39;, &#39;r&#39;) as file:\n    code = file.read()\n    exec(code)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u6587\u672c\u6587\u4ef6\u4e2d\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\uff0c\u5426\u5219\u6267\u884c\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u548c\u5904\u7406\u6587\u672c\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002\u8bfb\u53d6\u540e\uff0c\u53ef\u4ee5\u7528\u5404\u79cd\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u6765\u5206\u6790\u6570\u636e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;data.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())  # \u53bb\u6389\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\n<\/code><\/pre>\n<p>\u5904\u7406\u540e\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u5e76\u8fd0\u884cPython\u4ee3\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528<code>exec()<\/code>\u6216<code>eval()<\/code>\u51fd\u6570\u52a8\u6001\u751f\u6210\u5e76\u6267\u884cPython\u4ee3\u7801\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">code = &quot;print(&#39;Hello, World!&#39;)&quot;\nexec(code)\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u65f6\u8981\u5c0f\u5fc3\u4ee3\u7801\u6ce8\u5165\u7684\u98ce\u9669\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u4ee3\u7801\u662f\u5b89\u5168\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fd0\u884c\u6587\u672c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528exec()\u51fd\u6570\u3001\u4f7f\u7528eval()\u51fd\u6570\u3001\u4f7f\u7528subproces [&hellip;]","protected":false},"author":3,"featured_media":1172614,"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\/1172608"}],"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=1172608"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172608\/revisions"}],"predecessor-version":[{"id":1172615,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172608\/revisions\/1172615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172614"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}