1

So, a scenario:

Say I have my python project in a directory named 'pyproj'. There's the main file, 'myproj.py', and another file ('config.py') which holds some variables:

cfg_x = {}
cfg_y = 1031
cfg_z = 'Hello!'

Inside of 'myproj.py', would it be possible to set the variable...

import config

cfg_x['example'] = 'different value'

...and have that stay set (written to the file)? I've attempted to do this using the code above, however the variable still stays {} if I exit the python console and run it again.

sethg
  • 159
  • 1
  • 1
  • 10
  • can you please be more specific@Alphys – Navi Apr 07 '18 at 07:17
  • Why cannot you put your config to JSON or YAML object? If I remember right AST which would allow dynamically modify .py files from another py file was removed as it just creates complexity and bad code – Kimmo Hintikka Apr 07 '18 at 07:23
  • 1
    Don't do this. Avoid global state, avoid tight coupling with your config file. Instead create a way of injecting the necessary data in your module in a way that allows for the module to be easily testable. – Keilo Apr 07 '18 at 07:24
  • I don't agree that this question is the exact duplicate of the cited existing question. The kernel of the question is not about config files (even if the imported file is named "config") but about the possibility to import variables from one namespace to the other. – sciroccorics Apr 07 '18 at 08:43
  • Looking into the post this was marked as a duplicate of, that's precisely what I was trying to do here. I apologize, I should have worded it better; for some reason I didn't think of it as a config file at the time, but thinking on it more that's exactly what I wanted to do. – sethg Apr 08 '18 at 22:06

2 Answers2

0

Here is a simplified version using config.json for what you want to do. Note this creates malformed JSON so you might want to edit a bit.

config.py creates config.json file from config python dict by writing to file with standard lib json.

import json

config = {"cfg_x": {"example": "old value"}, "cfg_y": 1031, "cfg_Z": "Hello"}

with open('config.json', 'w') as file:
    json.dump(config, file)

created file

~/Desktop/octocat » cat config.json                                                          
{"cfg_x": {"example": "old value"}, "cfg_y": 1031, "cfg_Z": "Hello"}%                                                                     (venv1)

myproj.py opens with config.json read mode 'r' changes nested key and writes to file.

import json

with open('config.json', 'r') as file:
    config = json.load(file)

config["cfg_x"]["example"] = "different value"

with open('config.json', 'w') as file:
    json.dump(config, file)

modified file.

~/Desktop/octocat » cat config.json                                                          
\{"cfg_x": {"example": "different value"}, "cfg_y": 1031, "cfg_Z": "Hello"}%                                                              (venv1)

In real prod scenario, I would generally use 1 of million Python config libs that already exists rather than something like this. Here is an example: https://jbasko.github.io/configmanager/

If you use this, the bare minimum you want to modify config.py to check if the file already exists rather than overwriting the whole config.json.

Kimmo Hintikka
  • 13,472
  • 7
  • 34
  • 63
0

As said by others, this is CLEARLY NOT THE GOOD WAY to use config files. But if you REALLY want to do this kind of stuff (after all, I guess you are a consenting adult), simply replace your first line:

from config import *

print(cfg_x)
cfg_x['example'] = 'new value'
print(cfg_x)

produces the following output:

{}
{'example': 'new value'}
sciroccorics
  • 2,357
  • 1
  • 8
  • 21