我目前正在使用Go 1.12模块,对导入真的很累。
我正在使用gin(web微服务)和gorm(golang orm)制作rest-api。在golang模块中工作时一切正常。但是在导入本地软件包时遇到了麻烦
目录树:
go.mod:
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from GPSPhoto import gpsphoto
import numpy as np
import imutils
import pickle
import cv2
import os, sys
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True,
help="path to input image")
ap.add_argument("-m", "--model", required=True,
help="path to trained model")
ap.add_argument("-l", "--label", required=True,
help="path to label binarizer")
ap.add_argument("-k", "--ketqua", required=True,
help="path to output image")
args = vars(ap.parse_args())
image = cv2.imread(args["input"])
output = imutils.resize(image, width=400)
image = cv2.resize(image, (300, 300))
image = image.astype("float32") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
print("[INFO] loading network...")
model = load_model(args["model"])
lb = pickle.load(open(args["label"], 'rb'))
print("[INFO] classifying image...")
proba = model.predict(image)[0]
idxs = np.argsort(proba)[::-1][:1]
for (i, j) in enumerate(idxs):
label = "{}: {:.2f}%".format(lb.classes_[j], proba[j] * 100)
cv2.putText(output, label, (10, (i * 30) + 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
for (label, p) in zip(lb.classes_, proba):
print("{}: {:.2f}%".format(label, p * 100))
m = ("{}: {:.2f}%".format(lb.classes_[j], proba[j] * 100))
m1 = ("{}: {:.2f}%".format(lb.classes_[j-1], proba[j-1] * 100))
m2 = ("{}: {:.2f}%".format(lb.classes_[j-2], proba[j-2] * 100))
m3 = ("{}: {:.2f}%".format(lb.classes_[j-3], proba[j-3] * 100))
gps = gpsphoto.getGPSData(args["input"])
print(gps)
args["ketqua"] = open('Ket_qua.txt', 'w')
args["ketqua"].write(str(m) + "\n" + str(m1) + "\n" + str(m2) + "\n" +
str(m3) + "\n" + str(gps))
args["ketqua"].close()
db.go:
module github.com/Aragami1408/go-gorm
go 1.12
require (
github.com/gin-gonic/gin v1.4.0
github.com/jinzhu/gorm v1.9.9
github.com/lib/pq v1.1.1
github.com/satori/go.uuid v1.2.0
)
tasks.go:
package db
//code below...
但是当我跑步仍然出现此错误时:
package task
import (
"../db"
)
我在Google上搜索了很多东西,没有任何帮助
答案 0 :(得分:2)
来自“ Do modules work with relative imports like import "./subdir"?”
在模块中,最后有一个子目录的名称。
如果父目录说“
module m
”,那么子目录将导入为“m/subdir
”,而不再是“./subdir
”。
在您的情况下:
import "github.com/Aragami1408/go-gorm/db"
# or maybe
import "go-gorm/db"
答案 1 :(得分:1)
迁移为“软件包管理器”时,可以使用以下命令创建文件go mod
:
go mod init myhost/myrepo/mymodule
然后将创建文件go.mod
:
module myhost/myrepo/mymodule
go 1.15
现在,您可以利用此文件列出对其他模块的依赖关系:
# i.e: your module mymodule depennds on github.com/gorilla/mux
got get github.com/gorilla/mux
您运行它吗?然后再次检查go.mod
module myhost/myrepo/mymodule
go 1.15
require (
github.com/gorilla/mux v1.7.4
)
但是,您忘记了需要维护所有导入具有相对路径的目录的go文件。
示例:
如果您有main.go
package main
import (
"fmt"
"./router" // !! RELATIVE PATH
)
您还必须通过将相对路径替换为[module-name]/relative-path
来进行迁移。在这种情况下,它必须变为:
package main
import (
"fmt"
"myhost/myrepo/mymodule/router" // !! ? No more RELATIVE PATH
)