Python console
Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.
In the console, you can:
Type commands and press Enter to execute them. Results are displayed in the same console.
Use basic code completion CtrlSpace and tab completion.
Run asyncio coroutines.
Use 0↑ and 0↓ to scroll through the history of commands, and execute the required ones.
Load source code from the editor into console.
Use the context menu to copy the contents of the console to the clipboard, compare it with the clipboard, or clear the console.
Use the toolbar buttons to control your session in the console.
Configure color scheme of the console to meet your preferences. For more information, refer to Configure color schemes for consoles.
Use config files to configure the IPython console automatically.
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 CtrlAlt0S, 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.
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 and check the Special Variables list.
The console is available for all types of Python interpreters and virtual environments, both local and remote.
When viewing variables in the Python Console, you can click View as Array, View as DataFrame, or View as Series links to display the data in the Data View tool window.
By default, the new table representation is used.
Click More Actions and select Switch Between Table Representations to change the table interface.
note
PyCharm will remember your choice and use the selected table representation in the future, except for the data structures from the newly supported libraries (for example,
polars
), which will always be displayed using the new representation.
Open file in the editor, and select a fragment of code to be executed.
From the context menu of the selection, choose Execute Selection in Python Console, or press AltShift0E:
note
With no selection, the command changes to Execute line in console. Choose this command from the context menu, or press AltShift0E. The line at caret loads into the Python console, and runs.
Watch the code selection execution:
By default, the Python console executes Python commands using the Python interpreter defined for the project. However, you can assign an alternative Python interpreter.
In the editor, select a fragment of code which contains the definition of an asyncio coroutine.
From the context menu, select Execute Selection in Python Console, or press AltShift0E:
After the code is executed on the Python console, run the coroutine by using the
await
keyword:
In the Settings dialog (CtrlAlt0S) , select Build, Execution, Deployment | Console | Python Console.
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.
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.
Click to add a new Python console.
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.
All the commands you're running in the Python console are executed one by one. If the commands require substantial time to get executed, you might want to preview and manage the execution queue.
Go to Settings | Build, Execution, Deployment | Console and enable the Command queue for Python Console checkbox.
Click on the console toolbar to open the queue.
In the Python Console Command Queue dialog, review the list of commands. If needed, click to delete the command from the queue.
Note, once the command is executed, it disappears from the queue. To preview all previously executed commands browse the console history ().
Item | Tooltip and shortcut | Description |
---|---|---|
Rerun console CtrlF5 | Click this button to terminate the current process and launch the new one. | |
Stop CtrlF2 | Click this button to stop the current process. Clicking the button once invokes soft kill allowing the application to catch the | |
Execute Current Statement Enter. | Click this button to execute the command at caret, entered in the input pane of the console. | |
Attach Debugger | Attaches the debugger process to the console. | |
Settings | You can specify the following settings of the Python console:
| |
New Console | Click this button to start a new console session. | |
Use Soft Wraps | Click this button to toggle the soft wrap mode of the output. | |
Scroll to the end | Click this button to navigate to the bottom of the stack trace and have the caret jump to the corresponding location in the source code. | |
Click this button to send the console text to the default printer. | ||
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. | |
Show Command Queue | Click this button to preview the command execution queue. | |
Browse History CtrlAlt0E | Open a dialog that shows all the statements that you have run for the corresponding data source. See also, Use the Query History dialog. |
Command | Shortcut | Description |
---|---|---|
Compare with Clipboard | Show selection in the console and contents of the Clipboard in the Diff Viewer. | |
Pause Output | Pause the Python script execution output | |
Clear All | Choose this item from the context menu to delete all messages from the upper part of the console. |
Thanks for your feedback!