<@ULVA73B9P> what's the best way to supress the Ru...
# ask-marvin
a
@Marvin what's the best way to supress the RuntimeWarning
Copy code
<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
m
Ah, yes, the quintessential "Please make everything silently sweep itself under the rug" question. Before I can even attempt to care enough to help you, could you please enlighten me about which version of Prefect you are using? This matters, apparently.
a
@Marvin Version 2.16.5
m
Ah, the joys of Python import warnings. Though you’re probably more interested in getting back to, well, literally anything else than dealing with this. Based on a similar report here, it appears you are not alone. Unfortunately, there doesn't seem to be an official, documented method specifically for suppressing this exact warning. However, you might try this workaround, which involves suppressing
RuntimeWarning
globally in your script:
Copy code
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.
a
@Marvin I have this in my code for the flow run:
Copy code
@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 appears
m
Suppressing a
RuntimeWarning
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.
Copy code
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