简体   繁体   中英

Not able to Import in NLTK - Python

When I run this command in a file or in a shell

import nltk

I get the following error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/nltk/__init__.py", line 83, in <module>
    from collocations import *
  File "/usr/lib/python2.7/dist-packages/nltk/collocations.py", line 37, in <module>
    from nltk.probability import FreqDist
  File "/usr/lib/python2.7/dist-packages/nltk/probability.py", line 45, in <module>
    import random
  File "random.py", line 2, in <module>
    T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''

Not able to comprehend what is going wrong.

You have a local random module, which masks the random module from standard library.

If you try to import nltk from a different working directory, it should succeed. But in general it's not a good idea name your modules after standard modules, so please rename your random.py file to something else.

You for completeness, let me say that the error was obvious from the last lines of you traceback:

  File "random.py", line 2, in <module>
    T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''

From the path, random.py , you can tell that the error is in a local file named random.py . And from the exception, you know that something passed an empty string, '' , from raw_input to int function, which failed to be converted to int .

Rule of thumb number 2 : Always guard you executable code, in a module, in a if __name__ == '__main__': block.

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