Skip to main content
Version: Python

How do I import one Python script into another in the Deephaven IDE?

I have a Python script that defines some functions, classes, variables, or other objects. Can I use these in a new Python script from the Deephaven IDE?

Yes. Python allows importing one script into another in the same way that a Python package would be imported, so long as those two scripts are in the same directory. Take the following example:

# script1.py


def get_5():
return 5


a = get_5()
# script2.py

from script1 import *

b = get_5() + 1
c = a + b

print(a, b, c)

Here, script2.py imports all of the objects created in script1.py and uses them in its body. Naturally, this is expected to work from the Deephaven IDE. However, one extra step must be performed before this works as expected.

When the Deephaven server launches, it starts a Python interpreter that it will use to execute Python code. In order for script2.py in the above example to run, the Python interpreter must know about the existence of script1.py. Usually this is handled by default, because the Python interpreter is running from the same directory that the scripts are stored in. However, this is not the case in Deephaven. Scripts are stored in a separate directory known as the data directory, and the interpeter does not know about the data directory by default. Therefore, you must tell the Python interpreter where the data directory is located. This can be done with the append method of the sys.path list - you just need the path of the data directory.

In Docker-run Deephaven, the data directory is /data/storage/notebooks by default. So, add that path to the set of paths known to the interpreter:

import sys

sys.path.append("/data/storage/notebooks")

Then, the above example will run as expected:

img

Using pip-installed Deephaven makes this slightly more complicated because the data directory depends on the operating system. However, the data directory prints to the console when the Deephaven server starts, so it can be copied from there. Then, you must append "/storage/notebooks" to the end of the path in the call to sys.path.append.

In this example, the console output indicates that the data directory is /Users/alexpeters/Library/Application Support/io.Deephaven-Data-Labs.deephaven. So, the code to add the path looks like this:

import sys

sys.path.append(
"/Users/alexpeters/Library/Application Support/io.Deephaven-Data-Labs.deephaven/storage/notebooks"
)

The scripts will now run with no problem:

img

danger

If you do not append storage/notebooks to the end of the data directory in the call to sys.path.append, the script import will fail. Even if the correct path is then added in the same session, the Deephaven server must be restarted, and care must be taken to ensure that the correct path is added via sys.path.append.

note

These FAQ pages contain answers to questions about Deephaven Community Core that our users have asked in our Community Slack. If you have a question that is not in our documentation, join our Community and we'll be happy to help!