我在项目中使用带有wordnet的NLTK。我在我的电脑上手动安装,在终端中使用pip:pip3 install nltk --user
,然后在python shell中使用nltk.download()
下载wordnet。
我想用setup.py文件来创建它们,但我不知道安装wordnet的好方法。
目前,我在调用setup
之后有了这段代码(“nltk”在install_requires
的setup
列表中):
import sys
if 'install' in sys.argv:
import nltk
nltk.download("wordnet")
有一个更好的方法吗?
我设法通过用我自己的cmdclass
类覆盖Install
来在setup.py中安装NLTK数据:
from setuptools import setup, find_packages
from setuptools.command.install import install as _install
class Install(_install):
def run(self):
_install.do_egg_install(self)
import nltk
nltk.download("popular")
setup(...
cmdclass={'install': Install},
...
install_requires=[
'nltk',
],
setup_requires=['nltk']
...
)
在调用do_egg_install()
之前,在run()
方法中使用方法import nltk
以确保安装nltk非常重要(另请参见python setuptools install_requires is ignored when overriding cmdclass)。另外不要忘记将nltk
添加到setup_requires
。
您还可以使用shell脚本自动安装,例如,运行(在pip安装nltk之后):
python -m nltk.downloader -d /usr/share/nltk_data wordnet