相关疑难解决方法(0)

从脚本导入已安装的包会引发"AttributeError:module has no attribute"或"ImportError:无法导入名称"

我有一个名为的脚本requests.py导入请求包.该脚本无法访问包中的属性,也无法导入它们.为什么这不起作用,我该如何解决?

以下代码提出了一个问题AttributeError.

import requests

res = requests.get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    import requests
  File "/Users/me/dev/rough/requests.py", line 3, in <module>
    requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)

以下代码提出了一个问题ImportError.

from requests import get

res = get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "requests.py", line 1, in <module>
    from requests import get
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    from requests import get
ImportError: cannot import name 'get' …
Run Code Online (Sandbox Code Playgroud)

python exception python-module shadowing

43
推荐指数
2
解决办法
1万
查看次数

得到错误`没有名为mime.text`的模块[Python]

此代码"假设"发送电子邮件.

import smtplib
#SERVER = "localhost"

FROM = 'myEmail@email.com'

TO = ["toEmail@email.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息.

Traceback (most recent call last):
  File "C:\Users\myCpu\Documents\myFiles\python\test wy.py", line 1, in <module>
    import smtplib
  File "C:\Python27\lib\smtplib.py", line 46, …
Run Code Online (Sandbox Code Playgroud)

python email sendmail python-2.7

2
推荐指数
2
解决办法
1万
查看次数