MacOS High Sierra系统下安装NLTK失败,请求技术支持
Hey there! Let’s troubleshoot your NLTK installation issue on macOS High Sierra step by step—this is a common snag with that OS version, so I’ve got a few tried-and-true fixes to walk you through.
macOS High Sierra comes pre-installed with Python 2.7, but NLTK works much smoother with Python 3. Let’s make sure you’re targeting the right version:
- Open Terminal and run
python --versionandpython3 --versionto see which Python versions you have. - If you don’t have Python 3 installed, grab it via Homebrew (if you don’t have Homebrew, run this first:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)", thenbrew install python3).
Most installation failures here are due to system permission issues—never use sudo for pip installs (it can mess up your system files). Instead, use the --user flag to install to your user directory:
- For Python 3:
pip3 install --user nltk - For Python 2 (not recommended, but if you need it):
pip install --user nltk
If you get a "pip is out of date" error, upgrade pip first:
- Python 3:
pip3 install --user --upgrade pip - Python 2:
pip install --user --upgrade pip
High Sierra’s built-in Python has outdated SSL certificates, which can block pip from connecting to PyPI. Here’s how to fix it:
- If you installed Python 3 manually (not the system version), run this Terminal command (replace
3.xwith your actual Python version, like3.7):/Applications/Python\ 3.x/Install\ Certificates.command - For the system’s Python 2.7, manually update the certificates:
curl https://curl.se/ca/cacert.pem -o /usr/local/etc/openssl/cert.pem export SSL_CERT_FILE=/usr/local/etc/openssl/cert.pem
Then re-run the NLTK install command.
Once the install finishes, let’s confirm it works:
- Open a Python shell (run
python3orpythonin Terminal), then type:import nltk nltk.download('punkt')
If this downloads the punkt corpus without errors, you’re good to go. If the download gets stuck, you can manually download corpora:
- Create a folder named
nltk_datain your home directory (~/nltk_data), then download the corpus files from the NLTK website and unzip them into this folder.
If you have Anaconda installed, installing NLTK via conda avoids a lot of system-level headaches:
- Run
conda install nltkin Terminal, then usenltk.download()to grab the corpora you need.
内容的提问来源于stack exchange,提问作者user8833630

