+ -
当前位置:首页 → 问答吧 → python3.2.2版本import urllib.request 提示错误

python3.2.2版本import urllib.request 提示错误

时间:2011-10-19

来源:互联网


Python版本是3.2.2
我想用urltrieve来下载文件
用IDLE编辑保存之后按F5运行
import urllib.request
urladd = 'http://www.icafe8.com/icafe/archive/DisklessServer1.1.0.0.exe'
localfile = 'i:/DiskLessServer.exe'
urllib.request.urlretrieve(urladd, localfile)


运行的时候 提示
ImportError: No module named request

但是如果我单独在python shell里面运行这
import urllib.request
urladd = 'http://www.icafe8.com/icafe/archive/DisklessServer1.1.0.0.exe'
localfile = 'i:/DiskLessServer.exe'
urllib.request.urlretrieve(urladd, localfile)

是可以正常运行并且将文件下载回来的

我尝试过用
import urilib
from urllib import request
也是同样的问题、保存py文件之后运行就提示
ImportError: No module named request
在python shell里面运行就正常

不知道哪里写错了还是有什么地方需要注意的?
请各位行者指点一二

万分感谢!!

作者: huaisha1224   发布时间: 2011-10-19

Python code

import urllib.request

def test():
    try:
        urladd = 'http://www.icafe8.com/icafe/archive/DisklessServer1.1.0.0.exe'
        localfile = 'c:/temp/DiskLessServer.exe'
        urllib.request.urlretrieve(urladd, localfile)
    except (Exception) as ex:
        print(ex)
        
if __name__=='__main__':
    test()
    



D:\test\epp>c:\python32\python cu20111019a.py

D:\test\epp>dir c:\temp
 驱动器 C 中的卷没有标签。
 卷的序列号是 F07C-BDCA

 c:\temp 的目录

2011/10/19 19:26 <DIR> .
2011/10/19 19:26 <DIR> ..
2011/10/19 19:26 10,140,016 DiskLessServer.exe
2011/08/15 14:03 2 log.txt
2011/08/19 15:53 143 temp.py
2011/10/14 13:55 23 test.log
  4 个文件 10,140,184 字节
  2 个目录 23,355,830,272 可用字节

没报错啊,很正常的下载到了那个文件。

作者: panzifei   发布时间: 2011-10-19

我估计你用2.x版本的python来运行那个脚本了

作者: panzifei   发布时间: 2011-10-19

D:\test\epp>c:\python27\python cu20111019a.py
Traceback (most recent call last):
  File "cu20111019a.py", line 1, in <module>
  import urllib.request
ImportError: No module named request

像这样就报错了

作者: panzifei   发布时间: 2011-10-19

引用 1 楼 panzifei 的回复:

Python code

import urllib.request

def test():
try:
urladd = 'http://www.icafe8.com/icafe/archive/DisklessServer1.1.0.0.exe'
localfile = 'c:/temp/DiskLessServer.exe'
url……

我是初学python 应该用3.2版本还是2.7版本呀
我发现版本之间的函数用法不太一样

作者: huaisha1224   发布时间: 2011-10-19