从Python 2移动到3:必须重新安装NLTK目录吗?
当从Python2移动到Python3时,我知道我必须重新安装Python3的NLTK,但是是否也需要重新安装massive目录 我使用的是MacOS10.12.5,我一直在Sublime3中使用Python2.7。我现在正转向Python3,有时在Sublimit3中工作,有时在Jupyter笔记本中工作。问题是我为2.7安装了NLTK,但现在我要迁移到3,我不想为Python3重新安装庞大的NLTK目录。所以我想知道是否有可能并且很容易安装NLTK for 3,但仍然使用整个解析器/语料库/等等。我已经有了从Python 2移动到3:必须重新安装NLTK目录吗?,python,nltk,jupyter-notebook,Python,Nltk,Jupyter Notebook,当从Python2移动到Python3时,我知道我必须重新安装Python3的NLTK,但是是否也需要重新安装massive目录 我使用的是MacOS10.12.5,我一直在Sublime3中使用Python2.7。我现在正转向Python3,有时在Sublimit3中工作,有时在Jupyter笔记本中工作。问题是我为2.7安装了NLTK,但现在我要迁移到3,我不想为Python3重新安装庞大的NLTK目录。所以我想知道是否有可能并且很容易安装NLTK for 3,但仍然使用整个解析器/语料库/
感谢您的帮助,如果可能的话,请指导教程 NLTK数据目录保持不变,因此无需重新安装数据 但是Python2和Python3的代码在python的dist包中的驻留方式不同 因此,您只需使用
pip
和pip3
安装nltk
:
pip install -U nltk
pip3 install -U nltk
但您只需安装nltk_data
目录一次,例如:
# Let's delete the existing nltk_data directory and start afresh:
alvas@ubi:~$ ls nltk_data/
chunkers grammars misc sentiment taggers
corpora help models stemmers tokenizers
alvas@ubi:~$ rm nltk_data/
# Install the NLTK code for pip3 (Python3) and pip (Python2)
alvas@ubi:~$ pip3 install -U nltk
Requirement already up-to-date: nltk in /usr/local/lib/python3.5/dist-packages
Requirement already up-to-date: six in ./.local/lib/python3.5/site-packages (from nltk)
alvas@ubi:~$ pip2 install -U nltk
Requirement already up-to-date: nltk in /usr/local/lib/python2.7/dist-packages
Requirement already up-to-date: six in /usr/local/lib/python2.7/dist-packages (from nltk)
# Now, download the NLTK directory in Python2
alvas@ubi:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> nltk.download('popular')
[nltk_data] Downloading collection u'popular'
[nltk_data] |
...
[nltk_data] | Downloading package averaged_perceptron_tagger to
[nltk_data] | /home/alvas/nltk_data...
[nltk_data] | Unzipping taggers/averaged_perceptron_tagger.zip.
[nltk_data] |
[nltk_data] Done downloading collection popular
True
# Now in Python3, when we try to re-download the nltk_data directory
# We see that it doesn't re-download it =)
>>> import nltk
>>> nltk.download('popular')
[nltk_data] Downloading collection 'popular'
[nltk_data] |
[nltk_data] | Downloading package cmudict to
[nltk_data] | /home/alvas/nltk_data...
[nltk_data] | Package cmudict is already up-to-date!
...
[nltk_data] | /home/alvas/nltk_data...
[nltk_data] | Package averaged_perceptron_tagger is already up-
[nltk_data] | to-date!
[nltk_data] |
[nltk_data] Done downloading collection popular
True
我可以下载Python2的Python包并使其在Python3中工作吗? 不幸的是,没有。在Python2中安装包/库独立于Python3的环境,反之亦然。这不仅适用于
nltk
,也适用于其他库
不要把Python3看作是Python2的更高版本,把它们看作是两种不同的语言;P