Python in Positron

Positron ships with first class support for Python, along with R. Start with our guide on how Positron discovers and manages Python installations, then read on for details on working with Python code in Positron.

Settings

Tip

In addition to providing settings, Positron also provides keyboard shortcuts and bundled extensions, for a “batteries included” experience for data science with Python.

Positron provides setting defaults for working with Python for data science, but you can override or change these defaults if you prefer a different experience. For example:

  • Positron bundles Pyright to provide type checking for Python code, but disables the language services since these would duplicate some functionality provided by Positron itself. If you prefer to see Pyright’s type completion, definitions, and references (in addition to other Python language services), then change the pyright.disableLanguageServices setting from the Positron default.
  • Positron bundles Ruff to provide formatting for Python and sets the ruff.importStrategy setting to "useBundled". If you prefer to use a Ruff installation that you manage yourself, change this setting to "fromEnvironment".

You can see all the setting defaults that Positron provides with the command Preferences: Open Default Settings (JSON).

Magics

Positron provides support for IPython-style magics in the Positron console (as well as Python scripts and Jupyter notebooks, where appropriate). Magics are special commands prefixed with the % symbol that provide convenient shortcuts for common tasks. You can see the supported magics by running the %lsmagic command in the Positron console.

Positron provides three new, custom magics created specifically for working in Positron:

  • %view opens a Python object in the Data Explorer. You can use this magic most simply via %view df, or additionally like %view df.groupby('column').sum() or %view df "My Dataset".
  • %connection_show shows a connection object in the Connections Pane. Use this magic like %connection_show my_connection.
  • %clear clears the Console.

Code formatting

Positron can format Python code with Ruff, a Python linter and formatter. Ruff is included in Positron as a bootstrapped extension that is installed for you, including the bundled Ruff command line tool. Most Positron users do not need to do anything intentional to install Ruff or to keep it up-to-date.

You can format your Python code with various explicit and implicit gestures. You can use the commands Format Selection or Format Document to format specific bits of code. You can even configure Positron to run Ruff automatically whenever you save a Python file. In the Settings UI, search for @lang:python editor.formatOnSave and enable the setting, either at the workspace (recommended) or user level. This will add the following to the appropriate settings.json:

{
    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true
    }
}

Debugging

Positron bundles the Python Debugger as a bootstrapped extension to provide debugging support for Python code. You can set breakpoints, step through code, and inspect variables during debugging sessions. Learn more from the VS Code documentation on debugging Python.

Note

Positron provides basic support for debugging in the console via import ipdb; ipdb.set_trace() and %debug. Follow along on GitHub for further improvements in this support.