PyCharm Run and Debug
PyCharm provides powerful run and debug tools that help developers execute Python code efficiently and troubleshoot problems. This section details environment configuration, script execution, and debugging techniques.
Configure the runtime environment
Set the Python interpreter
**Open interpreter settings:** Settings -> Project -> Python Interpreter (Windows/Linux: Ctrl+Alt+S, Mac: Cmd+,).
Choose the interpreter:
-
Virtual environment(recommended):
Virtualenv/Conda/Pipenv -
System interpreter: use the installed Python directly
-
Remote interpreter: connect to a server or Docker environment

Install dependency packages:
-
On the interpreter screen, click
+Search for and install packages (e.g.,requests) -
or run via the terminal
pip install package_name

Configure run parameters
Open run configuration:
-
Top toolbar ->
运行 → 编辑配置(Run → Edit Configurations) -
or click the configuration drop-down next to the run button

**Add a Python configuration:** click + -> select Python

Key parameters:
-
Script path: select the
.pyfile -
Parameters: enter command-line arguments (e.g.,
--port 8000) -
Working directory: set the current directory when the script runs
-
Environment Variables: add custom environment variables (e.g.,
PYTHONPATH)

**Save the configuration:** click Apply -> OK.
Run a Python script
Run a single file
Method 1: right-click to run:
-
In the editor, right-click ->
运行 '文件名'(Run 'filename')
Method 2: run from the toolbar:
-
Click the green Run button on the top toolbar
Method 3: run with a shortcut:
-
Shift + F10(run the current file) -
Ctrl + Shift + F10(run the file currently being edited)

Run a module (e.g., Flask/Django)
**Configure module run:** choose the module name instead of a script path in the run configuration.
For example:
-
Module name:
flask -
Parameters:
run --port=5000
Run a module with parameters:
# Example running Django development server
python manage.py runserver 0.0.0.0:8000
In the run configuration's "Parameters" field, enter: runserver 0.0.0.0:8000

Debug code
Set breakpoints
Add a breakpoint:
-
Click in the left margin of the line number (a red dot appears)
-
Right-click a breakpoint to set a condition (e.g.,
x > 10)
Breakpoint types:
-
Line breakpoint: pause at the specified line
-
Exception breakpoint: pause when an exception is thrown (
运行 → 查看断点 → 添加 Python 异常断点)
Start debugging:
-
Click the
🐞 调试button -
Or shortcut
Shift + F9

Variable watches
View variables:
-
After debugging starts, in the
变量(Variables)panel, view the variable values in the current scope -
Double-click a variable to edit its value (for temporary testing)
Add a watch:
-
in
监视(Watches)In the panel, click+ -
Enter a variable name or expression (e.g.,
len(items))
Debug control operations
| Operation | Shortcut (Win/Linux) | Shortcut (Mac) | Description |
|---|---|---|---|
| Step Over | F8 |
F8 |
Execute the current line without entering the function |
| Step Into | F7 |
F7 |
Enter the function to debug inside it |
| Step Out | Shift + F8 |
⇧ + F8 |
Execute the rest of the current function and return to the caller |
| Resume | F9 |
⌘ + F8 |
Continue running until the next breakpoint |
| Stop | Ctrl + F2 |
⌘ + F2 |
Terminate the debug session |
Debug console
Interactive debugging:
-
During debugging, you can use the
调试控制台to execute Python code directly -
For example: call a function temporarily or modify a variable
Evaluate expression:
-
Select code → right click →
评估表达式(Evaluate Expression) -
Or shortcut
Alt + F8
Advanced debugging techniques
Remote debugging
Configure a remote interpreter:
-
文件 → 设置 → Python 解释器 → 添加远程解释器 -
Supports SSH, Docker, and Vagrant
Debug remote code:
-
Breakpoints and debug operations are the same as in local debugging
Multi-process debugging
Child process debugging:
-
Check the option in the run configuration
在子进程中调试(Debug child processes) -
Suitable for
multiprocessingorsubprocessScenario
Attach to process:
-
运行 → 附加到本地进程(Attach to Local Process) -
Select a running Python process
Common Problem Solutions
Q1: Breakpoints not working?
-
Check whether the interpreter matches the project environment
-
Make sure the code is saved (
Ctrl + S) -
Check whether the breakpoint is disabled (the breakpoint icon is gray)
Q2: How do I debug unit tests?
-
In the test file, right-click ->
调试 '测试名' -
Or use
pytest/unittestDedicated run configuration
Q3: Variables show "Unknown" during debugging?
-
Optimize the code structure and avoid overly complex expressions
-
Use
WatchesAdd watch variables manually
Debug shortcuts
| Scenario | Operation |
|---|---|
| Quickly run the current file | Shift + F10 |
| Start debugging | Shift + F9 |
| Conditional breakpoint | Right-click a breakpoint -> set condition |
| View variable values | During debugging, in the Variables panel |
| Execute code temporarily | Enter commands in the debug console |
| Multi-process debugging | Check the option in the run configuration Debug child processes |