0

I'm running a script that is grabbing data from a .env file. I am trying to update the .env variable in the middle of the script and then use that new value. It's an encryption script, so I need to have the .env value to decrypt, update the value, and then use the new value to encrypt it again. What's happening instead is it is decrypting, reencrypting, and then updating. It seems like that's just how .env files work, but I was hoping there was a way to make the variable update mid-script. Here's the code that I'm running:

def changeSecret(slices, envvars, num):
    key = sign(slices) #function I wrote decrypting several slices of data and putting them together
    dotenv_file = dotenv.find_dotenv()
    dotenv.load_dotenv(dotenv_file)
    dotenv.set_key(dotenv_file, 'secret', str(num)) #this ends up updating the .env variable only when the program finishes
    slices = encrypt(key) #the 'encrypt' function uses os.getenv('secret'), so I want it to have updated already
    updateenv(slices, envvars) #this updates other variables.
Ilan Katz
  • 3
  • 2
  • Does this answer your question? https://stackoverflow.com/questions/34820913/change-environment-variables-persistently-with-python or https://stackoverflow.com/questions/716011/why-cant-environmental-variables-set-in-python-persist – SLDem Dec 19 '22 at 18:25
  • Yeah, I think it does. So basically it's impossible, which is super annoying. – Ilan Katz Dec 19 '22 at 21:39
  • You can probably just use two data files of txt format to store your data instead of using the `env` module use context managers – SLDem Dec 20 '22 at 09:01

0 Answers0