Python3 Pycharm

PyCharm 的安装、首次配置、创建项目、运行调试与常用快捷键。

Beginner
Key PointsPyCharm 社区版免费够用;新建项目时选对解释器(建议 venv);运行用 Shift+F10、调试用 Shift+F9。

安装 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.

Python
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)
Output
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.

Python
def main():
    print("Hello from PyCharm")
    print("sum:", sum(range(5)))

if __name__ == '__main__':
    main()
Output
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.

ActionShortcut
RunShift + F10
DebugShift + F9
Step overF8
Step intoF7
Toggle breakpointCtrl + F8
Reformat codeCtrl + Alt + L
Python
def total(items):
    s = 0
    for i in items:
        s += i
    return s

print(total([1, 2, 3, 4]))
Output
10

Friends & Partners