YouTip LogoYouTip

Pycharm Run Debug

PyCharm provides powerful running and debugging tools to help developers efficiently execute and troubleshoot Python code issues. This section will provide a detailed introduction to environment configuration, script execution, and debugging techniques. * * * ## Configuring the Runtime Environment ### Setting Up the Python Interpreter **Opening Interpreter Settings:** Settings β†’ Project β†’ Python Interpreter (Windows/Linux: Ctrl+Alt+S, Mac: ⌘,). **Selecting the Interpreter:** * **Virtual Environment** (Recommended): `Virtualenv` / `Conda` / `Pipenv` * **System Interpreter**: Directly use the installed Python * **Remote Interpreter**: Connect to server or Docker environment !(#) **Installing Dependencies:** * Click `+` in the interpreter interface to search and install packages (e.g., `requests`) * Or run `pip install package_name` in the terminal !(#) * * * ## Configuring Run Parameters **Opening Run Configuration:** * Top toolbar β†’ `Run β†’ Edit Configurations` * Or click the configuration dropdown menu next to the run button !(#) **Adding Python Configuration:** Click + β†’ Select Python !(#) **Key Parameters:** * **Script Path**: Select the `.py` file to execute * **Parameters**: Enter command-line arguments (e.g., `--port 8000`) * **Working Directory**: Set the current directory for script execution * **Environment Variables**: Add custom environment variables (e.g., `PYTHONPATH`) !(#) **Saving Configuration:** Click Apply β†’ OK. * * * ## Running Python Scripts ### Running a Single File **Method 1: Right-click to Run:** * Right-click in the editor β†’ `Run 'filename'` **Method 2: Toolbar Run:** * Click the green run button ▢️ on the top toolbar **Method 3: Shortcut Run:** * `Shift + F10` (Run current file) * `Ctrl + Shift + F10` (Run the currently edited file) !(#) ### Running Modules (e.g., Flask/Django) **Configuring Module Run:** Select the module name instead of script path in the run configuration. For example: * **Module Name**: `flask` * **Parameters**: `run --port=5000` Running a module with parameters: # For example, running the 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 !(#) * * * ## Debugging Code ### Setting Breakpoints **Adding Breakpoints:** * Click on the left side of the line number (a red dot appears) * Right-click the breakpoint to set conditions (e.g., `x > 10`) **Breakpoint Types:** * **Line Breakpoint**: Pause at the specified line * **Exception Breakpoint**: Pause when an exception is thrown (`Run β†’ View Breakpoints β†’ Add Python Exception Breakpoint`) **Starting Debugging:** * Click the `? Debug` button on the toolbar * Or use the shortcut `Shift + F9` !(#) ### Variable Watches **Viewing Variables:** * After debugging starts, view variable values in the current scope in the `Variables` panel * Double-click a variable to edit its value (for temporary testing) **Adding Watches:** * Click `+` in the `Watches` panel * 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 function internals | | Step Into | `F7` | `F7` | Enter inside the function for debugging | | Step Out | `Shift + F8` | `⇧ + F8` | Finish executing the current function and return to the caller | | Resume | `F9` | `⌘ + F8` | Continue running until the next breakpoint | | Stop | `Ctrl + F2` | `⌘ + F2` | Terminate the debugging session | ### Debug Console **Interactive Debugging:** * During debugging, you can directly execute Python code in the `Debug Console` * For example: temporarily call functions or modify variables * **Evaluating Expressions:** * Select code β†’ Right-click β†’ `Evaluate Expression` * Or use the shortcut `Alt + F8` * * * ## Advanced Debugging Techniques ### Remote Debugging **Configuring Remote Interpreter:** * `File β†’ Settings β†’ Python Interpreter β†’ Add Remote Interpreter` * Supports SSH, Docker, and Vagrant **Debugging Remote Code:** * Breakpoints and debugging operations are the same as local debugging ### Multi-Process Debugging **Debugging Subprocesses:** * Check `Debug child processes` in the run configuration * Applicable to `multiprocessing` or `subprocess` scenarios **Attaching to Process:** * `Run β†’ Attach to Local Process` * Select the running Python process * * * ## **Common Problem Solutions** ### **Q1: Breakpoints not working?** * Check if the interpreter matches the project environment * Ensure the code is saved (`Ctrl + S`) * Check if the breakpoint is disabled (breakpoint icon is gray) ### **Q2: How to debug unit tests?** * Right-click in the test file β†’ `Debug 'test name'` * Or use dedicated run configurations for `pytest` / `unittest` ### **Q3: Variables show "Unknown" during debugging?** * Optimize code structure, avoid overly complex expressions * Use `Watches` to manually add watched variables * * * ## **Debugging Shortcuts** | **Scenario** | **Operation** | | --- | --- | | Quickly run current file | `Shift + F10` | | Start debugging | `Shift + F9` | | Conditional breakpoint | Right-click breakpoint β†’ Set condition | | View variable values | View in `Variables` panel during debugging | | Temporarily execute code | Enter command in Debug Console | | Multi-process debugging | Check `Debug child processes` in run configuration |
← Regexp QuizPycharm File β†’