Assign environment variables from bash script to current session from Python
Using shell=True
With Your Existing Script
First, in terms of the very simplest thing -- if you're using shell=True
, you can tell the shell that starts to run the contents of your preexisting script unmodified.
That is to say -- if you were initially doing this:
subprocess.Popen(['your-command', 'arg1', 'arg2'])
...then you can do the following to execute that same command, with almost the same security guarantees (the only additional vulnerabilities, so long as the contents of file1.sh
are trusted, are to out-of-band issues such as shellshock):
# this has the security of passing explicit out-of-band args
# but sources your script before the out-of-process command
subprocess.Popen(['. "$1"; shift; exec "$@"', "_", "./file1.sh",
"your-command", "arg1", "arg2"], shell=True)
Using /proc/self/environ
to export environment variables in a NUL-delimited stream
The ideal thing to do is to export your environment variables in an unambiguous form -- a NUL-delimited stream is ideal -- and then parse that stream (which is in a very unambiguous format) in Python.
Assuming Linux, you can export the complete set of environment variables as follows:
# copy all our environment variables, in a NUL-delimited stream, to myvars.environ
cat </proc/self/environ >myvars.environ
...or you can export a specific set of variables by hand:
for varname in HORCMINST PATH; do
printf '%s=%s\0' "$varname" "${!varname}"
done >myvars.environ
Reading and parsing a NUL-delimited stream in Python
Then you just need to read and parse them:
#!/usr/bin/env python
env = {}
for var_def in open('myvars.environ', 'r').read().split('\0'):
(key, value) = var_def.split('=', 1)
env[key] = value
import subprocess
subprocess.Popen(['your-command', 'arg1', 'arg2'], env=env)
You could also immediately apply those variables by running os.environ[key]=value
.
Reading and parsing a NUL-delimited stream in bash
Incidentally, that same format is also easy to parse in bash:
while IFS= read -r -d '' var_def; do
key=${var_def%%=*}
value=${var_def#*=}
printf -v "$key" '%s' "$value"
export "$key"
done <myvars.environ
# ...put the rest of your bash script here
Now, why a NUL-delimited stream? Because environment variables are C strings -- unlike Python strings, they can't contain NUL. As such, NUL is the one and only character that can be safely used to delimit them.
For instance, someone who tried to use newlines could be stymied by an environment variable that contained a literal newline -- and if someone is, say, embedding a short Python script inside an environment variable, that's a very plausible event!
Set shell environment variable via python script
As long as you start the "instrument" (a script I suppose) from the very same process it should work:
In [1]: os.putenv("VARIABLE", "123")
In [2]: os.system("echo $VARIABLE")
123
You can't change an environment variable of a different process or a parent process.
Can a shell script set environment variables of the calling shell?
Your shell process has a copy of the parent's environment and no access to the parent process's environment whatsoever. When your shell process terminates any changes you've made to its environment are lost. Sourcing a script file is the most commonly used method for configuring a shell environment, you may just want to bite the bullet and maintain one for each of the two flavors of shell.
How to set global environment variables using shell script .sh
According to Ubuntu env variables doc the best way would be
A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment
That's assuming you don't mind having them set for the whole machine.
How to set environment variables for current Command Prompt session from Python
Brute-force but straightforward is to emit your assignments as a batch script on stdout, and execute that script in the existing interpreter (akin to source
in bash):
python myscript >%TEMP%\myscript-vars.bat
call %TEMP%\myscript-vars.bat
del %TEMP%\myscript-vars.bat
How do i set environment variable using python script
This is not possible.
Environment variables are defined in the scope of the job that sets it. That's why when you define an environment variable in one terminal session ,you won't see it in other sessions. The same thing is happening with your script. A job runs your python script but you are trying to read variables from another terminal session(another job), so you can't see them. Depending on your goal, you have to find a workaround.
Access exported variables by a called script from python
You can't. Environment variables are copied from parent to child, never back to the parent.
If you execute a shell script from python then the environment variables will be set in that shell process (the child) and python will be unaware of them.
You could write a parser to read the shell commands from python, but that's a lot of work.
Better to write a shell script with the settings in that and then call the python program as a child of the script.
Alternatively, write a shell script that echoes the values back to python which can be picked-up using a pipe.
Read Bash variables into a Python script
You need to export the variables in bash, or they will be local to bash:
export test1
Then, in python
import os
print os.environ["test1"]
Related Topics
Which Python Packages Offer a Stand-Alone Event System
How to Take Column-Slices of Dataframe in Pandas
Pip Uses Incorrect Cached Package Version, Instead of the User-Specified Version
How to Convert a Pandas Series or Index to a Numpy Array
How to Validate a Url with a Regular Expression in Python
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List
Differencebetween Flatten and Ravel Functions in Numpy
How to Execute a File Within the Python Interpreter
Count Unique Values Per Groups with Pandas
Make Sure Only a Single Instance of a Program Is Running
How to Validate Ip Address in Python
Appending the Same String to a List of Strings in Python
Python String.Replace Regular Expression
Python Creating a Dictionary of Lists
Pandas Dataframe Groupby Two Columns and Get Counts
Is Shared Readonly Data Copied to Different Processes for Multiprocessing