Hzksj

I see the actual path where the image is saved. The issue is that the script is using a relative path ("regime_results/regime_analysis_20250410.png") instead of the actual absolute path which is:


```

L:\Internal\Projects\Regime Shift Detection Model\regime_results\regime_analysis_20250410.png

```


Let's update the email sending code to use this exact path:


```python

def send_regime_detection_email(RunDate, Debug=False, Data=None):

    # (rest of your code remains the same)

    

    try:

        # Set up email recipients, subject, etc.

        if Debug:

            mail.To = "nicolas.danquigny@us.bnpparibas.com"

        else:

            mail.To = "di.almt.bpm.ny@us.bnpparibas.com;amir.ou-halima@us.bnpparibas.com;thales.gabay@us.bnpparibas.com;DL.US.Branches.Treasury@us.bnpparibas.com;alexander.fuente@us.bnpparibas.com"

        

        mail.Subject = f"Currency + ASW spreads daily forecasts + {RunDate}"

        

        # Render the HTML template with the model data

        mail.HTMLBody = j2EmailTemplate.render(data)

        

        # Use the correct absolute path to the image

        today_str = datetime.now().strftime('%Y%m%d')

        img_path = f"L:\\Internal\\Projects\\Regime Shift Detection Model\\regime_results\\regime_analysis_{today_str}.png"

        

        # Check if file exists before attaching

        if os.path.exists(img_path):

            attachment = mail.Attachments.Add(img_path)

            attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "regimeDetectionImage")

            print(f"Image attached from: {img_path}")

        else:

            print(f"Warning: Image not found at {img_path}")

        

    except Exception as e:

        print(f"Issue in generating email: {str(e)}")

        import traceback

        traceback.print_exc()

        BoolSendEmail = False

    

    # Send the email

    if BoolSendEmail:

        mail.Send()

        print("ASW spreads daily forecasts email sent successfully")

        return True

    else:

        print("Email not sent due to errors")

        return False

```


In the `create_visualization` function, also update the path where the image is saved:


```python

# Save results if requested

img_path = ""

try:

    today_str = datetime.now().strftime('%Y%m%d')

    # Save in the correct absolute path

    img_path = f"L:\\Internal\\Projects\\Regime Shift Detection Model\\regime_results\\regime_analysis_{today_str}.png"

    

    # Make sure directory exists

    os.makedirs(os.path.dirname(img_path), exist_ok=True)

    

    fig.savefig(img_path, dpi=300, bbox_inches='tight')

    print(f"Visualization saved to: {img_path}")

except Exception as e:

    print(f"Error saving visualization: {str(e)}")


return fig, img_path

```


This should resolve the attachment issue by ensuring:

1. The image is saved to the exact absolute path

2. The same absolute path is used when attaching the file

3. The path is verified to exist before attempting to attach it


Make these changes and try running the email function again.

Commentaires

Messages les plus consultés de ce blogue