0

I need a help in making variables as ENV in python, so that I can see that variable by using 'export' command in Linux. So I tested a below short script and I can see variable using export command. But the problem is that, below two command didn't work.

var1 = os.environ['LINE']
print(var1)

Can you guide me how can I get this solved ?

import os
import json
import sys

Name = "a1"
def func():
        var = 'My name is ' + '' + Name
        os.putenv('LINE', var)
        os.system('bash')

func()

var1 = os.environ['LINE']
print(var1)

Output:

export | grep LINE
declare -x LINE="My name is a1"
  • Does this answer your question? [What are the differences between os.putenv(key, value) and os.environ\[key\]=value in python?](https://stackoverflow.com/questions/17333701/what-are-the-differences-between-os-putenvkey-value-and-os-environkey-value) – Gino Mempin Dec 03 '22 at 07:04
  • 2
    It is so weird that `os.environ` and `os.putenv` interact this way. You can't even inspect it with `os.getenv`, because that just delegates to `os.environ`. The whole setup is essentially broken and needs to be fixed. – user2357112 Dec 03 '22 at 07:06
  • @GinoMempin I checked that link. but it was different. Here in my case. I am able to make it system env variable using os.system('bash'). I noticed without using os.system('bash'), it can't be system env var. Once this os.system getting executed, then later part is not executed. – Anirban Das Dec 03 '22 at 07:12

1 Answers1

0

Try with

os.environ['LINE'] = var

instead of using putenv. Using putenv "bypasses" os.environ, that is, it doesn't update os.environ.

In fact, from the documentation for os.putenv:

Assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ. This also applies to getenv() and getenvb(), which respectively use os.environ and os.environb in their implementations."

9769953
  • 10,344
  • 3
  • 26
  • 37
  • tried this. Now it's printing the content as I commented os.system. but while I run export command, then didn't find "LINE" variable. – Anirban Das Dec 03 '22 at 07:22
  • Odd; works for me. This is on macOS, bash 3.2. And it's definitely not set outside of the program; only from the bash subshell run from inside the program. – 9769953 Dec 03 '22 at 07:36
  • can you pls provide the code which one you tried. It helps then @9769953 – Anirban Das Dec 03 '22 at 07:37
  • This is my output.. `[root@ansible-controller Scripts]# cat test.py import os import json import sys Name = "a1" def func(): var = 'My name is ' + '' + Name os.environ['LINE'] = var ###os.system('bash') func() var1 = os.environ['LINE'] print(var1) [root@ansible-controller Scripts]# [root@ansible-controller Scripts]# python3 test.py My name is a1 [root@ansible-controller Scripts]# export | grep LINE [root@ansible-controller Scripts]#` – Anirban Das Dec 03 '22 at 07:39
  • @AnirbanDas I've added the exact code to my answer. It's your exact code (minus two imports + my putenv replacement). – 9769953 Dec 03 '22 at 07:43
  • @AnirbanDas It looks like you first run `python3 test.py`, then `export | grep LINE` separately after the Python program has finished. Is that correct? – 9769953 Dec 03 '22 at 07:45
  • yes correct.... – Anirban Das Dec 03 '22 at 07:46
  • Then you misunderstand how setting environment variables from within a program work: they only apply to that running program, and its children (e.g., a bash subshell, as run by os.system). They don't apply to the *surrounding* (parent) shell; it simply can't. This essentially is the case for any program, not just Python. See for example https://unix.stackexchange.com/questions/372627/is-it-possible-to-pass-environment-variables-from-child-to-parent-in-user-space – 9769953 Dec 03 '22 at 07:47
  • okay got it.... – Anirban Das Dec 03 '22 at 07:52
  • but in linux, we can make sys env var using export, same thing we can't do ? For ex: export name="a1" then if you run source test.sh and run export command, you will be able to see variable "name". – Anirban Das Dec 03 '22 at 07:55
  • Shell scripts are a different beast; they are more part of the shell. The `export` command helps with that, but that is then all integrated into the shell. Python, C programs or anything else have, as far as a I know, no such export mechanism. This is basically a safeguard, so that only the shell can alter the shell. – 9769953 Dec 03 '22 at 07:57
  • understood. okay, then I need to restructure codes. Thanks !! :) – Anirban Das Dec 03 '22 at 07:58