1

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.

2
  • do you have a file named random.py ? Change its name to something else and then run. It is overwriting the random method used by nltk. Commented Dec 11, 2015 at 6:39
  • Yes thank you! That was the problem. Commented Dec 11, 2015 at 6:47

1 Answer 1

2

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.

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.