Andre Dias
08/30/2024, 2:38 PM<frozen runpy>:128: RuntimeWarning: 'prefect.engine' found in sys.modules after import of package 'prefect', but prior to execution of 'prefect.engine'; this may result in unpredictable behaviour
Marvin
08/30/2024, 2:38 PMAndre Dias
08/30/2024, 2:39 PMMarvin
08/30/2024, 2:39 PMRuntimeWarning
globally in your script:
python
import warnings
# Suppress specific RuntimeWarnings
warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*'prefect.engine'.*")
If that doesn't do the trick, you might want to dive into the code to figure out what specifically is causing the warning, but really, who has time for that?
Links of interest:
- Prefect Issue #7869
Good luck.Andre Dias
08/30/2024, 2:40 PM@prefect.flow(
task_runner=DaskTaskRunner(cluster_kwargs={"memory_limit": "2G"}),
result_serializer="pickle",
name="execrisks-tasks",
log_prints=True,
)
def execute_tasks(payload_params=None):
# Suppress specific RuntimeWarning related to prefect.engine import until a fix is deployed on their end.
# Related issue <https://github.com/PrefectHQ/prefect/issues/7869>.
warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*'prefect.engine'.*")
But it still appearsMarvin
08/30/2024, 2:41 PMRuntimeWarning
directly within the Prefect flow setup seems to be as effective as a screen door on a submarine. Based on both the Prefect docs and the GitHub issue I mentioned, you might need to get creative.
Here’s a potential workaround you could try:
1. Patch Warnings Globally: Ensure the warning is suppressed globally before Prefect starts messing around with imports.
2. Move Suppression Early: Place the warning suppression code earlier in your script, before importing anything from Prefect.
Try placing the warning filter at the very top of your script, before any imports.
python
import warnings
# Suppress specific RuntimeWarning
warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*'prefect.engine'.*")
from prefect import flow
from prefect_dask import DaskTaskRunner
@flow(
task_runner=DaskTaskRunner(cluster_kwargs={"memory_limit": "2G"}),
result_serializer="pickle",
name="execrisks-tasks",
log_prints=True,
)
def execute_tasks(payload_params=None):
pass
# Your flow code here
This approach should ensure that the runtime warning is suppressed before Prefect imports are initialized, thereby preventing that annoying warning from surfacing.
Wouldn't it be great if things just worked without all these warnings? Ah, one can dream...
Link to the related issue: GitHub Issue #7869