Pergunta

I'm using Python with the NLTK toolkit in Apache via CGI. The toolkit need to know the APPDATA directory, but when executed in the server, the os.environ not lists theAPPDATA.

When I execute a simple print os.envrion in console, APPDATA is present, but not when executed via CGI in the web server.

What is going on? How can I solve this? I'm new to Python and I'm just learning it yet.

Foi útil?

Solução

Is is quite possible. The CGI environment is different from the environment in the console.

You can set the variable from Apache (from apache.conf):

SetEnv APPDATA 1.2.3.4.5

Outras dicas

%APPDATA% is a special variable that expands to the "Application Data" directory of the user who expands the variable (i.e., who runs a script). Apache is not running as you, so it has no business knowing about your APPDATA directory.

You should either hard-code the relevant path into your script, or replace it with a path relative to the location of the script, e.g., r'..\data\nltk_data'. If you really need to, you can recover the absolute location of your script by looking at __file__.

The module check this before running:

if sys.platform == 'win32' and 'APPDATA' in os.environ

But APPDATA doesn't exist in Apache because it's running as another user different from yours.

so you have to assign your APPDATA manually

import os
os.environ.__setitem__('APPDATA','C:\Python27\Lib\site-packages')

For my case, i use python 2.7, and I store all the packages in site-packages, so they are accessible for all users. so i don't really need to give my user appdata folder.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top