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

“AttributeError:部分初始化的模块‘pytube’没有属性‘YouTube’(很可能是由于循环导入)”

这是代码:

import pytube as p
video_url = input("Enter the link: ")
youtube = p.YouTube(video_url)
filters = youtube.streams.filter(progressive=True, file_extension="mp4")
filters.get_highest_resolution().download("MyPath")
Run Code Online (Sandbox Code Playgroud)

我尝试编写代码来下载 YouTube 视频。但它抛出一个错误:

AttributeError:部分初始化的模块“pytube”没有属性“YouTube”(很可能是由于循环导入)`

完整的错误消息

我什至从网上复制粘贴代码,重新安装Python,并重新安装pytube,但没有任何效果。更令人沮丧的是,当我几个月前执行它时,它运行得很好。

python importerror pytube

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