I figured out the reason, I named the original script file nltk.py, so python tried to import word_tokenize from the script file orz. Sorry for this silly mistake.
I am trying to use nltk in Python on Windows. I have installed nltk and nltk data.
However, when I try to run
python -u 'filename.py'
in commandline, it gives an error as follows.
Traceback (most recent call last):
File "filename.py", line 1, in (module)
from nltk import word_tokenize
File "filenmae.py", line 1, in (module)
from nltk import word_tokenize
ImportError: cannot import name word_tokenize
On the other hand, when I run
python < 'filename.py'
the correct result is given.
The code in filename.py is a simple test code, as given below.
from nltk import word_tokenize
tokens = word_tokenize('hello i am your friend')
print(tokens)
Could you help me with this? Thanks in advance.
I tried re-installing nltk
following the source installation instruction here. But it did not work.
Additionally, I had python 3.3 installed in the past, but I found that nltk
cannot recognize python 3.3 during installation, so I installed python 2.7. And now there are actually two versions of python on my computer.
The related User Path is C:\Python27
.
In System Path, the related fields are:
Path C:\Python27\
PYTHONIOENCODING utf-8
PYTHONPATH C:\Python27
The python version when I run it in command line is,
Python 2.7.3 (Date) [MSC v.1500 32 bit (Intel)] on win32
Also, I tried running the following code on both command line and using a script file given here:
import nltk
import sys
print(nltk)
print(sys.executable)
In the command line the results are
>>> print(nltk)
<module 'nltk' from 'C:\Python27\lib\site-packages\nltk\__init__.pyc'>
>>> print(sys.executable)
C:\Python27\python.exe
Using a script the results are
<module 'nltk' from 'C:\Users\username\Documents\nltk.py'>
C:\Python27\python.exe
<module 'nltk' from 'C:\Users\username\Documents\nltk.py'>
C:\Python27\python.exe