PyCharm 2021.2 Help

Python console

Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.

Actions available in the Python console

In an interactive console, you can:

  • Type commands in the lower pane of the console, and press Enter to execute them. Results are displayed in the upper pane.

  • Use basic code completion Ctrl+Space and tab completion.

  • Use

    Up and Down arrow keys to scroll through the history of commands, and execute the required ones.

  • Load source code from the editor into console.

  • Use context menu of the upper pane to copy all output to the clipboard, compare with the current contents of the clipboard, or remove all output from the console.

  • Use the toolbar buttons

    to control your session in the console.

  • Configure color scheme of the console to meet your preferences. Refer to the section Configure color schemes for consoles for details.

Working with Python console

The console appears as a tool window every time you choose the corresponding command on the Tools menu. You can assign a shortcut to open Python console: press Ctrl+Alt+S, navigate to Keymap, specify a shortcut for Main menu | Tools | Python or Debug Console.

The main reason for using the Python console within PyCharm is to benefit from the main IDE features, such as code completion, code analysis, and quick fixes.

Code completion in the Python console

You can use up and down arrow keys to browse through the history of executed commands, and repeat the desired ones. To preview the variable values calculated in the course of the execution, click Show variables and check the Special Variables list.

Show variables in the Python console

The console is available for all types of Python interpreters and virtual environments, both local and remote.

Preview a variable as an array

When your variables are numpy arrays or dataframes, you can preview them as an array in a separate window. To try it, do one of the following:

  • Click the link View as Array/View as DataFrame:

    Viewing variables in array when running from Python console
  • From the context menu of a variable, choose View as Array/View as DataFrame:

    Viewing variables in data frames when running from Python console

The variable will be opened in the Data tab of the SciView window.

View an array or dataframe in a separate window

Run source code from the editor in console

  1. Open file in the editor, and select a fragment of code to be executed.

  2. From the context menu of the selection, choose Execute selection in console, or pressAlt+Shift+E:

    Context menu for executing the code selection
  3. Watch the code selection execution:

    Execution of the code selection in the console

By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter.

Configure Python console settings

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Build, Execution, Deployment | Console | Python Console.

  2. Select any available interpreter from the Python interpreter list. Note that you cannot introduce a new interpreter here. If you want to come up with the new interpreter, you need to create it first.

    Search an alternative Python interpreter
  3. In needed, click the Configure Interpreters link to inspect the list of the installed packages and add new ones.

    Mind the code in the Starting script area. It contains the script that will be executed after you open the Python console. Use it to pre-code some required Python commands.

When working on several Python scripts, you might want to execute each in a separate Python console.

Run several Python consoles

  1. Click New Console to add a new Python console.

  2. By default, each console has the name Python Console with an index. To make a console reflect the script you're running, right-click the console tab, select Rename Console, and enter any meaningful name.

    Rename the Python console

Toolbar

Item

Tooltip and shortcut

Description

Rerun console

Rerun console

Ctrl+F5

Click this button to terminate the current process and launch the new one.

the Stop button

Stop

Ctrl+F2

Click this button to stop the current process.

Clicking the button once invokes soft kill allowing the application to catch the SIGINT event and perform graceful termination (on Windows, the Ctrl+C event is emulated) . After the button is clicked once, it is replaced with the Kill Process button indicating that subsequent click will lead to force termination of the application, for example on Unix SIGKILL is sent.

Run command

Execute Current Statement

Enter.

Click this button to execute the command at caret, entered in the input pane of the console.

Debug

Attach Debugger

Attaches the debugger process to the console.

Settings

Settings

You can specify the following settings of the Python console:

  • Simplified Variables View: Select this option to include the following names into the Special Variables group:

    • all variables with the dunder names.

    • all instances of function, classobj and module.

    • all the IPython internal variables, if debug console with IPython has been started.

  • Variables Loading Policy. You can choose one of the following policies:

    • Synchronously: If this check command is selected, the message shown is Collecting data, and after that the debugger shows the variables' values.

    • Asynchronously: If this check command is selected, the variables in the debugger are loaded separately (asynchronously). In this case, if loading a variable takes a long time, the debugger shows ...Loading Value message, and only after that it shows its value or the Loading timed out message, if the variable is too slow. Mind the warning message that suggest switching to the on demand mode.

    • On demand: If this check command is selected, the debugger shows Show Value message. Click it to enable loading.

    Note that any changes in the loading policies become effective after you rerun the debugging process.

Add a new console

New Console

Click this button to start a new console session.

the Soft-Wrap button

Use Soft Wraps

Click this button to toggle the soft wrap mode of the output.

Scroll to the end

Scroll to the end

Click this button to navigate to the bottom of the stack trace and have the cursor jump to the corresponding location in the source code.

the Print button

Print

Click this button to send the console text to the default printer.

Debugger watches

Show variables

Click this button to show in a separate pane the variables declared in the console. Right-click a variable in this pane reveals a context menu.

Browse Data Source History

Browse History

Ctrl+Alt+E

Open a dialog that shows all the statements that you have run for the corresponding data source.

See also, Using the History dialog.

Context menu commands

Command

Shortcut

Description

Compare with Clipboard Compare with Clipboard

Show selection in the console and contents of the Clipboard in the Differences viewer.

Pause Output

Pause the Python script execution output

Create gist icon Create Gist

Choose this command to create a Git Gist.

Clear all Clear All

Choose this item from the context menu to delete all messages from the upper part of the console.

Last modified: 08 March 2021