1

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.

1
  • Did you already try threading? Commented Jun 29, 2020 at 17:56

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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 <module> with open("data", "r") as json_file: FileNotFoundError: [Errno 2] No such file or directory: 'data' ```
Make 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.