0

Hello I tried to set environement variable using python. My python file looks like this.

#!/usr/bin/python2.7
import os
os.system("check")

Please see here file check is csh file which is something like this.

#!usr/bin/csh
setenv VARIABLE 1

but when i check from my shell environment variable is not set. I understand that when I call os.system it makes another subshell and sets variable there. I tried directly running csh file and same issue. I also tried.

os.system("setenv VARIABLE 1")

it throws error command not found. That was my earlier reason to put the command in diff file and calling it.

All I need to do is set the environment variable from my python script in current shell

4
  • 4
    You cannot. A child process cannot change the environment of its parent. Commented Jun 23, 2017 at 20:43
  • Related (would be duplicate, but for the difference between csh and bash): stackoverflow.com/questions/41708458/… -- note the use of NUL delimited streams for safety in light of arbitrary content. Commented Jun 23, 2017 at 20:51
  • I think what you're trying to do is not possible - stackoverflow.com/questions/263005/…. If you're instead trying to set an environment variable for a new shell, then it looks like setting os.environ['VARIABLE'] = 1 works. (at least with subprocess) Commented Jun 23, 2017 at 22:23
  • @ chepner, @ Charles Duffy @ OldGeeksGuide. I read it online that subshell cant modify the environemnt variable of parent. But I need to set this environment variable and run the another shell script. Its like I have to do setenv VARIABLE 1 then check_run. I want to make python script which can do both. Please see the check_run would be using VARIABLE 1 thats why I insist that both should happen in same shell Commented Jun 23, 2017 at 23:53

1 Answer 1

0

why do you need to set an environment variable from a python script? The python script might need to get environment variables from the shell, but if you need some variable in Python, just use a Python variable. If you need to run another program that takes an envirnoment variable, you can always pass it with the command like:

os.system('export VARIABLE=1 && echo "$VARIABLE"')
Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to automate some commands which run from unix shell. So in that I need to set some environment variable and then I need to execute some csh file. So I was hoping to do something like os.system("setenv VARIABLE 1") then os.system("Something_run")
Apologies if I am asking too basic a question. I am quite new to unix but I read that export is shell command which just makes any variable available to local variable. So my understanding is that if I define a new variable and want it to be visible in "child process" then I would use export. but here VARIABLE is already there. When I enter setenv on shell it shows all the variables. setenv |grep VARIABLE I can see its value being 0. I want to set it to 1 and then run another file say check_run
just ise the syntax I showed here: set tour variable when running the script. forget export, this would work even without export. the point is set the VARIABLE and run the command on the same os.system call
Thanks. I am reading all sort of stuff online and experimenting myself too. So just one last doubt. when I run os.system(VARIABLE=1 && check_run) its like python create another subshell and both the commands setenv VARIABLE 1 and check_run executes from the same shell? and running os.system(VARIABLE=1 && check_run) is same as runnning setenv VARIABLE 1 && check_run from shell?

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.