2017-07-31 118 views
0

我receving這個錯誤當我嘗試運行(從CMD)的代碼:無法導入包文件(沒有名爲模塊...)(Python)的

ModuleNotFoundError: No module named 'numbers.hog'; numbers is not a package 

enter image description here

下面是hog.py文件代碼...

from skimage import feature 

class HOG: 
    def __init__(self, orientations = 9, pixelsPerCell = (8, 8), 
     cellsPerBlock = (3, 3), normalize = False): 
     self.orienations = orientations 
     self.pixelsPerCell = pixelsPerCell 
     self.cellsPerBlock = cellsPerBlock 
     self.normalize = normalize 

def describe(self, image): 
    hist = feature.hog(image, 
    orientations = self.orienations, 
    pixels_per_cell = self.pixelsPerCell, 
    cells_per_block = self.cellsPerBlock, 
    normalize = self.normalize) 

    return hist 

...和主(train.py),它返回錯誤。

from sklearn.svm import LinearSVC 
from numbers.hog import HOG 
from numbers import dataset 
import argparse 
import pickle as cPickle 


ap = argparse.ArgumentParser() 
ap.add_argument("-d", "--dataset", required = True, 
    help = "path to the dataset file") 
ap.add_argument("-m", "--model", required = True, 
    help = "path to where the model will be stored") 
args = vars(ap.parse_args()) 


(digits, target) = dataset.load_digits(args["dataset"]) 
data = [] 

hog = HOG(orientations = 18, pixelsPerCell = (10, 10), 
    cellsPerBlock = (1, 1), normalize = True) 

for image in digits: 
    image = dataset.deskew(image, 20) 
    image = dataset.center_extent(image, (20, 20)) 

    hist = hog.describe(image) 
    data.append(hist) 

model = LinearSVC(random_state = 42) 
model.fit(data, target) 

f = open(args["model"], "w") 
f.write(cPickle.dumps(model)) 
f.close() 

我不明白爲什麼它給了我錯誤的模塊包。 numbers是一個包,它爲什麼不導入它(好像它)?

enter image description here

UPDATE:試圖把from .hog import HOG,然後從CMD..It打印執行:

No module named '__main__.hog'; '__main__' is not a package 

難道瘋了嗎? hog.py與其他文件一起位於主包中。正如你所看到的,它也包含HOG類....不明白..有人可以重現錯誤?

在它打印IDE控制檯:

usage: train.py [-h] -d DATASET -m MODEL 
train.py: error: the following arguments are required: -d/--dataset, -m/--model 

這應該因爲它是在IDE中執行,因爲程序必須在CMD運行,一旦是正確的。

更新2:誰是有興趣,這是項目https://github.com/VAUTPL/Number_Detection

+0

您是否嘗試將「從numbers.hog import HOG」替換爲「from hog import HOG」?畢竟這兩個文件都在同一個目錄中。 –

+0

它給我錯誤(紅線)這樣寫... – Link

回答

1



變化from numbers.hog import HOGfrom hog import HOG
變化from numbers import datasetimport dataset

您已經在「數字」包中,因此您在導入時不必再精確一次。
當您鍵入from numbers import dataset時,Python將查找包含dataset.py文件的包numbers(在實際包中)。

如果您的train.py不在numbers包中,那麼您必須先放置包名(numbers)。

0

重要 號是一個Python標準封裝 https://docs.python.org/2/library/numbers.html

檢查,如果你是不是真的導入一個包或重命名你的包裝更具體的名稱。

另外:

它可能看起來像蟒蛇犯規認識你的包。

打開一個python殼和寫:

import sys 
print sys.path 

檢查你的電話號碼路徑是存在的。

如果不在那裏,您必須添加它。

​​
+0

給我回'不能導入名稱豬'..但它接受它作爲原來的修改「從numbers.hog導入HOG」 – Link

+0

毫米蟒蛇不承認你的包裹,我更新我的答案 – lapinkoira

+0

我更新了我的答案。檢查你是否不導入Python標準號碼包並重命名/添加你的包到Python路徑 – lapinkoira

0


你train.py文件已在包中的「數字」,這樣你就不必進口數字。

試試這個:

from hog import HOG 

我在評論看到它給你 「的錯誤(紅線)」。
你能更精確嗎,因爲我沒有在那裏看到錯誤。

+0

強調用紅線...是的,它仍然給出錯誤... – Link

+0

對不起,我不是英語,我不明白「強調與紅線」,哪個錯誤? – Zcode

+0

https://s2.postimg.org/ct1fgbund/stressed.jpg ----這也有可能我說錯了......好吧,下劃線.. – Link

相關問題