1. 程式人生 > >python的Error集,17個新手常見Python執行時錯誤

python的Error集,17個新手常見Python執行時錯誤

python的Error集,17個新手常見Python執行時錯誤

python及相關工具安裝Error集
1. 如果升級python版本中出現error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
解決方法:
 vim /etc/ld.so.conf
新增:/usr/local/lib
執行:/sbin/ldconfig -v

2. 如果出現unknown url type: https
解決方法:
安裝openssl-devel

3
. 出現numpy.distutils.system_info.NotFoundError: no lapack/blas resources found 解決辦法: 安裝atlas atlas-devel.x86_64 4. 出現 Failedto build these modules: binascii zlib 解決辦法: 安裝zlib並重新編譯python 5. 安裝numpy報錯RuntimeError: Running cythonize failed! 解決辦法: 編譯安裝cython 6. 出現error: library dfftpack has Fortran sources but no Fortran compiler found 解決辦法: 安裝 gcc-gfortran.x86_64 7
. 出現ImportError: No module named bz2: 解決辦法: yum install -y bzip2* cd ../Python-2.7.3/Modules/zlib ./configure make make install cd ../../ python setup.py install 重新編譯python 8. 出現ImportError: libpython2.7.so.1.0: cannot open shared object file: No such file or directory 解決辦法: 編譯時只有加上--enable-shared,才會編譯這個共享庫,預設的位置是python可執行程式所在目錄的lib目錄下,如/usr/local/python2.7.4

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  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!' ) =  Foo() a.myMethod()