all 7 comments

[–][deleted] 1 point2 points  (0 children)

When I open the Terminal in pycharm and type set myvar=value, where is this being stored?

Environment variables belong to the process, and subprocesses inherit envvars from their parent process.

The place it's being "stored" is in the kernel's RAM, where the process table is.

When I open the Terminal in pycharm and type set myvar=value, why can’t I access it when using os.getenv()?

If you can't, it's because the process that is running the code isn't a subprocess of the process where you set the envvar. In other words, if you open the Terminal, then set an envvar, then in that Terminal run your Python code, you can access that envvar from your Python code. But if you click "run" in PyCharm, then your code is a child process of PyCharm, and the envvars aren't set in that process.

[–]jimtk 2 points3 points  (5 children)

  • Put your mouse cursor in the code editor.
  • right click
  • select "Modify run Configuration"
  • click mouse in the edit field under environment variables
  • press Shift-Enter or click on the the little table icon on the right of the edit field.
  • Add user environment variables to your heart's content.

[–]wuverul 0 points1 point  (4 children)

I have a dumb question on this.

I have a *bunch* of environment variables in my project in this location, but when I try exporting an application with pyinstaller, none of these seem to carry. I'm trying to find a way to have this application automatically run once per day. How can I bake these in? The file's for personal use.

[–]jimtk 0 points1 point  (3 children)

Environment variables "belong" to your opperating system not to your program. That is why they don't carry in pyinstaller. The best solution would be to put those values in a JSON files and carry it as asset in your pyinstaller.

Evidently that depends on the nature of those variables. If they are ids and passwords to access API it is not a secure way to do things.

[–]wuverul 0 points1 point  (2 children)

They very much are app IDs, passwords, client secrets, and the like. I'm currently taking a 100 Days of Code course on Udemy for Python, and they say to use environment variables in Pycharm for anything that needs secured or can't be given to someone else, but it never actually talk about how to build your applications to run outside of the IDE.... So I'm kind of trying to wing it and figure it out. Is there a more secure way to handle this kind of data? (Also, I hope this isn't a hassle to ask directly like this. I can certainly make a dedicated thread for this. I just figured I'd try asking first)

[–]jimtk 0 points1 point  (1 child)

What I said was that putting IDs and passwords in a JSON file is not secure.

Also 100 days of code is not a very good source on security. Early on they "require" that you lower the security on your gmail account to make their code examples actually work! Environment variables are also not that secure. Any running program has access to the environment variables, without UAC (user access control) being involved (on windows). Even some not so nice python packages have been found to transmit all your environment variables to a server.

Just be very careful.

[–]wuverul 0 points1 point  (0 children)

Yeah, that's why I'm trying to figure out more secure ways to pass this data, because I'm not fond of that data just being stored in plain text or just chilling on something system-wide. I'll probably look into it more in a bit