pip安装sentence-transformers时的一些报错记录以及Python汉字转拼音cleverdeng/pinyin.py程序的调整处理
一、pip安装sentence-transformers时的一些报错记录
之前记的一些pip安装sentence-transformers时的一些报错记录,也不知道是什么时候记下来的,不想删除放这里存着吧。
pip3 install -U sentence-transformers
pip3 install -U transformers
1. PIP进行模块下载太慢甚至超时报错
PIP进行模块下载太慢甚至超时,urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out。解决方法是可以直接指定下载源,解决问题。也可以配置vim ~/pip/pip.conf文件中的源地址 ,一劳永逸。
pip install -i https://pypi.douban.com/simple -U sentence-transformers
2. ModuleNotFoundError: No module named 'setuptools_rust'
解决办法:升级pip,然后再安装oss2
pip3 install --upgrade pip
pip3 install oss2
3. ImportError: No module named ordered_dict
解决办法:安装urllib3
pip install urllib3==1.22
4. ImportError: no module named zipp
解决办法:安装zipp
pip install zipp==1.2.0
5. linux安装库时报错command 'x86_64-linux-gnu-gcc' failed
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
apt-get install -y build-essential python3-dev libssl-dev libffi-dev libxml2 libxml2-dev libxslt1-dev zlib1g-dev
6 .Warning: pip is being invoked by an old script wrapper
解决办法:
#For Python2 Versions:
python -m pip install --upgrade --force-reinstall pip
#For Python3 Versions:
python3 -m pip install --upgrade --force-reinstall pip
二、Python汉字转拼音cleverdeng/pinyin.py程序的调整处理
在使用python将汉字转为拼音的时候,我使用了https://github.com/cleverdeng/pinyin.py 中的这个程序文件,虽然原程序是基于python2开发的,但程序很简洁,代码量也不多,完全可以使用。现在因为是python3的环境,会遇到一些问题以及原程序不够好的地方,在这里总结列出来一下:
1. 在加载dict_file文件时使用的是
def __init__(self, dict_file='word.data'):self.dict_file = dict_file
根本没有考虑程序文件是基于框架并不是在当前程序目录中运行,因此会导致找不到dict_file文件而报错。更好的方法应该是基于当前路径去加载文件。
def __init__(self, dict_file='word.data'):abs_path = os.path.dirname(__file__)self.dict_file = abs_path + '/' + dict_file
2. 在读取dict_file文件时报错
with file(self.dict_file) as f_obj:
NameError: name 'file' is not defined, 因python版本升级,函数使用有所变化,需要把file函数改成open函数从而解决问题。
3. 在字符进行解码的时候unicode要改成str
if not isinstance(string, unicode):
string = string.decode("utf-8")
会报错:NameError: name 'unicode' is not defined。 Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode即可解决问题。