python - 为什么我的代码打开另一个文件并处理错误异常?

标签 python exception error-handling

我正在尝试通过python在C:\ Users \ WilsonNg \ Documents \ Internship中运行文件(asana_get_user.py)。

但是,当我运行它时,出现属性错误:

错误消息

enter image description here

但是,由于某种原因,此代码完成后还会发生另一个异常错误:

异​​常错误

enter image description here

因此,它在同一文件夹中打开了另一个文件(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'] + ".")

email.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()

最佳答案

请在email.py之前在if __name__ == '__main__':中将其作为subject = "This is a test subject"进行检查。
email.py被调用时正在运行import asana

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()

了解有关here的信息。

关于python - 为什么我的代码打开另一个文件并处理错误异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800989/

相关文章:

java - 了解异常处理 - 重新抛出异常

c# - 引发登录/注册错误的异常是否合适?

python "if len(A) is not 0"与 "if A"语句

Python 不允许在三元中有多个 return() 语句;在美学上也令人愉悦的可能替代方案?

python - python中的系统函数是什么

C++ dynamic_cast 异常

javascript - 在 NodeJS 中模拟发出 "Socket hang up"

python - 如果找不到元素或 Selenium 等待函数中发生超时异常,如何跳到下一个 url

c++ - 嵌入式系统中的错误处理

python - 您如何从 PRAW 中的提交对象获取 url?