繁体   English   中英

从命令行运行时 Python sendgrid 库错误

[英]Python sendgrid library error when run from command line

我设置了一个 sendgrid 帐户,我可以使用它通过 python 发送电子邮件。 When I run it in my spyder IDE it works great, but when run through the windows CMD i get <urlopen error unknown url type: https> .

我尝试将主机硬编码为sg = SendGridAPIClient(pw,host='https://api.sendgrid.com') 这适用于 IDE,但不适用于 CMD,我得到同样的错误。

我还尝试使用sg = SendGridAPIClient(pw,host=urllib.parse.quote('https://api.sendgrid.com'))删除“:”。 这会将错误更改为unknown url type: 'https%3A//api.sendgrid.com/v3/mail/send'这个想法是基于 SO post urllib.error.URLError: <urlopen error unknown url type: 'https> .

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *

api_key='your_api_key'

message = Mail(
from_email='me@example.com',
to_emails='you@example.com',
subject='SUBJECT',
    html_content='some body stuff')

try:
    sg = SendGridAPIClient(api_key)
    response = sg.send(message) #this line throws the error
    print('worked')

except Exception as e:

print(str(e))

当我在命令行中运行它时,我在脚本目录中使用“python.exe 'my_scirpt.py'”。

I was able to find the answer here: Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website

此问题与 anaconda 中的一些 DLL 有关。 我不确定最好的解决方法是什么。 我复制粘贴了 DLLS,如此处的一个答案所示。 不过,我将复制此答案的文本,因为它不是此线程上的最佳答案,并且使用 sendgrid 时不会出现此搜索结果,所以我认为我的问题确实增加了社区。 从另一个 SO 帖子:

"""

我在 2020 年 4 月面对它。 这些是我尝试过的选项,最后一个解决方案让我通过了。

问题:

当我在 Spyder IDE 中使用时,请求模块工作正常,但是当我尝试在 windows 中执行脚本时,它会因 SSL 错误而失败。 它适用于 HTTP 请求但对于 HTTPS 请求我得到 SSL 错误。

我厌倦了 Veify=True、False,也厌倦了 Certs。 同样的错误。 已删除证书 - conda remove certifi - 不起作用更新 openssl,证书 - 仍然相同的错误(请参阅: https://github.com/ContinuumIO/anaconda-issues/issues/494 )添加了路径变量 - 在新环境中创建相同的错误Anaconda - 同样的错误

修复它的解决方案:

解决方案来自: https://github.com/conda/conda/issues/8273

我已将以下文件从 Anaconda3\Library\bin 复制到 \Anaconda3\DLL

libcrypto-1_1-x64.* libssl-1_1-x64.*

"""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM