python的Error集,17个新手常见Python运行时错误
python及相关工具安装Error集 . 如果升级python版本中出现error .so.1.0: cannot open shared object file: No such file or directory 解决方法: vim /etc/ld.so.conf 添加:/usr/local/lib 执行:/sbin/ldconfig -v . 如果出现unknown url type: https 解决方法: 安装openssl-devel . 出现numpy.distutils.system_info.NotFoundError: no lapack/blas resources found 解决办法: 安装atlas atlas-devel.x86_64 . 出现 Failedto build these modules: binascii zlib 解决办法: 安装zlib并重新编译python . 安装numpy报错RuntimeError: Running cythonize failed! 解决办法: 编译安装cython . 出现error: library dfftpack has Fortran sources but no Fortran compiler found 解决办法: 安装 gcc-gfortran.x86_64 . 出现ImportError: No module named bz2: 解决办法: yum install -y bzip2* cd ../Python-/Modules/zlib ./configure make make install cd ../../ python setup.py install 重新编译python . 出现ImportError: libpython2..so.1.0: cannot open shared object file: No such file or directory 解决办法: 编译时只有加上--enable-shared,才会编译这个共享库,默认的位置是python可执行程序所在目录的lib目录下,如/usr/local/python2.7.4 9.SyntaxError: Non-ASCII character '\xe6' in file , but no encoding declared; 在第一行增加:#-*- coding: UTF-8 -*- 。
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)
该错误将发生在类似如下代码中:
1
2
|
if spam = = 42 print ( 'Hello!' ) |
2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)
= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:
1
2
|
if spam = 42 : print ( 'Hello!' ) |
3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
print ( 'Hello!' ) print ( 'Howdy!' ) 或者: if spam = = 42 : print ( 'Hello!' ) print ( 'Howdy!' ) 或者: if spam = = 42 : print ( 'Hello!' ) |
4)在 for 循环语句中忘记调用 len() (导致“TypeError: 'list' object cannot be interpreted as an integer”)
通常你想要通过索引来迭代一个list或者string的元素,这需要调用 range() 函数。要记得返回len 值而不是返回这个列表。
该错误发生在如下代码中:
1
2
3
|
spam = [ 'cat' , 'dog' , 'mouse' ] for i in range (spam): print (spam[i]) |
5)尝试修改string的值(导致“TypeError: 'str' object does not support item assignment”)
string是一种不可变的数据类型,该错误发生在如下代码中:
1
2
3
|
spam = 'I have a pet cat.' spam[ 13 ] = 'r' print (spam) |
而你实际想要这样做:
1
2
3
|
spam = 'I have a pet cat.' spam = spam[: 13 ] + 'r' + spam[ 14 :] print (spam) |
6)尝试连接非字符串值与字符串(导致 “TypeError: Can't convert 'int' object to str implicitly”)
该错误发生在如下代码中:
1
2
|
numEggs = 12 print ( 'I have ' + numEggs + ' eggs.' ) |
而你实际想要这样做:
1
2
3
4
5
6
7
|
numEggs = 12 print ( 'I have ' + str (numEggs) + ' eggs.' ) 或者: numEggs = 12 print ( 'I have %s eggs.' % (numEggs)) |
7)在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)
该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
|
print (Hello!') 或者: print ('Hello!) 或者: myName = 'Al' print ( 'My name is ' + myName + . How are you?') |
8)变量或者函数名拼写错误(导致“NameError: name 'fooba' is not defined”)
该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
|
foobar = 'Al' print ( 'My name is ' + fooba) 或者: spam = ruond( 4.2 ) 或者: spam = Round ( 4.2 ) |
9)方法名拼写错误(导致 “AttributeError: 'str' object has no attribute 'lowerr'”)
该错误发生在如下代码中:
1
2
|
spam = 'THIS IS IN LOWERCASE.' spam = spam.lowerr() |
10)引用超过list最大索引(导致“IndexError: list index out of range”)
该错误发生在如下代码中:
1
2
|
spam = [ 'cat' , 'dog' , 'mouse' ] print (spam[ 6 ]) |
11)使用不存在的字典键值(导致“KeyError:‘spam’”)
该错误发生在如下代码中:
1
2
|
spam = { 'cat' : 'Zophie' , 'dog' : 'Basil' , 'mouse' : 'Whiskers' } print ( 'The name of my pet zebra is ' + spam[ 'zebra' ]) |
12)尝试使用Python关键字作为变量名(导致“SyntaxError:invalid syntax”)
Python关键不能用作变量名,该错误发生在如下代码中:
1
|
class = 'algebra' |
Python3的关键字有:and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
13)在一个定义新变量中使用增值操作符(导致“NameError: name 'foobar' is not defined”)
不要在声明变量时使用0或者空字符串作为初始值,这样使用自增操作符的一句spam += 1等于spam = spam + 1,这意味着spam需要指定一个有效的初始值。
该错误发生在如下代码中:
1
2
3
|
spam = 0 spam + = 42 eggs + = 42 |
14)在定义局部变量前在函数中使用局部变量(此时有与局部变量同名的全局变量存在)(导致“UnboundLocalError: local variable 'foobar' referenced before assignment”)
在函数中使用局部变来那个而同时又存在同名全局变量时是很复杂的,使用规则是:如果在函数中定义了任何东西,如果它只是在函数中使用那它就是局部的,反之就是全局变量。
这意味着你不能在定义它之前把它当全局变量在函数中使用。
该错误发生在如下代码中:
1
2
3
4
5
|
someVar = 42 def myFunction(): print (someVar) someVar = 100 myFunction() |
15)尝试使用 range()创建整数列表(导致“TypeError: 'range' object does not support item assignment”)
有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式。然而,你需要记住 range() 返回的是 “range object”,而不是实际的 list 值。
该错误发生在如下代码中:
1
2
|
spam = range ( 10 ) spam[ 4 ] = - 1 |
也许这才是你想做:
1
2
|
spam = list ( range ( 10 )) spam[ 4 ] = - 1 |
(注意:在 Python 2 中 spam = range(10) 是能行的,因为在 Python 2 中 range() 返回的是list值,但是在 Python 3 中就会产生以上错误)
16)不错在 ++ 或者 -- 自增自减操作符。(导致“SyntaxError: invalid syntax”)
如果你习惯于例如 C++ , Java , PHP 等其他的语言,也许你会想要尝试使用 ++ 或者 -- 自增自减一个变量。在Python中是没有这样的操作符的。
该错误发生在如下代码中:
1
2
|
spam = 1 spam + + |
也许这才是你想做的:
1
2
|
spam = 1 spam + = 1 |
17)忘记为方法的第一个参数添加self参数(导致“TypeError: myMethod() takes no arguments (1 given)”)
该错误发生在如下代码中:
1
2
3
4
5
|
class Foo(): def myMethod(): print ( 'Hello!' ) a = Foo() a.myMethod() |
python的Error集,17个新手常见Python运行时错误的更多相关文章
- [转]17个新手常见Python运行时错误
原址:http://www.oschina.net/question/89964_62779?p=1 当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的 ...
- 17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- 【转载】17个新手常见Python运行时错误
转自:http://www.cnblogs.com/smile-ls/archive/2013/05/20/3088393.html 当初学 Python 时,想要弄懂 Python 的错误信息的含义 ...
- python--17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- 新手常见Python运行时错误
经过整理与在实际中遇到的问题,将新手经常遇到的汇总下,以便自己犯傻又这么干了 1)"SyntaxError :invalid syntax",语法错误 A.查看是否在 if , e ...
- python脚本实现集群检测和管理
python脚本实现集群检测和管理 场景是这样的:一个生产机房,会有很多的测试机器和生产机器(也就是30台左右吧),由于管理较为混乱导致了哪台机器有人用.哪台机器没人用都不清楚,从而产生了一个想法-- ...
- python 时间合集 一
**以下内容均为我个人的理解,如果发现错误或者疑问可以联系我共同探讨**#### python中4种时间表示形式:1.格式化时间字符串 2.时间戳 3.时间元祖 4.时间对象- string_time ...
- Python学习笔记—自动化部署【新手必学】
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:唯恋殊雨 目录 pexpect fabric pexpect P ...
- Python pip – error: invalid command ‘bdist_wheel’
原文@http://software-engineer.gatsbylee.com/python-pip-error-invalid-command-bdist_wheel/ Python pip – ...
随机推荐
- 在Windows7防火墙允许指定的端口
在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在windows7下启动了网络服务,需要开启 ...
- apache+mysql+php环境的手动搭建
一.搭建Apache Http Server 官方下载地址:http://www.apachehaus.com/cgi-bin/download.plx 搭建环境:win10 64位 WIN10 64 ...
- 浅谈Spring(四)
一.Spring+MyBatis整合 spring大大简化了Mybatis的开发步骤. 1.MyBatis的开发要点: mybatis-config.xml配置文件:配置与数据库的链接.mapper文 ...
- Hibernate学习之常用方法比较
一.save()和persist()方法 使用 save() 方法保存持久化对象时,该方法返回该持久化对象的标识属性值(即对应记录的主键值):但使用 persist() 方法来保存持久化对象时,该方法 ...
- leetcode 60. Permutation Sequence(康托展开)
描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- linux杂记(六)档案权限
[KANO@kelvin ~]$ ls -al 总用量 drwx------. KANO KANO 10月 : . drwxr-xr-x. root root 9月 : .. drwxrwxr-x. ...
- Android 利用摄像头指尖测试心率
过摄像头来获得心率,搜了一下这个技术真不是噱头,据说在iPhone早有实现,主要原理是:当打开软件时,手机的闪光灯也会被自动打开,用户将手指放在摄像头上时,指尖皮下血管由于有血液被压入,被光源照射的手 ...
- c++ 静态多态与动态多态
多态polymorphism是指具有多种形态的情况,它能根据单一的标记关联不同的行为.多态是面向对象程序设计的基础.在面向对象程序设计中的多态是一种运行时的多态.C++中有两种多态,称为动多态(运行时 ...
- CVTE 一面
在网上做完了测评之后,当天就收到面试的通知了,CVTE效率真高.第二天就去参加面试,面试前紧张了一把,后来去到之后发现只有几个应聘者,很多面试官前面都没人,估计现在中午一点,所以都去吃饭了.我和一个同 ...
- [置顶] C++基础之六:运算符的重载
网上太多有关运算符的重载了,但是写的太过的详细,不适合新手入门,特别是那什么++和--的前增量后增量重载,一元二元运算符重载,特殊运算符,下标运算符,new和delete,甚至是指针运算符的重载,吓退 ...