Skip to main content
 首页 » 编程设计

python之为什么pip install bert后不能导入bert中的函数

2025年05月04日25zhwl

我是 bert 的初学者,我正在尝试使用 GitHub 上提供的 bert 文件:https://github.com/google-research/bert

但是在终端使用pip install bert安装bert后,我无法从bert导入文件(如run_classifier、optimization等)。我尝试在 jupiter notebook 中运行以下代码:

import bert 
from bert import run_classifier 

错误是:

ImportError: cannot import name 'run_classifier' 

然后我在\anaconda3\lib\python3.6\site-packages中找到名为'bert'的文件,里面没有'run_classifier','optimization'等python文件.所以我从 GitHub 下载了这些文件,并自己将它们放入文件“bert”中。完成此操作后,我可以导入 run_classifier。

然而,另一个问题出现了。尽管我可以导入它们,但我无法使用文件中的函数。 比如tokenization.py中有一个函数convert_to_unicode:

Help on module bert.tokenization in bert: 
 
NAME 
 
    bert.tokenization - Tokenization classes.     
FUNCTIONS 
 
    convert_to_unicode(text) 
    Converts `text` to Unicode (if it's not already), assuming utf-8 input. 

然后我试了一下:

import tokenization from bert 
convert_to_unicode('input.txt') 

错误是:

NameError: name 'convert_to_unicode' is not defined 

然后我尝试了:

from tokenization import convert_to_unicode 

错误是:

ModuleNotFoundError: No module named 'tokenization' 

我真的很困惑。

请您参考如下方法:

您要查找的软件包是 bert-tensorflow,而不是 bert。

bert-tensorflow是 Google 的 BERT 实现的 Python 包。
bert是一个序列化库。