Python3 环境搭建
Python3 下载安装、环境变量配置、运行 Python 与常用配套工具。
Python3 下载
Get the installer for your platform from python.org's Downloads page: 64-bit installer on Windows, universal2 on macOS; on Linux the package manager usually provides it. Pick the current stable release, not a preview.
- Windows — python-3.x.x-amd64.exe
- macOS — python-3.x.x-macos11.pkg
- Linux — apt / dnf / pacman install python3
Python 安装
On the first screen of the Windows installer, tick "Add Python to PATH" — otherwise the shell cannot find python. Under Customize you can also enable pip and tcl/tk.
import sys
import os
import platform
print("executable:", os.path.basename(sys.executable))
print("platform:", platform.system())
print("prefix set:", bool(sys.prefix))executable: python.exe
platform: Windows
prefix set: True环境变量配置
PATH must contain both the Python install directory and its Scripts directory: the first gives you python, the second pip. Verify in a new terminal — an already-open one keeps the old environment.
import os
import sys
paths = os.environ.get('PATH', '').split(os.pathsep)
has_python = any('python' in p.lower() for p in paths)
print("path entries:", len(paths) > 0)
print("python in PATH:", has_python)
print("sys.path entries:", len(sys.path) > 0)path entries: True
python in PATH: True
sys.path entries: TruePATH lets the shell find executables; sys.path lets import find modules. They are unrelated — do not edit PATH to fix an import error.运行 Python
Three ways to run: interactive python, script python hello.py, module python -m http.server. -c executes a string directly — handy for quick checks.
import subprocess
import sys
r1 = subprocess.run([sys.executable, '-c', 'print(1 + 1)'],
capture_output=True, text=True)
print("stdout:", r1.stdout.strip())
print("returncode:", r1.returncode)
r2 = subprocess.run([sys.executable, '-c', 'raise SystemExit(3)'],
capture_output=True, text=True)
print("exit on error:", r2.returncode)stdout: 2
returncode: 0
exit on error: 3更多必备工具
After the interpreter you need three things: pip for packages, venv for per-project isolation, and pipx for command-line tools. For editors, VS Code and PyCharm both have first-class Python support.
import sys
import ensurepip
print("pip available:", ensurepip.version() != '')
print("venv available:", 'venv' in sys.stdlib_module_names)
print("ensurepip parsed:", ensurepip.version().count('.') >= 1)pip available: True
venv available: True
ensurepip parsed: TruePython3 下载
从 python.org 的 Downloads 页取对应平台的安装包。Windows 用 64-bit installer,macOS 用 universal2 installer,Linux 一般由包管理器提供。选当前稳定版而非最新预览版。
- Windows —— python-3.x.x-amd64.exe
- macOS —— python-3.x.x-macos11.pkg
- Linux —— apt / dnf / pacman 安装 python3
Python 安装
Windows 安装向导第一屏有一个关键的勾选项「Add Python to PATH」,必须勾上,否则命令行找不到 python。选 Customize 时可以顺便勾选 pip 和 tcl/tk。
import sys
import os
import platform
print("executable:", os.path.basename(sys.executable))
print("platform:", platform.system())
print("prefix set:", bool(sys.prefix))executable: python.exe
platform: Windows
prefix set: True环境变量配置
PATH 里要有 Python 的安装目录和 Scripts 目录:前者让你能敲 python,后者让你能敲 pip。装完在新开的终端里验证,旧终端不会自动刷新环境变量。
import os
import sys
paths = os.environ.get('PATH', '').split(os.pathsep)
has_python = any('python' in p.lower() for p in paths)
print("path entries:", len(paths) > 0)
print("python in PATH:", has_python)
print("sys.path entries:", len(sys.path) > 0)path entries: True
python in PATH: True
sys.path entries: TruePATH 是给命令行找可执行文件用的,sys.path 是给 import 找模块用的,两者互不相干。模块导入失败不要改 PATH。运行 Python
三种运行方式:交互式 python、脚本 python hello.py、模块 python -m http.server。-c 可以直接执行一段字符串,适合快速验证。
import subprocess
import sys
r1 = subprocess.run([sys.executable, '-c', 'print(1 + 1)'],
capture_output=True, text=True)
print("stdout:", r1.stdout.strip())
print("returncode:", r1.returncode)
r2 = subprocess.run([sys.executable, '-c', 'raise SystemExit(3)'],
capture_output=True, text=True)
print("exit on error:", r2.returncode)stdout: 2
returncode: 0
exit on error: 3更多必备工具
装好解释器后还需要三件套:pip 管第三方包、venv 隔离项目环境、pipx 装命令行工具。编辑器推荐 VS Code 或 PyCharm,两者都有官方 Python 支持。
import sys
import ensurepip
print("pip available:", ensurepip.version() != '')
print("venv available:", 'venv' in sys.stdlib_module_names)
print("ensurepip parsed:", ensurepip.version().count('.') >= 1)pip available: True
venv available: True
ensurepip parsed: True