Suppose we have the following:
import subprocess
import sys
a = 6
result = subprocess.run([sys.executable, "-c", "a*2"])
Why does it say a is not defined? Does the subprocess module remember prior variables?
Suppose we have the following:
import subprocess
import sys
a = 6
result = subprocess.run([sys.executable, "-c", "a*2"])
Why does it say a is not defined? Does the subprocess module remember prior variables?
subprocess created a new python process, as though you opened up a new terminal and ran python there. The new process does not share any memory, and certainly does not have access to the global variable dictionary of the current process.
You can expand the value of a directly in the string:
subprocess.run([sys.executable, "-c", f"print({a} * 2)"])
Alternatively, you can pass in the value of a by setting it in a separate statement:
subprocess.run([sys.executable, "-c", f"a = {a}; a * 2"])
In all cases, you need to print the result if you want to see console output.
If you want to use something other than numbers, use {a!r} in preference to just {a}, since repr is more likely to create a string that can be interpreted back to the original object.