Run Python code
The Python editor toolbar provides quick access to the most popular run/debug actions:
|Run code in the Python Console.||Execute the Python script in the selected console. See Run a file in the Python Console for more details.|
|Debug Python code||Start debugging the Python script. See Debug Python code for more details.|
|.||Run the code line in the Python Console||Execute the line at caret. See Run a code fragment in the console for more details.|
|Edit run/debug configuration||Modify the way JetBrains DataSpell executes your code. See Run/Debug Configuration: Python for more details.|
A simplest way to do it is to run the file in the Run tool window (Ctrl+Shift+F10). However, you can also execute your script in the interactive read-evaluate-print loop (REPL) Python Console. You can even execute a selected fragment of your code, or a Python code cell.
Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell. The console is located in the lower group of the JetBrains DataSpell tool windows and it opens each time you initiate code execution in it. You can also open it by selecting from the main menu.
Run a file in the Python Console
To run a Python script in the console, click on the Python editor toolbar.
Once you initiate running, the Python Console opens and you should see the code execution.
By default, each console has a name that corresponds to the filename of the executed file with the index. So, if you want to execute sample.py in a new console, it will be run in the tab with the name sample (1). Use the list in the upper-right corner of the Python editor to select a particular console you need.
To rename a console tab, right-click the it, select Rename Console, and enter any meaningful name.
The renamed console is shown in the list of the consoles:
Run code in a non-interactive console
If the way you execute your Python script doesn't imply any interactivity, you can run it in a non-interactive console, similar to running code in the Run tool window.
Select Non-Interactive Shell from the list of controls and click to execute the currently opened file in the specified shell console.
Run a code fragment in the console
Place a caret at any code line and click on the Python editor toolbar. JetBrains DataSpell executes that line and moves the caret to the next line.
Select a code fragment and click on the Python editor toolbar. The selected fragment will be executed in the current console.
When executing a selected fragment, mind code dependencies and required import statements.
Run code cells
You can format your Python code as a set of executable cells to run each separately.
To split your code into cells, add
# %%lines where appropriate.
Each cell has a Run icon () in the gutter. Click it to execute a cell. Note that the
kernel_statsvariable is not defined until you execute the first cell. So, mind code dependencies and required imports.
You can preview all commands executed in the current session of the selected Python console.
View console query history
Select a target console and click on the console toolbar.
Preview the list of the executed commands in the history dialog: You can select any item in the list and click the Paste button to copy and paste the related commands into the current Python console
Once the commands are added, press Enter to execute them.
Use the Python Console toolbar to configure the way JetBrains DataSpell present the execution output.
Python Console Toolbar
|Item||Tooltip and shortcut||Description|
|Click this button to terminate the current process and launch the new one.|
Click this button to stop the current process.
Clicking the button once invokes soft kill allowing the application to catch the
Execute Current StatementEnter.
|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.|
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 cursor 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.|
|Browse Data Source History|
Open a dialog that shows all the statements that you have run for the corresponding data source. queries that you have run in the console.
See also, Using the History dialog.