How to set a environment variable in the current shell with Python? - python

I want to set an environment variable with a Python script, influencing the shell I am starting the script in. Here is what I mean
python -c "import os;os.system('export TESTW=1')"
But the command
echo ${TESTW}
returns nothing. Also with the expression
python -c "import os;os.environ['TEST']='1'"
it does not work.
Is there another way to do this in the direct sense? Or is it better to write the variables in a file which I execute from 'outside' of the Python script?

You can influence environment via: putenv BUT it will not influence the caller environment, only environment of forked children.
It's really much better to setup environment before launching the python script.
I may propose such variant. You create a bash script and a python script. In bash script you call the python script with params. One param - one env variable. Eg:
#!/bin/bash
export TESTV1=$(python you_program.py testv1)
export TESTV2=$(python you_program.py testv2)
and you_program.py testv1 returns value just for one env variable.

I would strongly suggest using the solution proposed by chepner and Maxym (where the Python script provides the values and your shell exports the variables). If that is not an option for you, you could still use eval to execute what the python script writes in your current Bash process:
eval $( python -c "print('export TESTW=1')" )
Caution: eval is usually read "evil" in Bash programming. As a general rule of thumb, one should avoid "blindly" executing code that is not fully under one's control. That includes being generated by another program at runtime as in this case. See also Stack Overflow question Why should eval be avoided in Bash, and what should I use instead?.

Related

Is it possible to set bash/linux terminal environment Variables from Python Script? [duplicate]

i'm calling a python script inside my bash script and I was wondering if there is a simple way to set my bash variables within my python script.
Example:
My bash script:
#!/bin/bash
someVar=""
python3 /some/folder/pythonScript.py
My python script:
anotherVar="HelloWorld"
Is there a way I can set my someVar to the value of anotherVar? I was thinking of printing properties in a file inside the python script and then read them from my bash script but maybe there is another way. Also I don't know and don't think it makes any difference but I can name both variable with the same name (someVar/someVar instead of someVar/anotherVar)
No, when you execute python, you start a new process, and every process has access only to their own memory. Imagine what would happen if a process could influence another processes memory! Even for parent/child processes like this, this would be a huge security problem.
You can make python print() something and use that, though:
#!/usr/bin/env python3
print('Hello!')
And in your shell script:
#!/usr/bin/env bash
someVar=$(python3 myscript.py)
echo "$someVar"
There are, of course, many others IPC techniques you could use, such as sockets, pipes, shared memory, etc... But without context, it's difficult to make a specific recommendation.
shlex.quote() in Python 3, or pipes.quote() in Python 2, can be used to generate code which can be evaled by the calling shell. Thus, if the following script:
#!/usr/bin/env python3
import sys, shlex
print('export foobar=%s' % (shlex.quote(sys.argv[1].upper())))
...is named setFoobar and invoked as:
eval "$(setFoobar argOne)"
...then the calling shell will have an environment variable set with the name foobar and the value argOne.

R: system() cannot use bash function defined in .bashrc

Question
My default Python is 2.7, but I have a script that requires Python 3.4. I am trying to create a function in R that will:
Switch to Python 3.4
Run this script
Switch back to Python 2.7
Import results into R
To switch between Python versions, I use my cluster's "dotkit" system, like this:
use Python-2.7
use Python-3.4
"use" is a bash function that is imported in my .bashrc file. It sets all of my path variables (PATH, LIBRARY_PATH, LD_LIBRARY_PATH, CPATH, C_INCLUDE_PATH, etc). The problem is that when I try to call this function in R, I get the following error:
system('use Python-3.4')
sh: use: command not found
It seems like this is a problem with my PATH. I am using the correct shell:
system('echo $SHELL')
/bin/bash
My $PATH variable also looks good. However, when I create a script that essentially does the same thing:
load_py34.sh:
#!/bin/bash
source ~/.bashrc
use Python-3.4
and call this script through R, then it actually runs, but for some reason, it doesn't change my python version within R. (I have verified that this script works from the command line.)
> R
> system('python --version')
Python 2.7.1
> system('sh load_py34.sh')
Prepending: R-3.4 (ok)
> system('python --version')
Python 2.7.1
So I'm a little confused, but if anyone can help, I would really appreciate it.
Suggested fixes
When I combine them into a single command, I still have the same problem:
> system("sh load_py34.sh; python --version")
Prepending: Python-3.4 (already loaded)
Python 2.7.1
When I try calling bash directly, I still have a problem with the PATH:
> system("bash -c 'use Python-3.4; python --version'")
bash: use: command not found
Python 2.7.1
.bashrc is only loaded for interactive bash sessions.
"use" is a bash function that is imported in my .bashrc file. It sets
all of my path variables.
If set via export, the environment of the calling process will not be altered.
export [-fn] [name[=word]] ... The supplied names are marked for automatic export to the environment of subsequently executed commands. (https://man7.org/linux/man-pages/man1/bash.1.html)
Child processes do not normally have access to the parent process' environment. (This poses a problem because system() creates a sub-process.)
The source and . built-ins execute the commands in the current shell environment, hence why your script works.
Other commands (executables, non-shell-builtins) are executed by the fork-and-exec mechanism, whereby the executing shell process forks, creating a child process with an identical environment and state. This new child process is the process in which the command is executed. Changes to the environment of that process are not replicated to the parent's environment.
This means that you will not be able to rely on system('...') to modify the environment of the R process, or that of processes spawned by subsequent system() invocations.
In a single invocation to system(), you can construct a command-line that changes the environment of the spawned shell like so:
bash -c 'source ~/.bashrc; use Python-3.4; python --version'
Mind you, ~/.bashrc is not really the best place to put this functionality (might be subjective).
When you call system() it uses /bin/sh, not /bin/bash. sh doesn't read your .bashrc file when it starts up, so it does not know any of the functions you've defined there.
To use the function from your .bashrc, you must get bash to run it instead:
system("bash -c 'use Python-3.4; python --version'")
Edit: placement of closing single quote.

Setting environment variables within python

There are quite a few question for this topic but unfortunatly they didn't solve my problem.
I have a shell script whose purpose is only to set environment variables (let's call it env.sh). A second shell script is the main program script that sources env.sh and is using variables that are defined there. This works fine when both are bash-scripts.
I have the problem that I try to replace the main shell script with a python program. This python program does also depend on those environment variables that are set by env.sh.
What can I do to source env.sh within python before starting any routine that use those environmen variables?
I have to run bash -c source env.sh with the module subprocess. But if I understand correctly this does not work, because the variables are set in the child process of the calling python program and are therefore not available in the parent process. Is this correct?
A similar solution would have been to use bash -c source env.sh && env and read the output of env in python, iterate over the list and write into os.environ. But this approach would write every variable again, even if it was already defined. What if there are a lot of variables?
The last solution I could think of was to read and parse the env.sh file and set the variables I can find. One problem is that some exports in env.sh are nested, mening:
export SOMETHING=$FOO/Bar
This could become complicated to parse correctly.
Is there another way that I haven't found yet or didn't think of?
Some options:
https://github.com/mattseymour/python-env
https://github.com/rconradharris/envparse
Env variable sourcing is very common, I would not recommend rolling your own.

exporting ruby variables to parent process

I am new to ruby. Still learning it.
I have bash script and a ruby script, having independent responsibilities.
The bash script calls the ruby script with some command line arguments. The ruby script, after parsing these command line args will set/unset some variables local to it. These variables will have the same use/name in the parent bash script.
So, i need to have the modified values of the variables in the ruby script be reflected in the parent bash script also.
Couple of approaches I have thought of are:
1.) Export the variables to the environment in bash, then have ruby modify them by using the ENV[] hash. In this case, there wouldn't be any need to have local variables in ruby to store these values, ruby would directly modify the imported variables from bash, as they are environment variables. However, would these changed values of the environment be reflected in the bash script?
2.) After the ruby script returns to the parent bash script, call on some methods of ruby that will return the a variable's value to it. So for each variable, a method is called. Something like for python,
python -c 'import python_module; print python_method()'
Is this possible in ruby?
If none of the above two are possible, is there any other way to have the values of ruby variables be reflected back in the parent bash script?
Or, is there any scripting option other than ruby/python that can help me do this?
I Appreciate the time in reading this post and helping me out. :-)
The simplest mechanism would be similar to the one used by the ssh-agent program.
Have your program output a set of environment export statements like
export THIS=something
export THAT=something_else
and then have the calling script eval the result, as in
eval `my_program`
This will cause the shell to incorporate your environment variables. For example, here is a small Python program.
print "export THIS=something"
print "export THAT='something else'"
When I run it, it outputs the two export statements:
airhead:Python sholden$ python stackoflo.20140710-1.py
export THIS=something
export THAT='something else'
So when I eval the result the export statements are executed by the calling shell:
airhead:Python sholden$ eval `python stackoflo.20140710-1.py`
The variables its sets are now a part of the environment:
airhead:Python sholden$ echo $THIS
something
airhead:Python sholden$ echo $THAT
something else
Finally, here's proof that the values do go into the shell's environment: a subshell sees them.
airhead:Python sholden$ bash
bash-3.2$ echo $THIS
something

problem with python script

I want to run a csh file from a python script,
example,
#!/usr/bin/python
import os
os.system("source path/to/file.csh")
and I want this file to run in the same shell as I am running the python script, because the file.csh script is settings some environment variables that I need.
Does anyone know how to do this in Python?
A child process cannot affect the environment of the parent process. The best you can do is to run your csh script in a separate process, get the environment variables that it defines, then set each environment variable in your python script.
Even with that, the python script won't be able to affect the shell in which you run the python script.
The common way to solve this (AFAIK) is to have your script emit shell commands to set the environment, then from the main shell you run the script and eval what you get back.
For more information you might want to check out this question: can a shell script set environment variables of the calling shell
You can kludge it this way:
#!/usr/bin/env python
# This is kludge.py
print "setenv VARNAME \"the value\""
In your case, you can have the file.sh print the setenv line.
Then from csh:
$ eval `./kludge.py`
$ echo $VARNAME
the value
This isn't clean, but it is the only way to have a child process effect the environment of its parent. This is only because the parent process is explicitly letting it happen with eval.

Categories

Resources