相关疑难解决方法(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万
查看次数

Python Tkinter Tk未定义

我需要帮助,使用Python 3.5.2在Ubuntu 16.04.1上运行这个简单的Tkinter程序.

这是代码:

from tkinter import *

root = Tk()
mylabel = Label(root, text="Test")
mylabel.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

代码as-is给了我这个错误:

Traceback (most recent call last):
  File "tkinter.py", line 8, in <module>
    from tkinter import *
  File "/home/dylan/Documents/proj/python/tkinter.py", line 10, in <module>
    root = Tk()
NameError: name 'Tk' is not defined
Run Code Online (Sandbox Code Playgroud)

我注意到在浏览其他问题时,在制作Tkinter程序时会出现一些可能出错的热门内容.

  1. 未安装Tkinter. 我在更新软件包,保存文件和运行程序之前安装了软件包python-tkpython3-tk软件包python3 tkinter.py.

注意:将我的文件重命名为其他内容会tkinter.py导致一个奇怪的错误:

Traceback (most recent call last):
  File "mytkinter.py", line 8, in <module>
    from tkinter import *
ImportError: bad magic …
Run Code Online (Sandbox Code Playgroud)

user-interface tkinter python-3.x

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