mattyyey
Posts: 43
Joined: Sat Jan 11, 2014 11:44 am

import variables and change and save.

Sun Sep 15, 2019 1:24 pm

Ok so I have muliple python3 programs but I'm using them more as modules to a larger program. So save setting the variables in each module I have made one which ONLY contains the variables I need.

first running program:

Code: Select all

import config_file
import subprocess
cmd = config_file.cmd
light1_on = config_file.light1_on
#below works to modify the cmd
cmd = cmd + light 1_on

output = (subprocess.check_output(cmd))
#below does not SAVE into config_file after program exits
config_file.output = str(output)
#program now exits
config_file

Code: Select all

cmd = "/home/pi/433/codesend "
light1_on = "288888"
light1_off = "18888"
#below is never updated and therefore when i run a different module output is blank
output = ""
2nd running program:

Code: Select all

import config_file
output = config_file.output
#always blank
print(output)
This is just an example of how I'm doing it not my real program. I know I could create a file (csv or w.e.) to log stuff but I want it easily importable from multiple python scripts which run independently...

So how am I MEANT to do it?

User avatar
paddyg
Posts: 2422
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: import variables and change and save.

Sun Sep 15, 2019 8:35 pm

You can pickle data very easily, or just read/write json to files, or use sqlite. Lots of info online for all these.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Andyroo

Re: import variables and change and save.

Sun Sep 15, 2019 9:19 pm

I like using the old ini file format beloved of Windows 3.11 etc :lol:

This is done with the configparser module.

Though I have a background in EDI formats, XML, JSON etc, I find the ini format neat and very readable.

mattyyey
Posts: 43
Joined: Sat Jan 11, 2014 11:44 am

Re: import variables and change and save.

Mon Sep 16, 2019 11:25 am

I was aware of writing json and other file formats but due to the nature of what I'm doing I was trying to avoid having lots of config files. I just want one easily importable to all my scipts.
Andyroo wrote:
Sun Sep 15, 2019 9:19 pm
I like using the old ini file format beloved of Windows 3.11 etc :lol:
This is done with the configparser module.
Though I have a background in EDI formats, XML, JSON etc, I find the ini format neat and very readable.
As funny as it is to be using windows3.1 .ini files in linux ConfigParser actually looks like what im after. thanks! :D its a little more work than just importing my config_file but does solve me problem. Plus like you said its very readable.

Return to “Python”