使用网状调用Python脚本并发送电子邮件 [英] Use reticulate to call Python script and send email

查看:118
本文介绍了使用网状调用Python脚本并发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每天使用Windows Task Scheduler运行R脚本几次.该脚本将转换一些新数据并将其添加到现有数据文件中.

I use Windows Task Scheduler to run an R Script several times a day. The script transforms some new data and adds it to an existing data file.

我想使用reticulate调用Python脚本,该脚本将向我发送一封电子邮件,列出添加了多少行数据以及是否发生任何错误.当我从RStudio中逐行运行它时,此方法可以正常工作.问题是当脚本按计划运行时它不起作用.我收到以下错误:

I want to use reticulate to call a Python script that will send me an email listing how many rows of data were added, and if any errors occurred. This works correctly when I run it line by line from within RStudio. The problem is that it doesn't work when the script runs on schedule. I get the following errors:

Error in py_run_file_impl(file, local, convert) : 
  Unable to open file 'setup_smtp.py' (does it exist?)
Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: module '__main__' has no attribute 'message'
Calls: paste0 ... py_get_attr_or_item -> py_get_attr -> py_get_attr_impl
Execution halted

此github答案 https://github.com/rstudio/reticulate/issues/232 )听起来像reticulate只能在RStudio中使用-至少用于我要执行的操作.有人有建议吗?

This github answer https://github.com/rstudio/reticulate/issues/232) makes it sound like reticulate can only be used within RStudio - at least for what I'm trying to do. Does anyone have suggestions?

R脚本示例:

library(tidyverse)
library(reticulate)
library(lubridate)

n_rows <- 10
time_raw <- now()

result <- paste0("\nAdded ", n_rows, 
                 " rows to data file at ", time_raw, ".")

try(source_python("setup_smtp.py"))

message_final <- paste0(py$message, result)

try(smtpObj$sendmail(my_email, my_email, message_final))
try(smtpObj$quit())

Python脚本("setup_smtp.py")如下:

The Python script ("setup_smtp.py") is like this:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Call from reticulate to log in to email
"""

import smtplib

my_email = '...'
my_password = '...'

smtpObj = smtplib.SMTP('smtp.office365.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(my_email, my_password)

message = """From: My Name <email address>
To: My Name <email address>
Subject: Test successful!
"""

推荐答案

此执行问题

当我从RStudio中逐行运行它时,此方法可以正常工作.问题是脚本按计划运行时不起作用

This works correctly when I run it line by line from within RStudio. The problem is that it doesn't work when the script runs on schedule

可能有多种原因:

您有多个Python版本,其中smtplib安装在一个版本(例如Python 2.7或Python 3.6)上,而另一个版本则没有.检查在命令行Rscript -e "print(Sys.which("python"))"和RStudio Sys.which("python")上使用的是哪个Python.明确定义要与网状use_python("/path/to/python")一起运行的Python.exe.

You have multiple Python versions where smtplib is installed on one version (e.g., Python 2.7 or Python 3.6) and not the other. Check which Python is being used at command line, Rscript -e "print(Sys.which("python"))" and RStudio, Sys.which("python"). Explicitly define which Python.exe to run with reticulate's use_python("/path/to/python").


您有多个R版本,其中Rscript使用的版本不同于RStudio.在以下两项中都检查R.home()变量:Rscript -e "print(R.home())"并在RStudio中调用R.home().在相应的R版本bin文件夹中明确调用所需的Rscript:/path/to/R #.#/bin/Rscript "/path/to/code.R".

You have multiple R versions where Rscript uses a different version than RStudio. Check R.home() variable in both: Rscript -e "print(R.home())" and call R.home() in RStudio. Explicitly call the required Rscript in appropriate R version bin folder: /path/to/R #.#/bin/Rscript "/path/to/code.R".


您在同一R版本上安装了多个reticulate软件包,它们位于不同的库位置,每个软件包调用不同的Python版本.检查矩阵:installed.package(),找到reticulate行.明确呼叫library(reticulate, lib.loc="/path/to/specific/library").

You have multiple reticulate packages installed on same R version, residing in different library locations, each calling a different Python version. Check with the matrix: installed.package(), locating the reticulate row. Explicitly call library(reticulate, lib.loc="/path/to/specific/library").

这篇关于使用网状调用Python脚本并发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆