Module import error in custom functions with a Parameterized class

Hi all,

Apologies in advanced, I don’t know how to recreate the error exactly- the behavior is seemingly random- but I’ll give as much code as I can.

I have an app of type pn.viewable.Viewer with many parameters, including tap_x = param.Number(np.nan) and tap_y = param.Number(np.nan). I also have a DoubleTap stream initiallized as

self._dtap = hv.streams.DoubleTap(
    x=np.nan,
    y=np.nan,
    source=self._tile
)

where self._tile is a geoviews tile (geoviews.tile_sources.EsriImagery()). This stream is used in a pn.bind function as follows:

timeseries_plot = pn.bind(
    self._timeseries_plot,
    self._dtap.param.x,
    self._dtap.param.y,
    self.param.time,
    self.param.depth
)

def _timeseries_plot(self,x,y,time,depth):
    if (not np.isnan(x)): # at app initialization, don't return any plots
        # if a point is selected, transform from web_mercator to PlateCarree
        lon,lat = self._plot_proj.transform_point(x, y,self._tile_proj)
        da = self._roms_ds.isel(
            s_rho=depth
        )
        da = da.sel(lat_rho=lat, lon_rho=lon)
        if (da.mask_rho == 1)): # check if selected point is in valid domain
            # loop through variables, create time series of each at given point
            time_x = self._roms_ds.ocean_time.values[int(time/3)]
            overlay = da[list(VAR_OPTIONS.values())[0]].hvplot(
                x='ocean_time',
                dynamic=False,
                title=list(VAR_OPTIONS.keys())[0]
            ).opts(labelled=[],active_tools=[],max_height=150,width=250,)
            overlay *= hv.VLine(time_x).opts(color='r')
            for var in list(VAR_OPTIONS.values())[1:-1]:
                var_name=list(VAR_OPTIONS.keys())[list(VAR_OPTIONS.values()).index(var)]
                plot = da[var].hvplot(
                    x='ocean_time',
                    dynamic=False,
                ).opts(labelled=[],active_tools=[],max_height=150,width=250,title=var_name,)
                plot *= hv.VLine(time_x).opts(color='r')
                overlay += plot
            self._text.object = f'Timeseries at location:\n {lat:.2f}°N, {lon:.2f}°E'
            return overlay
        else: self._text.object = ''
    else: self._text.object = ''

I know there is a lot that is unexplained above, but the idea is that a map is double clicked and a set of timeseries are returned at that point; however, if a point outside the valid domain is clicked then no plots are returned. Sometimes this works perfectly, while other times I get an import error about numpy. I can fix this by adding import numpy as np in the functions, but then I sometimes get an import error about xarray. I can import xarray in the function as well with no problems, but it seems silly to have to import the same packages every time a function is called. Is there something going on with the threadpool workers? I know in some mpi and threading packages you must manually copy your active environment to each worker- perhaps there’s a way for me to do that?