python3 urllib导入错误

时间:2018-05-27 12:44:02

标签: python-3.x urllib

我搜索了一些网页,但不符合我的情况。它在jupyter笔记本和shell上运行正常,但在pycharm 2017.1.3上运行不正常。版本是Python 3.6.0 :: Anaconda 4.3.1(64位)

from urllib import request

with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
    data = f.read()
    print('Status:', f.status, f.reason)
    for k, v in f.getheaders():
        print('%s: %s' % (k, v))
    print('Data:', data.decode('utf-8'))

错误的堆栈跟踪

"D:\Program Files (x86)\Anaconda3\python.exe" D:/pyProject/liao/usual/urllib.py
Traceback (most recent call last):
  File "D:/pyProject/liao/usual/urllib.py", line 8, in <module>
    from urllib import request
  File "D:\pyProject\liao\usual\urllib.py", line 8, in <module>
    from urllib import request
ImportError: cannot import name 'request'

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题。请先将文件重命名为其他名称,因为导入将在找到urllib.py软件包之前找到您的文件名urllib