DataSpell 2023.2 Help

Run Python code

Once you have created a Python file and configured a virtual environment, you can execute your code.

The Python editor toolbar provides quick access to the most popular actions:

Icon

Action

new cell

Adds a code cell below the selected cell.

Cut

Moves the selected code cell the current location to the clipboard.

Copy

Copies the selected code cell to the clipboard.

Paste

Inserts the contents of the clipboard into a new code cell below the selected one.

Move cell up

Moves the current cell up.

Move cell down

Moves the current cell down.

Run cell

Executes this cell and selects a cell below. If there is no a cell below, DataSpell will create it.

the Run All button

Runs code of the Python file in the Python Console.

Debug Python code

Starts debugging Python code.

remove the cell

Deletes the current cell.

You can 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

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 DataSpell tool windows and it opens each time you initiate code execution in it. You can also open it by selecting Windows | Tool Windows | Python Console from the main menu.

Run a file in the Python Console

  • To run a Python script in the console, click the Run button on the Python editor toolbar.

    The Run button on the toolbar

Once you initiate running, the Python Console opens and you should see the code execution.

Running file in the Python Console

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.

Running a file in several consoles

To rename a console tab, right-click it, select Rename Console, and enter any meaningful name.

Renaming the console tab

The renamed console is shown in the list of the consoles:

Renamed console

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 the Run All button.

    Running a script in the non-interactive console

Run code cells

You can format your Python code as a set of executable cells to run each separately.

  1. To split your code into cells, add # %% lines where appropriate.

    Splitting code into cells
  2. Each cell has a Run icon (the Run button) in the gutter. Click it to execute a cell. Mind code dependencies and required imports. In the following example, the kernel_stats variable is defined in the first cell. So, running the second cell first results in NameError:

    Running the second cell

You can preview all commands executed in the current session of the selected Python console.

View console query history

  1. Select a target console and click Browse Query History on the console toolbar.

  2. Preview the list of the executed commands in the history dialog:

    Query 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

  3. Once the commands are added, press Enter to execute them.

    Execute commands from the Python console query history

Use the Python Console toolbar to configure the way DataSpell present the execution output.

Python Console Toolbar

Item

Tooltip and shortcut

Description

Rerun console

Rerun console

Control+F5

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

the Stop button

Stop

Control+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 caret 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.

Show Command Queue

Show Command Queue

Click this button to preview the command execution queue.

the Browse Query History button

Browse Query History

Control+Alt+E

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

See also, Use the Query History dialog.

Last modified: 08 September 2023