Consider the following program, which is saved in a file named script.py:
x = 2/3
x
(This is a script in Python, but actually my real script is in R -- I am using this Python script just for illustration of what I am wanting.)
Now, I want to use a second program in Python to get the numerical value of variable x calculated in script.py.
The following program accomplishes that but only partially, in the sense that the number with all decimal digits is not returned. In truth, float(s) does not have the same decimal precision as x.
import subprocess
cmd_line = ["python", "script.py"]
s = subprocess.check_output(cmd_line, text=True)
float(s)
What I want to obtain is the value of x with the same precision as it gets in script.py.
EDIT
This question was closed because -- it is argued -- it is a duplicate of previous question. I am not looking for a way to get arbitrary decimal precision: I am looking for a way to pass a numerical result of a Python script to a second Python script.
Please, I kindly and respectfully ask you for reopening the question.
float(s)doesn't work. You'd want to format a string for stdout and that's where you'd make your precision decision.print(f"{x:.16f}")should doimportand then passing arguments to functions. That said, only raw bytes can be passed between processes - it is up to both ends of that communication to establish the protocol for interpreting the data. Just like when you read and write a file; it only stores raw bytes. (Yes, "ASCII text" is a protocol.)