Python3 Pycharm
PyCharm 的安装、首次配置、创建项目、运行调试与常用快捷键。
安装 Python
PyCharm is only an editor; the Python interpreter actually runs the code, so install Python first. Then point PyCharm at that interpreter — no extra setup needed.
import sys
print("has executable:", bool(sys.executable))
print("major version:", sys.version_info.major)
print("can import os:", 'os' in sys.stdlib_module_names)has executable: True
major version: 3
can import os: True安装 PyCharm
Download from JetBrains: the free Community edition covers pure Python, while Professional adds web frameworks like Django/Flask. On Windows, tick "Add launchers dir to the PATH".
配置 PyCharm
First launch asks about importing settings, then theme and plugins. Afterwards set Settings → Project → Python Interpreter. Create a separate venv per project so dependencies do not collide.
- Settings → Project → Python Interpreter — pick the interpreter
- Settings → Editor → Font — set a monospace font with ligatures
- Settings → Tools → Terminal — verify the shell works
创建第一个项目
In New Project choose Pure Python, set Location, and pick New environment using Virtualenv. Then right-click the folder → New → Python File to start coding.
def main():
print("Hello from PyCharm")
print("sum:", sum(range(5)))
if __name__ == '__main__':
main()Hello from PyCharm
sum: 10运行与调试
Run with Shift+F10 (Ctrl+R on macOS) and debug with Shift+F9. Click the gutter to set a breakpoint; the debugger shows variables, steps through code and reveals the call stack — far better than scattering print calls.
| Action | Shortcut |
|---|---|
| Run | Shift + F10 |
| Debug | Shift + F9 |
| Step over | F8 |
| Step into | F7 |
| Toggle breakpoint | Ctrl + F8 |
| Reformat code | Ctrl + Alt + L |
def total(items):
s = 0
for i in items:
s += i
return s
print(total([1, 2, 3, 4]))10安装 Python
PyCharm 只是编辑器,真正执行代码的是 Python 解释器,所以要先装 Python。装好后在 PyCharm 里指定解释器路径即可,无需额外配置。
import sys
print("has executable:", bool(sys.executable))
print("major version:", sys.version_info.major)
print("can import os:", 'os' in sys.stdlib_module_names)has executable: True
major version: 3
can import os: True安装 PyCharm
从 JetBrains 官网下载,社区版(Community)免费且支持纯 Python 开发,专业版额外支持 Django/Flask 等 Web 框架。Windows 安装时建议勾选「Add launchers dir to the PATH」。
配置 PyCharm
首次启动会提示导入旧设置、选择主题和插件。装完在 Settings → Project → Python Interpreter 里指定解释器;建议为每个项目单独建 venv,避免依赖互相污染。
- Settings → Project → Python Interpreter —— 指定解释器
- Settings → Editor → Font —— 设置等宽字体
- Settings → Tools → Terminal —— 确认终端可用
创建第一个项目
New Project 时选 Pure Python,Location 填目录,Interpreter 选 New environment using Virtualenv。项目建好后右键目录 → New → Python File 即可写代码。
def main():
print("Hello from PyCharm")
print("sum:", sum(range(5)))
if __name__ == '__main__':
main()Hello from PyCharm
sum: 10运行与调试
运行用 Shift+F10(macOS 是 Ctrl+R),调试用 Shift+F9。在行号旁点一下设断点,调试时能看变量、单步执行、看调用栈 —— 比到处插 print 高效得多。
| 操作 | 快捷键 |
|---|---|
| 运行 | Shift + F10 |
| 调试 | Shift + F9 |
| 单步跳过 | F8 |
| 单步进入 | F7 |
| 切换断点 | Ctrl + F8 |
| 格式化代码 | Ctrl + Alt + L |
def total(items):
s = 0
for i in items:
s += i
return s
print(total([1, 2, 3, 4]))10