PermissionError when downloading NLTK data

2019-08-21 07:38发布

问题:

I use Anaconda's Python 3.6.3 distribution and it comes with NLTK installed, but not with NLTK DATA, which I need for a project, the problem is, when I try to install with

nltk.download()

I get

PermissionError: [Errno 13] Permission denied: '/usr/share/nltk_data'

So, I did some research, and I see people suggesting to run Python as

sudo python

but if I do that, it will launch the base Linux's Python, not Anaconda's.

tl;dr

I need some way to do something like

sudo conda python

If you have other suggestions that might work, I'll take it too.

Thanks!

回答1:

Find out which directory you can write files to. E.g. if it's /home/alvas/testdir

Then

>>> pip install -U nltk
>>> mkdir -p /home/alvas/testdir 
>>> python -m nltk.download popular -d /home/alvas/testdir 

If you want to know how to configure the custom path for nltk_data, at the start of your Python code:

import nltk
nltk.data.path.append('/home/alvas/testdir')


回答2:

Would something like this work ? Supposing your Anaconda env is called myenv.

source activate myenv
sudo python -c "import nltk; nltk.download()"

That's assuming having activated your env before would prevent using the base Linux's Python as you pointed out.