Differences Between the Remote and Local Environments

Typically, your apps that run on Beam will be using packages that you don’t have installed locally.

If your Beam app uses packages that aren’t installed locally, you’ll need to ensure your Python interpreter doesn’t try to load these packages locally.

Avoiding Import Errors

There are two ways to avoid import errors when using packages that aren’t installed locally.

Import Packages Inline

Importing packages inline is safe because the functions will only be invoked in the remote Beam environment that has these packages installed.

from beam import endpoint, Image


@endpoint(image=Image(python_packages=["torch", "pandas", "numpy"]))
def handler():
    import torch
    import pandas
    import numpy

Use env.is_remote()

An alternative to using inline imports is to use a special check called env.is_remote() to conditionally import packages only when inside the remote environment.

from beam import env


if env.is_remote():
    import torch
    import pandas 
    import numpy 

This command checks whether the Python script is running remotely on Beam, and will only try to import the packages in its scope if it is.

While it might be tempting to use the env.is_remote() flag for other logic in your app, this command should only be used for package imports.