1. 程式人生 > >python學習中遇到的幾個報錯

python學習中遇到的幾個報錯

1.d:

cd d:\AI-learn-daima\KNN

>>>e:\Python34\python.exe

import kNN

group,labels = kNN.creatDataSet()

kNN.myclass([0,0],group,labels,3)

報錯:

1.ImportError: No module named “myclass”

在ide中執行python程式,都已經在預設的專案路徑中,所以直接執行是沒有問題的。但是在cmd中執行程式,所在路徑是python的搜尋路徑,如果涉及到import引用就會報類似ImportError: No module named xxx這樣的錯誤,解決方法:

在報錯的模組中新增:

import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

2.dict object has no attribute iteritems

Python2和Python3存在版本上的不相容性

Python3.5中:iteritems變為items