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
11
  • How did you install nltk? Commented Apr 18, 2014 at 14:03
  • @mskimm, I installed it following the source installation provided by the official site link. For the data, the connection was slow using nltk.download(), I downloaded it else where. Commented Apr 18, 2014 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. Commented Apr 18, 2014 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. Commented Apr 18, 2014 at 14:18
  • @mskimm, I tried installing it again, it did not report any error during installation. But the problem still remains. Commented Apr 18, 2014 at 15:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.