1

I have two script
1. demo.ksh
2. demo.py

in demo.ksh i am exporting variable as

#!/bin/ksh
TEST="Hello"
export TEST

in demo.py I am executing demo.ksh and trying to read exported value as ..

import os
import subprocess
cmd='. demo.ksh' #I even tried 'demo.py' (no .)
subprocess(cmd,shell=True,stdout=subprocess.PIPE)
print(os.getenv('TEST'))
print(os.environ['TEST'])

I am expecting

Hello
Hello

But getting

None
KeyError: 'TEST'

Although it is a simple exercise. I could not find correct solution for this.Please help me what is wrong with my code.

1
  • Why can't you use script stdout? With environment variables it would be difficult because it slightly hard to get another process environment variables. You can read about it more there: stackoverflow.com/questions/5905574/… Commented May 29, 2017 at 12:53

1 Answer 1

1

Exporting a variable makes it available to sub-processes of the spawned shell process, but not to the parent process (i.e. your Python program).

To get the expected output try a shell script like this:

#!/bin/ksh

TEST="Hello"
export TEST
python demo.py

You can instead communicate with the subprocess via STDOUT. For this, subprocess.check_output can be useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.