ValueError: Could not find a default download directory of nltk
<p>I have problem on import nltk.
I configured apache and run some sample python code, it worked well on the browser.
The URL is : /localhost/cgi-bin/test.py.
When I import the nltk in test.py its not running. The execution not continue after the "import nltk" line.And it gives me that error ValueError: Could not find a default download directory 
But when I run in the command prompt its working perfect.
how to remove this error?</p>

<p>The problem is that on being imported, the <code>nltk</code> tries to initialize a <code>Downloader</code> object (even though you have not tried to download any resources), and fails to identify a usable download location. The easiest way to make it happy is to define <code>NLTK_DATA</code> in the environment, initialized to a folder that (a) exists, and (b) your server has write access to. </p>

<p>In case that's not possible for some reason, let's take a look at the code that throws the error. The function <code>default_download_dir()</code> in <code>nltk\downloader.py</code> first looks for writeable locations in <code>nltk.data.path</code> (initialized from <code>NLTK_DATA</code>). If it fails to find any, it makes one last try: it tries for a folder <code>nltk_data</code> in your HOME directory (except on Windows). Evidently, your environment settings prevent Python from resolving <code>~/</code> to your HOME directory, leading to the error.</p>

<pre><code># On Windows, use %APPDATA%
if sys.platform == 'win32' and 'APPDATA' in os.environ:
    homedir = os.environ['APPDATA']

# Otherwise, install in the user's home directory.
else:
    homedir = os.path.expanduser('~/')
    if homedir == '~/':
        raise ValueError("Could not find a default download directory")
</code></pre>

<p>So figure out what you can do to your environment to make this function happy.</p>

python nltk