do export statement in Python script

Diez B. Roggisch deets at nospam.web.de
Wed Oct 22 13:20:56 CEST 2008


Simon Strobl wrote:

> Hello,
> 
> a program of mine, which is supposed to be used in a project, uses
> nltk. I want the other users to be able to use my program without
> having to fiddle around with their environment. Therefore, I tried
> this code:
> 
> #!/usr/bin/
> python
> 
> import os
> 
> os.system("export NLTK_DATA=/opt/nltk/data/")
> 
> import nltk
> 
> This doesn't work, although running "export NLTK_DATA=/opt/nltk/data/"
> on the command line before running the program works. Why?

because export is a shell-command that affects only the current shell, not
the parent process environment. And os.system spawns a child-process.

Instead, use

os.environ["NLTK_DATA"] = "/opt/nltk/data"

Diez



More information about the Python-list mailing list