我的项目结构是这样的-有3个py文件-main.py,main_lib.py和helper_lib.py。 main_lib和helper_lib导入main.py中。
我正在将help.lib.py中的dask.dataframe导入,并具有一个助手功能,可以像这样从pandas数据框创建dask数据框-
import dask.dataframe as ddf
def get_dask_df(df: pd.DataFrame):
return ddf.from_pandas(df, npartitions=2)
helper_lib.py和main.py中都调用了此函数。
引起ImportError的代码是这样的-
data_dask_df = helper_lib.get_dask_df(data)
result['area'] = data_dask_df[['length', 'width']].apply(
lambda x: helper_lib.calculate_area(x),
meta=('int'),
axis=1).compute()
这是堆栈跟踪-
File "/xxx/dask/base.py", line 156, in compute
(result,) = compute(self, traverse=False, **kwargs)
File "/xxx/dask/base.py", line 398, in compute
results = schedule(dsk, keys, **kwargs)
File "/xxx/dask/threaded.py", line 76, in get
pack_exception=pack_exception, **kwargs)
File "/xxx/dask/local.py", line 462, in get_async
raise_exception(exc, tb)
File "/xxx/dask/compatibility.py", line 112, in reraise
raise exc
File "/xxx/dask/local.py", line 230, in execute_task
result = _execute_task(task, data)
File "/xxx/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/xxx/dask/optimization.py", line 942, in __call__
dict(zip(self.inkeys, args)))
File "/xxx/dask/core.py", line 149, in get
result = _execute_task(task, cache)
File "/xxx/dask/core.py", line 119, in _execute_task
return func(*args2)
File "/xxx/dask/compatibility.py", line 93, in apply
return func(*args, **kwargs)
File "/xxx/dask/dataframe/core.py", line 3879, in apply_and_enforce
df = func(*args, **kwargs)
File "/xxx/dask/utils.py", line 772, in __call__
return getattr(obj, self.method)(*args, **kwargs)
File "/xxx/pandas/core/frame.py", line 6477, in apply
from pandas.core.apply import frame_apply
ImportError: cannot import name 'frame_apply'
我在main_lib和helper_lib中都有此导入-
import pandas as pd
我尝试在main_lib和helper_lib中复制get_dask_df
函数,并在两者中导入dask。仍然不起作用。
在互联网上,这似乎与循环/多次导入有关,或者frame_apply
已被导入到其他地方,因此它在此推迟的导入上引发了ImportError。
如何调试/解决这个问题?