pythonwindowsterminalenvironment-variables

How can I create user environment variables for a non-admin user account?


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.


Solution

  • 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