3

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
Community
  • 1
  • 1
xychang
  • 51
  • 1
  • 7
  • How did you install `nltk`? – emesday Apr 18 '14 at 14:03
  • @mskimm, I installed it following the source installation provided by the official site [link](http://www.nltk.org/install.html). For the data, the connection was slow using `nltk.download()`, I downloaded it else where. – xychang Apr 18 '14 at 14:11
  • If installed properly. It works. I got `['hello', 'i', 'am', 'your', 'friend']` from your code. please try install again following the **instruction** on official site. – emesday Apr 18 '14 at 14:13
  • @mskimm, if I am going to install it again, should I uninstall the current version first? I am really new to python, and do not know if there is something like uninstall required. – xychang Apr 18 '14 at 14:18
  • @mskimm, I tried installing it again, it did not report any error during installation. But the problem still remains. – xychang Apr 18 '14 at 15:02
  • The question also still remains. How exactly did you install the first time around? Presumably you did something which needs to be undone, but we can't guess much more than that. – tripleee Apr 18 '14 at 15:15
  • @tripleee, 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. Could this have any thing to do with my problem here? – xychang Apr 18 '14 at 15:36
  • Yes, it could. Above all, we need more details about what you have done, and how. Please edit your question to include all pertinent details. – tripleee Apr 18 '14 at 15:40
  • @tripleee, is there any more information that I should provide that might help? – xychang Apr 18 '14 at 15:43
  • What are the paths? Which Python version do you get when you run `python`? Where and how did you install NLTK? – tripleee Apr 18 '14 at 15:45
  • @tripleee, I have add the related information into the question. Would that help? – xychang Apr 18 '14 at 15:53

1 Answers1

2

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.

xychang
  • 51
  • 1
  • 7