Nikhil Prabhu Nikhil Prabhu - 1 year ago 203
Python Question

Python, nltk: Cannot import mkdtemp

I got this error while trying to compile that contains these two commands -

import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

This is the error I got

Traceback (most recent call last):
File "", line 3, in <module>
import nltk
File "C:\Anaconda2\lib\site-packages\nltk\", line 89, in <module>
from nltk.internals import config_java
File "C:\Anaconda2\lib\site-packages\nltk\", line 29, in <module>
from nltk import compat
File "C:\Anaconda2\lib\site-packages\nltk\", line 206, in <module>
from tempfile import mkdtemp
ImportError: cannot import name mkdtemp

The strange thing is, that these same two commands when executed on the command line work perfectly fine.

What could be the reason for this to happen? And more importantly, how do I fix this?


This is what i got when i executed print(sys.path)

['', 'C:\\Anaconda2\\', 'C:\\Anaconda2\\DLLs', 'C:\\Anaconda2\\lib', 'C:\\Anaconda2\\lib\\plat-win', 'C:\\Anaconda2\\lib\\lib-tk', 'C:\\Anaconda2', 'C:\\Anaconda2\\lib\\site-packages', 'C:\\Anaconda2\\lib\\site-packages\\Sphinx-1.3.5-py2.7.egg', 'C:\\Anaconda2\\lib\\site-packages\\win32', 'C:\\Anaconda2\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda2\\lib\\site-packages\\Pythonwin', 'C:\\Anaconda2\\lib\\site-packages\\setuptools-20.3-py2.7.egg']


I realised that I had a file called in my working directory. Upon deletion everything works fine

Answer Source

Make sure you don't have somewhere in python path; which prevents import of standard library tempfile module.

Also make sure deleting tempfile.pyc if there it is.