Reputation: 13
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?
Upvotes: 1
Views: 1401
Reputation: 151
Problem
NLTK package tries to find the os.environ["APPDATA"]
variable to load it's contents.
XAMPP or any other CGI server doesn't load all of the os variables which are generally available on windows.
Hence we must explicitly provide The APPDATA SET Variable. This can be done via 2 methods.
Solution
import os
os.environ['APPDATA'] = r"C:\Users\YOUR_USER\AppData\Roaming"
Set the environment variable in Xampp's http.conf file by adding this Line to it.
SetEnv APPDATA "${APPDATA}"
Upvotes: 3
Reputation: 1190
The Problem is raised probably because you don't have a default directory created for your ntlk downloads. If you are on a Windows Platform, All you need to do is to create a directory named "nltk_data" in any of your root directory and grant write permissions to that directory. The Natural Language Tool Kit initially searches for the destination named "nltk_data" in all of the root directories.
For Instance: Create a folder in your C:\ drive named "nltk_data"
After Making sure everything is done fine, execute your script to get rid of this error.
Hope this helps.
Regards.
Upvotes: -1
Reputation: 50220
The problem is that on being imported, the nltk
tries to initialize a Downloader
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 NLTK_DATA
in the environment, initialized to a folder that (a) exists, and (b) your server has write access to.
In case that's not possible for some reason, let's take a look at the code that throws the error. The function default_download_dir()
in nltk\downloader.py
first looks for writeable locations in nltk.data.path
(initialized from NLTK_DATA
). If it fails to find any, it makes one last try: it tries for a folder nltk_data
in your HOME directory (except on Windows). Evidently, your environment settings prevent Python from resolving ~/
to your HOME directory, leading to the error.
# 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")
So figure out what you can do to your environment to make this function happy.
Upvotes: 0
Reputation: 5818
The environment in which your CGI script is executed is not the same as when you run it in from a terminal or similar. Specifically, environment variables like $PYTHONPATH
might not be set to what you need.
An ugly but safe work-around is adding the needed directories inside the script, before any third-party import statements:
import sys
sys.path.append('path/to/package-parent') # change this to what you actually need
import nltk
To find the location of NLTK or whatever causes trouble, import it in an interactive session. Then, typing the module/package name will print the location:
>>> import nltk
>>> nltk
<module 'nltk' from '/usr/local/lib/python3.4/dist-packages/nltk/__init__.py'>
So, you would append '/usr/local/lib/python3.4/dist-packages' to sys.path
in this case.
I'm not entirely sure if this also applies to the "default download directory", but you can give it a try.
Upvotes: 0