12

I have a file contains set of environment variables .

env_script.env:

export a=hjk
export b=jkjk
export c=kjjhh
export i=jkkl
..........

I want set these environment variables by reading from file . how can i do this in python

Tried sample code:

pipe = subprocess.Popen([".%s;env", "/home/user/env_script.env"], stdout=subprocess.PIPE, shell=True)
output = pipe.communicate()[0]
env = dict((line.split("=", 1) for line in output.splitlines()))
os.environ.update(env)

Please give some suggestion

asked May 12, 2017 at 9:16

3 Answers 3

12

There's a great python library python-dotenv that allows you to have your variables exported to your environment from a .env file, or any file you want, which you can keep out of source control (i.e. add to .gitignore):

# to install
pip install -U python-dotenv
# your .env file
export MY_VAR_A=super-secret-value
export MY_VAR_B=other-very-secret-value
...

And you just load it in python when your start like:

# settings.py
from dotenv import load_dotenv
load_dotenv()

Then, you can access any variable later in your code:

from os import environ
my_value_a = environ.get('MY_VALUE_A')
print(my_value_a) # 'super-secret-value'
answered Sep 19, 2019 at 15:33
Sign up to request clarification or add additional context in comments.

Comments

10

You don't need to use subprocess.

Read lines and split environment variable name, value and assign it to os.environ:

import os
with open('/home/user/env_script.env') as f:
 for line in f:
 if 'export' not in line:
 continue
 if line.startswith('#'):
 continue
 # Remove leading `export `
 # then, split name / value pair
 key, value = line.replace('export ', '', 1).strip().split('=', 1)
 os.environ[key] = value

or using dict.update and generator expression:

with open('env_script.env') as f:
 os.environ.update(
 line.replace('export ', '', 1).strip().split('=', 1) for line in f
 if 'export' in line
 )

Alternatively, you can make a wrapper shell script, which sources the env_script.env, then execute the original python file.

#!/bin/bash
source /home/user/env_script.env
python /path/to/original_script.py
radtek
36.6k13 gold badges149 silver badges114 bronze badges
answered May 12, 2017 at 9:23

11 Comments

i am getting this error while using first method key, value = line.replace('export ', '', 1).split('=', 1) ValueError: need more than 1 value to unpack
@user092, Thank you for the feedback. It seems like there's a line without export .... I updated the answer to skip such lines. Please try the updated code.
environment variables are not update in the system .no changes
@user092, Are you trying to change shell's env variable? Updating env vars of the process itself is possible. Changed env variables can be inherited to child processes. But, it's not possible to update parent process' env vars.
@user092, just run source /home/user/env_script.env in shell.
|
1

Modern operating systems do not allow a child process to change the environment of its parent. The environment can only be changed for the current process and its descendants. And a Python interpreter is a child of the calling shell.

That's the reason why source is not an external command but is interpreted directly by the shell to allow a change in its environment.

It used to be possible in the good old MS/DOS system with the .COM executable format. A .com executable file had a preamble of 256 (0x100) bytes among which was a pointer to the COMMAND.COM's environment string! So with low level memory functions, and after ensuring not overwriting anything past the environment, a command could change directly its parent environment.

It may still be possible in modern OS, but require cooperation from system. For example Windows can allow a process to get read/write access to the memory of another process, provided the appropriate permissions are set. But this is really a hacky way, and I would not dare doing this in Python.

TL/DR: if your requirement is to change the environment of the calling shell from a Python script, you have misunderstood your requirement.


But what is easy is to start a new shell with a modified environment:

import os
import subprocess
env = os.environ.copy() # get a copy of current environment
# modify the copy of environment at will using for example falsetru's answer
# here is just an example
env['AAA'] = 'BBB'
# and open a subshell with the modified environment
p = subprocess.Popen("/bin/sh", env = env)
p.wait()
answered May 12, 2017 at 10:48

1 Comment

Interesting but this answer is completely off topic, it doesn't answer the question in the original post

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.