Python常见错误

1、UnboundLocalError: local variable 'xxx' referenced before assignment

参考链接:

http://blog.csdn.net/onlyanyz/article/details/45009697

https://www.cnblogs.com/fendou-999/p/3822028.html

2、IndentationError: expected an indented block

缩进错误

3、PyCharm下解决Unresolved Reference问题

https://blog.csdn.net/haiyanggeng/article/details/79215819

4、<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>

在HTMLTestRunner.py的631行,

print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)

或者

print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

修改成

sys.stderr.write('\nTime Elapsed: %s\n' % (self.stopTime - self.startTime))

5、warning:statement has no effect:申明无效

检查是否给全局变量赋值了。

6、shadows name 'xxx' from outer scope

当你在外部有个相同名称的变量在方法内部被重新指定了新的值,也就是说你在外部的相同名称的变量压根就没有任何作用。

python的变量作用域: 
模块对应global, 
最内层为local, 
外层为nonlocal 
变量查找顺序:内层作用域->外层->全局->builtin 
只有class、def和lamda会改变作用域

读取变量的时候,如果local没有,查找nonlocal,然后global 
写变量的时候,如果没有指明nonlocal/global, 就是在局部作用域定义一个新的变量

Python 五个知识点搞定作用域:http://python.jobbole.com/86465/

7、unexpected argument

函数中添加一个参数试试:params=None

8、‘xxx’takes 0 positional arguments but 1 was given

这个函数xxx不需要参数,但是函数却被传递了一个参数

9、python positional argument follows keyword argument

当参数的位置不正确时,就会报上面的错误;

关键字参数必须跟随在位置参数后面! 因为python函数在解析参数时, 是按照顺序来的, 位置参数是必须先满足, 才能考虑其他可变参数.

10、ValueError: no such test method in <class '__main__.TestInterfaceRun'>: test_01
 

11、TypeError: a bytes-like object is required, not 'str'

在字符串前加b

https://www.cnblogs.com/dpf-learn/p/8028121.html

 

12、UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 259: invalid continuation byte

try:
    html = html.decode('UTF-8')  # html是bytes类型的字符串
except UnicodeDecodeError as reason:
    print(reason)
    html = html.decode('GBK')
except UnicodeDecodeError as reason:
    print(reason)

13、同级目录导入问题

14、pycharm无法import已安装模块

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值