do export statement in Python script

S

Simon Strobl

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?

Simon
 
D

Diez B. Roggisch

Simon said:
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
 
J

James Mills

#!/usr/bin/
python

import os

os.system("export NLTK_DATA=/opt/nltk/data/")

Try:

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

if that doesn't work, consider wrapping up NLTK
in a bash script that contains the shell statement:
export var=...

cheers
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top