python - 为什么我的代码会打开另一个文件并处理错误异常?
问题描述
我正在尝试通过 python 在 C:\Users\WilsonNg\Documents\Internship 中运行一个文件(asana_get_user.py)。
但是,当我运行它时,我得到一个属性错误:
错误信息
但是,由于某种原因,此代码完成后发生了另一个异常错误:
异常错误
所以它在同一个文件夹中打开了另一个文件(email.py)并创建了一个异常错误。
关于它为什么引用我的 email.py 文件的任何想法?
asana_get_user.py
import asana
# replace with your personal access token.
personal_access_token = '0/2c...'
# Construct an Asana client
client = asana.Client.access_token(personal_access_token)
# Set things up to send the name of this script to us to show that you succeeded! This is optional.
client.options['client_name'] = "hello_world_python"
# Get your user info
me = client.users.me()
# Print out your information
print ("Hello world! " + "My name is " + me['name'] + " and I my primary Asana workspace is " + me['workspaces'][0]['name'] + ".")
电子邮件.py
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
def send_message(service, user_id, message):
"""Send an email message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
message: Message to be sent.
Returns:
Sent Message.
"""
try:
message = (service.users().messages().send(userId=user_id, body=message)
.execute())
print ('Message Id: %s' % message['id'])
return message
except (errors.HttpError, error):
print ('An error occurred: %s' % error)
subject = "This is a test subject"
message = "This is a test content"
sent_to = "test@test.com"
sender = "test@test.com"
sending_msg = create_message(sender,sent_to,subject,message)
send_message()
解决方案
Please put a check in email.py
as if __name__ == '__main__':
before subject = "This is a test subject"
.
email.py
is being run as the import asana
is called.
if __name__ == '__main__':
subject = "This is a test subject"
message = "This is a test content"
sent_to = "test@test.com"
sender = "test@test.com"
sending_msg = create_message(sender,sent_to,subject,message)
send_message()
Read about it here.
推荐阅读
- nexus3 - 如何为 Gauge 插件配置 Nexus3 存储库?
- powerapps - 如何将 google sheet excel 列值添加到电源应用程序的下拉列表中
- javascript - Javascript on Press Keydown“Q”按下键“1”和“2”
- ruby - 从 CSV 解析唯一值
- django - 获取 django 下拉菜单项查询集
- dart - Flutter 组合框和数字输入
- zsh - zsh:运行 zle 小部件后的刷新提示
- javascript - 我在关键操作中遇到错误
- python - 不能在 python virtualenv 中使用 pip
- vb.net - 如何找出 ExecuteScalar() 是 dbnull