{"id":2720,"date":"2025-03-14T21:53:36","date_gmt":"2025-03-14T13:53:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2720"},"modified":"2025-03-14T21:53:36","modified_gmt":"2025-03-14T13:53:36","slug":"python-3-%e8%a7%a3%e9%87%8a%e5%99%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-3-%e8%a7%a3%e9%87%8a%e5%99%a8%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python 3 \u89e3\u91ca\u5668\u8be6\u89e3"},"content":{"rendered":"\n<p>Python 3 \u662f\u4e00\u79cd<strong>\u89e3\u91ca\u578b<\/strong>\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740 Python \u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab<strong>\u9010\u884c\u7ffb\u8bd1\u6210\u673a\u5668\u7801<\/strong>\uff0c\u800c\u4e0d\u662f\u50cf C\/C++ \u8fd9\u6837\u7684\u7f16\u8bd1\u578b\u8bed\u8a00\u90a3\u6837\u5148\u7f16\u8bd1\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\u518d\u6267\u884c\u3002\u56e0\u6b64\uff0cPython \u4ee3\u7801\u7684\u6267\u884c\u9700\u8981<strong>Python \u89e3\u91ca\u5668\uff08Python Interpreter\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Python 3 \u89e3\u91ca\u5668\u7684\u4f5c\u7528<\/strong><\/h2>\n\n\n\n<p>Python \u89e3\u91ca\u5668\u7684\u4e3b\u8981\u4efb\u52a1\u662f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bfb\u53d6<\/strong>\uff08Read\uff09\uff1a\u89e3\u6790 Python \u4ee3\u7801\uff0c\u5e76\u8f6c\u6362\u4e3a<strong>\u5b57\u8282\u7801<\/strong>\uff08Bytecode\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7f16\u8bd1<\/strong>\uff08Compile\uff09\uff1a\u5c06 Python \u4ee3\u7801\u8f6c\u6362\u4e3a\u4e00\u79cd\u4e2d\u95f4\u5f62\u5f0f\uff08<code>.pyc<\/code> \u6587\u4ef6\uff09\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u91ca\u6267\u884c<\/strong>\uff08Execute\uff09\uff1a\u5c06\u5b57\u8282\u7801\u4ea4\u7ed9 PVM\uff08Python Virtual Machine\uff0cPython \u865a\u62df\u673a\uff09\u6267\u884c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0cPython \u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python \u6e90\u7801\uff08.py \u6587\u4ef6\uff09 \u2192 \u5b57\u8282\u7801\uff08.pyc \u6587\u4ef6\uff09 \u2192 PVM \u89e3\u91ca\u6267\u884c<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5982\u4f55\u67e5\u770b Python 3 \u89e3\u91ca\u5668<\/strong><\/h2>\n\n\n\n<p>\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python --version<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 --version<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python 3.11.2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Python 3 \u89e3\u91ca\u5668\u7684\u4e0d\u540c\u5b9e\u73b0<\/strong><\/h2>\n\n\n\n<p>Python \u89e3\u91ca\u5668\u6709\u591a\u4e2a\u4e0d\u540c\u7684\u5b9e\u73b0\uff0c\u6bcf\u79cd\u5b9e\u73b0\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u89e3\u91ca\u5668<\/th><th>\u4ecb\u7ecd<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><strong>CPython<\/strong><\/td><td>\u5b98\u65b9\u9ed8\u8ba4\u5b9e\u73b0\uff0c\u57fa\u4e8e C \u8bed\u8a00\uff0c\u6700\u5e38\u89c1<\/td><td>\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f<\/td><\/tr><tr><td><strong>PyPy<\/strong><\/td><td>\u4f7f\u7528 JIT\uff08\u5373\u65f6\u7f16\u8bd1\uff09\uff0c\u8fd0\u884c\u901f\u5ea6\u66f4\u5feb<\/td><td>\u9700\u8981\u66f4\u9ad8\u6027\u80fd<\/td><\/tr><tr><td><strong>Jython<\/strong><\/td><td>\u8fd0\u884c\u5728 JVM\uff08Java \u865a\u62df\u673a\uff09\u4e0a\u7684 Python<\/td><td>\u9700\u8981\u548c Java \u4ea4\u4e92<\/td><\/tr><tr><td><strong>IronPython<\/strong><\/td><td>\u8fd0\u884c\u5728 .NET \u5e73\u53f0\u4e0a\u7684 Python<\/td><td>\u9700\u8981\u548c C#\/.NET \u4ea4\u4e92<\/td><\/tr><tr><td><strong>MicroPython<\/strong><\/td><td>\u9002\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf<\/td><td>\u7269\u8054\u7f51\u3001\u5d4c\u5165\u5f0f\u5f00\u53d1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u901a\u5e38\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f <strong>CPython<\/strong> \u89e3\u91ca\u5668\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5982\u4f55\u4f7f\u7528 Python 3 \u89e3\u91ca\u5668<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u76f4\u63a5\u5728\u7ec8\u7aef\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python<\/code><\/pre>\n\n\n\n<p>\u8fdb\u5165 Python \u4ea4\u4e92\u6a21\u5f0f\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; print(\"Hello, Python!\")\nHello, Python!<\/code><\/pre>\n\n\n\n<p>\u8981\u9000\u51fa\u4ea4\u4e92\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; exit()<\/code><\/pre>\n\n\n\n<p>\u6216\u6309 <code>Ctrl + D<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u8fd0\u884c Python \u811a\u672c<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Python \u6587\u4ef6\uff08\u4f8b\u5982 <code>hello.py<\/code>\uff09\uff0c\u5199\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello, Python!\")<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u7ec8\u7aef\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 hello.py<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python hello.py<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Python 3 \u89e3\u91ca\u5668\u7684\u8def\u5f84<\/strong><\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b Python \u89e3\u91ca\u5668\u7684\u8def\u5f84\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>which python3  # Linux\/macOS\nwhere python   # Windows<\/code><\/pre>\n\n\n\n<p>\u901a\u5e38\uff0c\u5b83\u4f1a\u8fd4\u56de\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/bin\/python3<\/code><\/pre>\n\n\n\n<p>\u5728 Python \u4ea4\u4e92\u6a21\u5f0f\u4e0b\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nprint(sys.executable)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u5207\u6362 Python \u89e3\u91ca\u5668<\/strong><\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u5b89\u88c5\u4e86\u591a\u4e2a Python \u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5207\u6362\u89e3\u91ca\u5668\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u4f7f\u7528 <code>python<\/code> \u547d\u4ee4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>python3.9 hello.py  # \u4f7f\u7528 Python 3.9 \u8fd0\u884c\npython3.11 hello.py  # \u4f7f\u7528 Python 3.11 \u8fd0\u884c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4f7f\u7528 <code>virtualenv<\/code> \u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u4e00\u4e2a Python 3.10 \u7684\u865a\u62df\u73af\u5883\npython3.10 -m venv myenv\nsource myenv\/bin\/activate  # Linux\/macOS\nmyenv\\Scripts\\activate  # Windows<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7cfb\u7edf\u4e0a\u4f7f\u7528\u4e0d\u540c\u7684 Python \u7248\u672c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. Python 3 \u89e3\u91ca\u5668\u7684\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u68c0\u67e5 <code>PYTHONPATH<\/code><\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo $PYTHONPATH  # Linux\/macOS\necho %PYTHONPATH%  # Windows<\/code><\/pre>\n\n\n\n<p><code>PYTHONPATH<\/code> \u5f71\u54cd Python \u89e3\u91ca\u5668\u641c\u7d22\u6a21\u5757\u7684\u8def\u5f84\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4fee\u6539\u73af\u5883\u53d8\u91cf<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u5b89\u88c5\u4e86\u591a\u4e2a Python \u7248\u672c\uff0c\u53ef\u4ee5\u624b\u52a8\u4fee\u6539 <code>PATH<\/code> \u53d8\u91cf\uff0c\u8ba9 Python \u89e3\u91ca\u5668\u6307\u5411\u4f60\u5e0c\u671b\u7684\u7248\u672c\u3002<\/p>\n\n\n\n<p>\u5728 <strong>Linux\/macOS<\/strong>\uff08\u4fee\u6539 <code>~\/.bashrc<\/code> \u6216 <code>~\/.zshrc<\/code>\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export PATH=\"\/usr\/local\/bin\/python3.11:$PATH\"<\/code><\/pre>\n\n\n\n<p>\u5728 <strong>Windows<\/strong>\uff08\u4fee\u6539 <code>\u73af\u5883\u53d8\u91cf -&gt; \u7cfb\u7edf\u53d8\u91cf -&gt; Path<\/code>\uff09\uff0c\u6dfb\u52a0 Python \u89e3\u91ca\u5668\u6240\u5728\u76ee\u5f55\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Python311\\<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Python 3 \u89e3\u91ca\u5668\u662f\u8fd0\u884c Python \u4ee3\u7801\u7684\u6838\u5fc3\u5de5\u5177<\/strong>\uff0c\u9ed8\u8ba4\u4f7f\u7528\u7684\u662f <strong>CPython<\/strong> \u5b9e\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u4ea4\u4e92\u6a21\u5f0f\u548c\u811a\u672c\u6a21\u5f0f<\/strong>\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u8fd0\u884c Python \u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>Python \u89e3\u91ca\u5668\u6709\u591a\u4e2a\u7248\u672c<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>python --version<\/code> \u68c0\u67e5\u5f53\u524d\u7248\u672c\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u4e0d\u540c\u7684\u89e3\u91ca\u5668\u5b9e\u73b0<\/strong>\uff0c\u5982 PyPy\u3001Jython\u3001IronPython \u7b49\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u4ee5\u4f7f\u7528 <code>virtualenv<\/code> \u6765\u7ba1\u7406\u4e0d\u540c\u7684 Python \u7248\u672c<\/strong>\uff0c\u907f\u514d\u51b2\u7a81\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3 \u662f\u4e00\u79cd\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u8fd9\u610f\u5473\u7740 Python \u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u9010\u884c\u7ffb\u8bd1\u6210\u673a\u5668\u7801\uff0c\u800c\u4e0d\u662f\u50cf C\/C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2721,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2720"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2720\/revisions"}],"predecessor-version":[{"id":2722,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2720\/revisions\/2722"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2721"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}