python2 的代码在python3环境中运行常见报错及解决方式

个人目前接触python还不是很久,也不是很深入。。。。

所以,只能总结下我遇到的坑。。(好多给忘了)。。以后会经常更新的。

1、no module named "urllib2"

    在3中urllib2和urllib.request合并了 所以将 urllib2改为urllib.request应该就能运行了。

2、TabError: Inconsistent use of tabs and spaces in indentation

    python要用tab完成缩进的,不能使用空格,否则会报错

3、module 'urllib' has no attribute 'urlopen'

    urlopen放到urllib.request中去了

4、modue 'rullib' has no attribute 'urlencode'

    改为urllib.parse.uelencode就可以了

5、post data should be bytes, an iterable of bytes, or a file object.it cannot be of type str

    造成此错误的是因为你的文件中使用了中文,如果是json格式的改为json.dumps().encode(encoding='UTF8')就行了

    

猜你喜欢

转载自blog.csdn.net/qq_24391357/article/details/80266964
今日推荐