Answer the question
In order to leave comments, you need to log in
How to edit Windows system variables or current user variables in python?
At first it might seem like this:
os.environ['DEBUG']='FALSE'
os.environ.get('DEBUG')
del os.environ.get('DEBUG')
python test.py --no-debug initdb
python test.py --debug initd
if "DEBUG" not in os.environ:
os.environ["DEBUG"] = "FALSE"
print('debug mode is off')
def logging(s):
if s=='FALSE': return False
if s=='TRUE': return True
@click.group()
@click.option('--no-debug', is_flag=True)
@click.option('--debug', is_flag=True)
def main(no_debug, debug):
if no_debug:
os.environ["DEBUG"] = "FALSE"
print('debug mode is off')
if debug:
os.environ["DEBUG"] = "TRUE"
print('debug mode is on')
@click.command()
@click.option('--name')
def initdb(name):
---------------------------------
if logging(os.environ.get('DEBUG')):
log.write(f'initialization {name} database....\n')
@click.command()
@click.option('--name')
def dropdb(name):
---------------------------------
if logging(os.environ.get('DEBUG')):
log.write(f'dropping {name} database....\n')
Answer the question
In order to leave comments, you need to log in
The question actually was how to create and edit environment variables at the user or system level at the process level?
update Found the answer myself. Environment variables are keys in the registry:
System variables HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment
Current user variables HKEY_CURRENT_USER\Environment
Example:
key=winreg.CreateKey(winreg.HKEY_CURRENT_USER, "Environment")
winreg.SetValueEx(key, "DEBUG", 0, winreg.REG_SZ, "FALSE")
Environment variables have hierarchical scopes:
Система
Пользователь
Сеанс
Процесс
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question