2

I'm on Windows 10, using python with an API token. I want to store that token as an environment variable outside of my code, then access it in python like this:

import os
token = os.environ['TOKEN_NAME']

When I set the variable as a user environment variable, I can only access it via my admin account. Ex. TOKEN_ONE in the image link below.

When I set it as a system environment variable, I can additionally access via my non-admin account. Ex. TOKEN_TWO in the image link below.

Where it says "User variables for" at the top of the below image - the blacked out portion is my admin username- and I can't change it to set to my non admin username.

My path: Control Panel ⟶ System and Security ⟶ System ⟶ Advanced System Settings (asks for admin password) ⟶ Environment Variables

Environment Variables Settings


Is there a way to make a user environment variable, accessible from a user account, that is not system wide?

By accessible, I mean it shows up when I type set into terminal.


My use case doesn't necessarily need it to be local to the user, but it would be good to know how to do that if possible.

If not, I will learn more about virtual environments.

Thank you.

tadaclan
  • 23
  • 1
  • 5
  • Can you login as this user you are wanting to set the environment variable to? – TeddyBearSuicide Jun 04 '21 at 20:05
  • `os.environ['foo']` shows me a user specific setting. One thing you might be running into is that after you use that system control panel to set a variable you need **ok** out of the control panel (closing it) **and then** open a **new** console to see the changes. – JonSG Jun 04 '21 at 20:09
  • @Mythalorian Yes, I can log into both the admin and user account- I'm on the regular user account right now- where I want the environment variable access. – tadaclan Jun 04 '21 at 21:44
  • @JonSG No luck with closing out the control panel- Restarted pc again as well. – tadaclan Jun 04 '21 at 21:54

2 Answers2

9

I'm guessing you're on windows 10. The way I would set a user environment variable is this. There are other ways of getting to this screen but they require admin access which means it gets run as admin and set admin variables. Also make sure you do this while you're logged in as the user.

Control Panel -> User Accounts -> User Accounts -> Change my environment variables (left side).

This will open the user variables without admin access allowing the user's variables to be edited.enter image description here

enter image description here

enter image description here

Make sure the user account's name is showing where the red circle is.

enter image description here

TeddyBearSuicide
  • 1,377
  • 1
  • 6
  • 11
4

Shortcut in Windows 10: Click on Windows Logo -> Copy&Paste: rundll32.exe sysdm.cpl,EditEnvironmentVariables -> Press: ENTER

Michael
  • 71
  • 1
  • 5