I have tow python script running on the same computer at he same time. I need one script to import variables from the other script. I don't want to import functions, I want data, accurate to the second. Any help would be greatly appreciated.
Asked
Active
Viewed 61 times
1
-
Did you already try threading? – Jonas Jun 29 '20 at 17:56
1 Answers
2
I would suggest you take the data from the variable and put it into an external file then access the information that way.
Example:
a.py
import json
data = 1
with open(r"examples\data.json", "w") as json_file:
info = {"Data" : data}
json.dump(info, json_file)
b.py
import json
with open(r"examples\data.json", "r") as json_file:
file = json.load(json_file)
data = file["Data"]
data.json
{"Data": "1"}
I hope this helps!

NoName69
- 172
- 2
- 14
-
I got this error when I ran your code: ``` Traceback (most recent call last): File "/home/pi/lib-python/examples/sever.py", line 13, in
with open("data", "r") as json_file: FileNotFoundError: [Errno 2] No such file or directory: 'data' ``` – UNKNOWN Jun 30 '20 at 13:03 -
1Make sure data.json is in your current working directory. + I changed the code a little to specify that it's a JSON file and that it's in the examples folder. Make those changes and the code should work without any problems. – NoName69 Jun 30 '20 at 16:02