问题描述:
- 准备通过pip3 install pyenchant安装PyEnchant模块,实现识别输入字符串是否为有效英文单词的功能。结果安装后执行如下测试脚本报错importError : The ‘enchant’ C library was not found。
import enchant
d = enchant.Dict("en_US")
d.check("Hello")
>>>
True
d.check("Helo")
False
- 为解决该问题,查贴发现推荐的解决方法【Enchant安装及使用】并不适合我的Mac系统,具体如下:
# CentOS 7安装Enchant: yum install enchant # Ubuntu 16.04安装Enchant: apt-get install libenchant1c2a
考虑到Mac没有yum源,故而想到用brew install name 来替代,结果在本机直接执行了如下命令:
brew install enchant
之后,就杯具了~~~,Python版本被自动不说,且安装的所有第三方包,Jupyter Notebook等都不翼而飞了!!!!说白了,就是被更新格式化掉了!!!!这是由于在Mac下使用brew install name 安装软件时,默认每次都会自动更新homebrew,显示Updating Homebrew…,会自动清理过期程序。
解决方案:
唉,过往不可追,只能亡羊补牢,避免类似错误了~
- 必须关闭在Mac下使用brew命令时默认的homebrew自动更新功能:
具体方法如下:
(1)临时取消方法:
# 在终端输入 export HOMEBREW_NO_AUTO_UPDATE=true
(2)永久性关闭brew每次执行命令时的自动更新(推荐!!!)
vim ~/.bash_profile # 新增一行 export HOMEBREW_NO_AUTO_UPDATE=true
参考链接:Mac 禁止homebrew自动更新
- 重新配置Python环境:
(1)关于这篇博文的初衷【通过pip3 install pyenchant安装PyEnchant模块】,在使用【brew install enchant】后,再次执行pip3 install pyenchant命令,之后运行测试上述脚本,完美通过~
反思:最初【通过pip3 install pyenchant安装PyEnchant模块】后运行测试脚本不成功,应该是由于缺少了PyEnchant模块的依赖环境,根据错误提示,应该是基于C语言开发的。执行pip3 uninstall pyenchant卸载后,先执行【brew install enchant】,之后重新安装pip3 install pyenchant,就可以正常运行了。至此,关于Mac下【通过pip3 install pyenchant安装PyEnchant模块】的问题得到圆满解决。
(2)只能重新配置Python环境了,补全被格掉的第三方模块。
具体参考本人的另一篇博文【Mac系统中Python3.7环境和Jupyter notebook的安装、部署——最详细搭建教程,一文解决全部问题~】
关于安装过程中超时解决方法参考本人的博文【Mac系统在使用pip安装总报超时错误的解决方法】
pip批量安装第三方模块的解决方案:
pip install xxx -i http://pypi.douban.com/simple/ pip install -r requirements.txt -i http://pypi.douban.com/simple/
参考链接:pip install 时使用豆瓣源
上面提到的 “requirements.txt”存储了需要安装的第三方包及对应的版本号,查贴说requirements 文件最简单的格式如下:
pandas future macholib pefile pyinstaller
经测试直接运行时报错不存在文件中的xxx模块,解决方法:
创建一个txt文件,例如:requirements.txt,里面写入几个模块,例如: Django==2.1.5 psycopg2==2.7.7 django-excel==0.0.10 pyexcel-xls==0.5.8 然后需要安装时,进入 requirements.txt 所在路径,执行: pip install -r requirements.txt 即可一次性安装txt文件中列出的所有模块
这个应该是正确的方法,反思了一下应该是缺少版本号,格式不对~
参考链接:pip一次性安装多个模块
基于配置好的环境生成requirements.txt,便于以后快速配置、部署Python环境
1. 生成requirements.txt文件:
pip3 freeze > requirements.txt # 在当前目录下生成requirements.txt文件
2. 安装requirements.txt依赖:
pip3 install -r requirements.txt
3. 引入豆瓣源加速安装requirements.txt依赖:
pip3 install -r requirements.txt -i http://pypi.douban.com/simple/
参考链接:在Linux系统使用pip install -r requirements.txt 以及出现的问题



