简体   繁体   中英

I can not import NLTK in Python in Windows7

System: Windows 7 64bit

Software: python-2.7.10.msi, nltk-3.0.4.win32.exe

Problem Description:

I use my desktop today to install Python 2.7.10 ( 32bit version ), and then install nltk 3.0.4. The path of Python is correctly set:

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Python27\\Lib\\idlelib', 'C:\\Python27\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
>>> 

Then I try import nltk in the Shell, then I get this:

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import nltk

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import nltk
  File "C:\Python27\lib\site-packages\nltk\__init__.py", line 128, in <module>
    from nltk.align import *
  File "C:\Python27\lib\site-packages\nltk\align\__init__.py", line 18, in <module>
    from nltk.align.bleu_score import bleu
  File "C:\Python27\lib\site-packages\nltk\align\bleu_score.py", line 15, in <module>
    from nltk.tokenize import word_tokenize
  File "C:\Python27\lib\site-packages\nltk\tokenize\__init__.py", line 62, in <module>
    from nltk.data              import load
  File "C:\Python27\lib\site-packages\nltk\data.py", line 76, in <module>
    if 'APPENGINE_RUNTIME' not in os.environ and os.path.expanduser('~/') != '~/':
  File "C:\Python27\lib\ntpath.py", line 311, in expanduser
    return userhome + path[i:]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 9: ordinal not in range(128)
>>> 

I tried several methods: 1) use different version, such as Python 2.6 and nltk 3.0.0, but not solved. 2) use patch from the official site, not working.

HOWEVER , when I use my laptop (Win7 64bit), with the same version of Python and nltk, everything is OK! I don't know the reason. So could anyone tell me why it happens?

I solved this by changing my operating system locale for non-Unicode programs:

  1. Go to Control Panel
  2. Click “Clock, Language and Region”
  3. Click “Regional and Language Options”
  4. Go under “Administrative” tab
  5. current system locale (for non-Unicode) is displayed, to change it click “Change system locale”
  6. Then just choose English! After a reboot, done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM