笔者近日在cmd命令行执行程序时总是报ModuleNotFoundError的错误。
笔者的python项目文件夹架构如下
笔者在run_classifier.py文件中导入了modeling.py、optimization.py和tokenization.py中的类,导入代码段如下:
from BERTlearning.BertTextClassification.modeling import BertModel, get_assignment_map_from_checkpoint, BertConfig
from BERTlearning.BertTextClassification.optimization import create_optimizer
from BERTlearning.BertTextClassification.tokenization import convert_to_unicode, printable_text, FullTokenizer
在cmd命令行输入命令时报错如下:
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
出现错误的原因是因为在cmd中执行程序,所在路径是python的搜索路径,如果涉及到import时就会出现此类错误。
文章转载: