[英]Can't import package file (no module named…) (Python)
I am receving this error while I am try to run the code (from CMD): 我在尝试运行代码(从CMD)时收到此错误:
ModuleNotFoundError: No module named 'numbers.hog'; numbers is not a package
Here is the hog.py
file code... 这是
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
...and the main ( train.py
) which return the error. ...以及返回错误的主要代码(
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()
I don't uderstand why it gives me error on module package. 我不明白为什么它给我模块包装上的错误。
numbers
is a package, why it don't import it as well (as it seems) ? numbers
是一个包,为什么它也不能导入(看上去)?
UPDATE: tried to put from .hog import HOG
and then execute from CMD..It prints: 更新:尝试
from .hog import HOG
,然后从CMD执行。它打印:
No module named '__main__.hog'; '__main__' is not a package
Is it crazy ? 疯了吗?
hog.py
is in the main package together with the other files. hog.py
与其他文件一起位于主软件包中。 As you can see, it also contains HOG class.... Can't understand.. Some one can reproduce the error ? 如您所见,它还包含HOG类。...无法理解..有人可以重现该错误?
In the IDE console it prints: 在IDE控制台中,它打印:
usage: train.py [-h] -d DATASET -m MODEL
train.py: error: the following arguments are required: -d/--dataset, -m/--model
This should be correct as soon as it is executed in IDE because the program MUST run in CMD. 在IDE中执行该命令后,它应该是正确的,因为该程序必须在CMD中运行。
UPDATE 2: for who is interested, this is the project https://github.com/VAUTPL/Number_Detection 更新2:对谁感兴趣,这是项目https://github.com/VAUTPL/Number_Detection
Change from numbers.hog import HOG
to from hog import HOG
and from numbers.hog import HOG
更改为from hog import HOG
和
change from numbers import dataset
to import dataset
. from numbers import dataset
更改为import dataset
。
You are already in the "numbers" package so you don't have to precise it again when you import it. 您已经在“数字”包中,因此在导入时不必再次进行调整。
When you type from numbers import dataset
, Python will look for a package numbers
(inside the actual package) that contains a dataset.py
file. 当您
from numbers import dataset
键入内容时,Python将在包含实际数据包的包中查找一个from numbers import dataset
numbers
(其中包含一个dataset.py
文件)。
If your train.py
was outside the numbers
package then you have to put the package name ( numbers
) before. 如果您的
train.py
不在numbers
包中,则必须在包名前加上numbers
。
Important numbers is a python standard package https://docs.python.org/2/library/numbers.html 重要数字是python标准软件包https://docs.python.org/2/library/numbers.html
Check if you are not really importing that package or rename your package to a more specific name. 检查您是否真的没有导入该程序包,或者将您的程序包重命名为更特定的名称。
Also: 也:
It might looks like python doesnt recognize your package. 看起来python无法识别您的软件包。
Open a python shell and write: 打开python shell并编写:
import sys
print sys.path
Check if your number path is there. 检查您的号码路径是否存在。
If it's not there you have to add it. 如果不存在,则必须添加它。
sys.path.insert(0, "/path/to/your/package_or_module")
Your train.py file is already in the package "numbers", so you don't have to import numbers. 您的train.py文件已经在“数字”包中,因此您不必导入数字。
Try this instead: 尝试以下方法:
from hog import HOG
I saw in comment that it gives you "error (red line)". 我在评论中看到它给您“错误(红线)”。
Can you be more precise, because I don't see errors there. 您能更精确吗,因为我看不到那里的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.