Imaplib.py:';NoneType';对象没有属性';替换';
问题描述
我已经在GitHub上设置了一个工作流,当我运行它时,我收到以下错误:
1 Run python app.py
2 python app.py
3 shell: /usr/bin/bash -e {0}
4 env:
5 pythonLocation: /opt/hostedtoolcache/Python/3.9.6/x64
6 LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.9.6/x64/lib
8 Traceback (most recent call last):
9 File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/app.py", line 9, in <module>
10 main()
11 File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/app.py", line 5, in main
12 mail_connect()
13 File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/read_email.py", line 13, in mail_connect
14 mailbox = MailBox(SMTP_SERVER).login(
15 File "/opt/hostedtoolcache/Python/3.9.6/x64/lib/python3.9/site-packages/imap_tools/mailbox.py", line 46, in login
16 login_result = self.box.login(username, password)
17 File "/opt/hostedtoolcache/Python/3.9.6/x64/lib/python3.9/imaplib.py", line 610, in login
18 typ, dat = self._simple_command('LOGIN', user, self._quote(password))
19 File "/opt/hostedtoolcache/Python/3.9.6/x64/lib/python3.9/imaplib.py", line 1222, in _quote
20 arg = arg.replace('\', '\\')
21 AttributeError: 'NoneType' object has no attribute 'replace'
22 Error: Process completed with exit code 1.
此imaplib.py
文件不在我的项目中,但我正在导入imap-tools
依赖项,我不知道这是否可能是原因。
有没有可能这和这件事有关?唯一的解决方案是寻找其他依赖项,或者是否有其他选择?
编辑1:
以下是我在工作流中运行的文件:
#app.py
from read_email import mail_connect
def main():
mail_connect()
if __name__ == '__main__':
main()
在GitHub操作中执行的工作流中:
#main.yml
name: Python Script Workflow
on:
- cron: "*/10 11-13 * * 1-5"
workflow_dispatch:
inputs:
name:
required: true
default: 'Tweet'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository content
uses: actions/checkout@v2 # Checkout the repository content to github runner.
- name: Setup Python Version
uses: actions/setup-python@v2
with:
python-version: 3.9.6
- name: Install Python dependencies
uses: py-actions/py-dependency-install@v2
with:
path: "requirements.txt"
- name: Execute Python script # Run the app.py
run: python app.py
这是requirements.txt
:
imap-tools==0.49.1
python-dotenv==0.19.0
tweepy==4.0.1
编辑2:
这里是唯一使用imap_toolts
的文件:
#read_email.py
from imap_tools import MailBox, MailMessageFlags, A
from os import getenv
from dotenv import load_dotenv
from post_tweet import post_tweet
def mail_connect():
load_dotenv()
FROM_EMAIL = getenv('FROM_EMAIL')
FROM_PWD = getenv('FROM_PWD')
SMTP_SERVER = "imap.gmail.com"
mailbox = MailBox(SMTP_SERVER).login(
FROM_EMAIL, FROM_PWD, initial_folder='INBOX')
read_email(mailbox)
def read_email(mailbox):
posts = []
for msg in mailbox.fetch():
posts = msg.text.replace('*', '').split('
')
posts = posts[2:-3]
mark_as_read(mailbox)
archive_message(mailbox, msg.uid)
post_tweet(posts)
def mark_as_read(mailbox):
flags = (MailMessageFlags.ANSWERED, MailMessageFlags.FLAGGED)
mailbox.flag(mailbox.fetch(A(seen=False)), flags, True)
mailbox.flag(mailbox.fetch("SENTON 01-Jan-2021"),
MailMessageFlags.SEEN, False)
def archive_message(mailbox, msg_uid):
move_to = 'Tweeted'
mailbox.move(msg_uid, move_to)
解决方案
问题是您的电子邮件和密码的环境变量未在GitHub工作流环境中设置。按如下方式修改您的工作流的YAML:
environment:
name: production
- name: Execute Python script # Run the app.py
env:
FROM_PWD: ${{secrets.FROM_PWD}}
FROM_EMAIL: ${{secrets.FROM_EMAIL}}
run: python app.py
如有必要,请根据您在GitHub中创建机密时的命名方式进行修改。
相关文章