When trying to write script with python, I have a fundamental hole of knowledge.
Update: Thanks to the answers I corrected the word shell
to process/subprocess
Nomenclature
- Starting with a Bash prompt, lets call this
BASH_PROCESS
- Then within
BASH_PROCESS
I runpython3 foo.py
, the python script runs in sayPYTHON_SUBPROCESS
- Within
foo.py
is a call tosubprocess.run(...)
, this subprocess command runs in say `SUBPROCESS_SUBPROCESS - Within
foo.py
issubprocess.run(..., shell=True)
, this subprocess command runs in saySUBPROCESS_SUBPROCESS=True
Test for if a process/subprocess is equal
Say SUBPROCESS_A
starts SUBPROCESS_B
. In the below questions, when I say is SUBPROCESS_A
== SUBPROCESS_B
, what I means is if SUBPROCESS_B
sets an env variable, when it runs to completion, will they env variable be set in SUBPROCESS_A
? If one runs eval "$(ssh-agent -s)"
in SUBPROCESS_B
, will SUBPROCESS_A
now have an ssh agent too?
Question
Using the above nomenclature and equality tests
- Is
BASH_PROCESS
==PYTHON_SUBPROCESS
? - Is
PYTHON_SUBPROCESS
==SUBPROCESS_SUBPROCESS
? - Is
PYTHON_SUBPROCESS
==SUBPROCESS_SUBPROCESS=True
? - If
SUBPROCESS_SUBPROCESS=True
is not equal toBASH_PROCESS
, then how does one alter the executing environment (e.g.eval "$(ssh-agent -s)"
) so that a python script can set up the env for the calller?
CodePudding user response:
You seem to be confusing several concepts here.
TLDR No, there is no way for a subprocess to change its parent's environment. See also Global environment variables in a shell script
You really don't seem to be asking about "shells".
Instead, these are subprocesses; if you run python foo.py
in a shell, the Python process is a subprocess of the shell process. (Many shells let you exec python foo.py
which replaces the shell process with a Python process; this process is now a subprocess of whichever process started the shell. On Unix-like systems, ultimately all processes are descendants of process 1, the init
process.)
subprocess
runs a subprocess, simply. If shell=True
then the immediate subprocess of Python is the shell, and the command(s) you run are subprocesses of that shell. The shell will be the default shell (cmd
on Windows, /bin/sh
on Unix-like systems) though you can explicitly override this with e.g. executable="/bin/bash"
Examples:
subprocess.Popen(['printf', '%s\n', 'foo', 'bar'])
Python is the parent process,
printf
is a subprocess whose parent is the Python process.subprocess.Popen(r"printf '%s\n' foo bar", shell=True)
Python is the parent process of
/bin/sh
, which in turn is the parent process ofprintf
. Whenprintf
terminates, so doessh
, as it has reached the end of its script.Perhaps notice that the shell takes care of parsing the command line and splitting it up into the four tokens we ended up explicitly passing directly to
Popen
in the previous example.The commands you run have access to shell features like wildcard expansion, pipes, redirection, quoting, variable expansion, background processing, etc.
In this isolated example, none of those are used, so you are basically adding an unnecessary process. (Maybe use
shlex.split()
if you want to avoid the minor burden of splitting up the command into tokens.) See also Actual meaning of 'shell=True' in subprocesssubprocess.Popen(r"printf '%s\n' foo bar", shell=True, executable="/bin/bash")
Python is the parent process of Bash, which in turn is the parent process of
printf
. Except for the name of the shell, this is identical to the previous example.There are situations where you really need the slower and more memory-hungry Bash shell, when the commands you want to execute require features which are available in Bash, but not in the Bourne shell. In general, a better solution is nearly always to run as little code as possible in a subprocess, and instead replace those Bash commands with native Python constructs; but if you know what you are doing (or really don't know what you are doing, but need to get the job done rather than solve the problem properly), the facility can be useful.
(Separately, you should probably avoid bare Popen
when you can, as explained in the subprocess
documentation.)
Subprocesses inherit the environment of their parent when they are started. On Unix-like systems, there is no way for a process to change its parent's environment (though the parent may participate in making that possible, as in your eval
example).
To perhaps accomplish what you may ultimately be asking about, you can set up an environment within Python and then start your other command as a subprocess, perhaps then with an explicit env=
keyword argument to point to the environment you want it to use:
import os
...
env = os.environ.copy()
env["PATH"] = "/opt/foo:" env["PATH"]
del env["PAGER"]
env["secret_cookie"] = "xyzzy"
subprocess.Popen(["otherprogram"], env=env)
or have Python print out values in a form which can safely be passed to eval
in the Bourne shell. (Caution: this requires you to understand the perils of eval
in general and the target shell's quoting conventions in particular; also, you will perhaps need to support the syntax of more than one shell, unless you are only targeting a very limited audience.)
... Though in many situations, the simplest solution by far is to set up the environment in the shell, then run Python as a subprocess of that shell instance (or exec python
if you want to get rid of the shell instance after it has performed its part; see also What are the uses of the exec command in shell scripts?)
Python without an argument starts the Python REPL, which could be regarded as a "shell", though we would commonly not use that term (perhaps instead call it "interactive interpreter" - see also below); but python foo.py
simply runs the script foo.py
and exits, so there is no shell there.
The definition of "shell" is slightly context-dependent, but you don't really seem to be asking about shells here. (Some GUIs have a concept of "graphical shell" etc but we are already out of the scope of what you were trying to ask about.) Some programs are command interpreters (the Python executable interprets and executes commands in the Python language; the Bourne shell interprets and executes shell scripts) but generally only those whose primary purposes include running other programs are called "shells".
CodePudding user response:
None of those equalities are true, and half of those "shells" aren't actually shells.
Your bash shell is a shell. When you launch your Python script from that shell, the Python process that runs the script is a child process of the bash shell process. When you launch a subprocess from the Python script, that subprocess is a child process of the Python process. If you launch the subprocess with shell=True
, Python invokes a shell to parse and run the command, but otherwise, no shell is involved in running the subprocess.
Child processes inherit environment variables from their parent on startup (unless you take specific steps to avoid that), but they cannot set environment variables for their parent. You cannot run a Python script to set environment variables in your shell, or run a subprocess from Python to set your Python script's environment variables.