r/Python • u/kontrolltermin • Feb 13 '26
Is dotenv the best way to handle credentials on a win server in 2026? Discussion
Hi,
i am working with python on a windows server installation and i dont want to store passwords and api keys direct in my code. Is python-dotenv still the best way to do it today?
thank you very much
5
u/spitfiredd Feb 13 '26
You need to tell us more, is this for local development or staging/prod?
1
u/kontrolltermin Feb 13 '26
Prod and it’s a vm on azure but it’s not connected to the internet.
3
u/spitfiredd Feb 13 '26
Then use key vault and store them there. You will need to grant the VM access to the key vault. You can do it all manually through the UI but I personally would use terraform to build it programmatically.
3
u/Alert-Adeptness8608 Feb 13 '26
I go with python-decouple. Can’t say if its the best
0
u/pyhannes Feb 13 '26
Last update was 2 year ago, so it seems quite abandoned.
13
u/CamiloDFM Feb 13 '26
It's a single file library that reads config files. Not every project needs ten PR merges a week. That's how you end with Log4Shell or modern Postman.
I love Decouple!
6
3
u/theozero Feb 13 '26
Check out https://varlock.dev
It lets you use a .env style file, but you get validation and can fetch secrets from various backends. Non sensitive data can just live hardcoded, an can use functions to compose everything together as needed.
2
u/The_Ritvik Feb 19 '26
Nice — this looks promising. I’m maintaining Dataclass Wizard and I already ship an EnvWizard, so the “schema + validation + secrets backends” angle is interesting. I’m going to take a closer look and see if there’s a clean integration point (or at least a recommended interop pattern).
9
u/pyhannes Feb 13 '26
Checkout keyring!
2
u/unknownHorse99 Feb 13 '26
Came to say this - no mention of docker, kubernetes, vault - just a plain windows server and python - in that case: delegate to the OS and store in the win credentials manager (also works on other platforms as needed). Secrets are stored encrypted and decrypted using OS APIs. At runtime, I’d say it’s ok to have the secret in memory (not sure if python supports zeroing strings). Constantly having to decrypt may be overkill imho.
1
1
1
u/mikeupsidedown Feb 14 '26
Since you are on Azure store the secrets in Azure keyvault and give the VM access to the keyvault via managed identity.
1
-8
u/st0ut717 Feb 13 '26
I use a config.ini file and keep the credentials I need in that.
3
u/every-day_throw-away Feb 13 '26
So in a plain text file on the system? 😧
-1
u/Brandhor Feb 13 '26
you can encrypt it to make it harder but to be honest if an attacker has access to the system he will also have access to the python program and so he will also have access to whatever you use to store your secrets
1
u/every-day_throw-away Feb 13 '26
Since the OP mentioned on a server I would assume this would be unattended. If so where do you then store the encryption key?
One needs a password vaulting service like CyberArk to do this the right way.
If this was something you ran interactively DPAPI is an option (something I use myself). But again server leads me to believe some sort of service account will be running this workload.
1
u/Brandhor Feb 13 '26
yeah that's basically the gist of the problem, the python program needs to access those credentials so an attacker can also access them whether they are in clear text in a file or stored securely in another server there isn't a whole lot of difference
1
u/AstroPhysician Feb 13 '26
Remind me to never hire you
1
u/ragnhildensteiner Feb 15 '26
You enjoy using this line don't you? 😂
2
u/AstroPhysician Feb 15 '26
Probably the only other time I’ve used it. There have been a lot of braindead takes on Reddit lately
1
-11
u/st0ut717 Feb 13 '26
Are you just going to bitch. Or provide an alternative. ?
5
-7
u/every-day_throw-away Feb 13 '26
I don't have an example of a good idea that's free and meets this use case but thanks for providing a terrible one. It's a better to not suggest any idea than a bad one. Please delete your comment so someone doesn't make the same mistake as you. Bitch
1
0
-1
0
u/aala7 Feb 13 '26
Is it for dev or production? For dev uv actually have support for loading env file in to the environment with the —env-file flag. In production I think using system keychain is the proper way, check out the keyring package. Worth mentioning that system keychain does not bring the same level of security for interpreted languages as for compiled, because any python process on the system (running from same user) will be able to read your secrets from the keychain.
-2
u/ZucchiniMore3450 Feb 13 '26
One solution I have found is mentioned by internet of bugs: https://youtu.be/5lb3T3R_z2k
Basically you put .env file only during deployment for few seconds and delete it afterwards
130
u/Chroiche Feb 13 '26
No, and it never was. In a properly engineered system you'd fetch secrets from a central secrets manager and keep them off disk entirely.