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
    You cannot. A child process cannot change the environment of its parent. – chepner Jun 23 '17 at 20:43
  • Related (would be duplicate, but for the difference between csh and bash): https://stackoverflow.com/questions/41708458/assign-environment-variables-from-bash-script-to-current-session-from-python -- note the use of NUL delimited streams for safety in light of arbitrary content. – Charles Duffy Jun 23 '17 at 20:51
  • I think what you're trying to do is not possible - https://stackoverflow.com/questions/263005/is-it-possible-to-change-the-environment-of-a-parent-process-in-python. 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`) – OldGeeksGuide Jun 23 '17 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 – Aashish sharma Sharma Jun 23 '17 at 23:53

1 Answers1

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"')
MrE
  • 19,584
  • 12
  • 87
  • 105
  • 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") – Aashish sharma Sharma Jun 23 '17 at 22:42
  • 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 – Aashish sharma Sharma Jun 23 '17 at 23:40
  • 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 – MrE Jun 23 '17 at 23:42
  • 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? – Aashish sharma Sharma Jun 23 '17 at 23:48