How to use Deephaven in a local development environment (Python)

Set up your environment

This guide shows you how to create a Python project that uses Deephaven libraries for local development and testing. Because deephaven_enterprise.database resolves Java classes at import time, you must configure both Python packages and the Java classpath.

Prerequisites

  • Python 3.10+: Required by the Core+ worker package.
  • Java 17+: Set JAVA_HOME to your Java installation.
  • Maven: Used to download Core+ JARs.
  • Deephaven repository credentials: Required for downloading Core+ artifacts.

Install Python packages

Create a virtual environment and install the required packages:

Copy the requirements file and worker wheel from your Deephaven server, then install version-matched dependencies:

Important

The requirements.txt from the server specifies the deephaven-core version tested with your Core+ installation. Installing an unpinned deephaven-core from PyPI can leave the Python wrapper and Java JARs on incompatible versions.

Download Core+ JARs

The deephaven_enterprise.database module requires Core+ Java classes. Use Maven to download the JARs from the Deephaven repository.

First, add your Deephaven credentials to ~/.m2/settings.xml:

Then create a pom.xml to download dependencies:

Then run:

Initialize the JVM

Before importing any deephaven_enterprise modules, initialize the JVM with the Core+ classpath. Create a conftest.py file for pytest:

Important

The JVM must be initialized before any deephaven or deephaven_enterprise imports. Place JVM initialization in conftest.py so it runs before test collection.

Local unit testing

It may be helpful to have some local test data you can use to test your query's correctness. The steps below extract query logic into a testable function, then add pytest fixtures and tests for it.

The following query runs on a Deephaven worker and calculates the average and mid prices of stocks for a given day:

The query logic is extracted into helper functions in a separate module. These functions accept a Database parameter instead of importing db directly, making them testable with a mock:

We need some test data. Create the following CSV files:

tests/resources/StockTrades.csv — Sample trade data for the average price test:

tests/resources/StockQuotes.csv — Sample quote data for the mid price test:

  1. Add a fixture to conftest.py for the execution context. Merely importing deephaven does not create a working execution context — the default context is only wired up once a Deephaven script session has started, which does not happen here. Instead, build one directly from the same TestExecutionContext the Groovy guide uses, which is self-contained and does not require a running server. The execution context is required for table operations like where and update_view.
  1. Add a fixture for a mocked Database. Mocking the Database to read test data from CSV files is easier than creating a real Database instance.
  1. Use the methods described in the Core Extract table values guide to test your queries:
The full conftest.py:
The full test file:

Connect to a remote DB

The steps above cover testing query logic locally, without a running Deephaven server. Client applications can also connect to Deephaven server installations to run queries on a remote database. See the Core+ Python Client for more information.