[Python] Python: How to add variable in run statement?

Discussão em 'Python' iniciado por Stack, Agosto 17, 2025.

  1. Stack

    Stack Membro Participativo

    My vs code looks like this:

    import requests
    import json
    import os

    os.environ = "website.abc.com"
    header={"Accept":application/json", "Authorization": f"Bearer {os.environ['MY_API_KEY']}"

    #then a parameter list={}
    #other code...


    I am using windows and my other files run with no problems but when I try to add the api key, the system prints an error message related to the os.environ variable,

    # assume MY_API_KEY = 'abcd1234'

    >python myfile.py MY_API_KEY='abcd1234'


    I searched for answers online then I tried this:

    >set MY_API_KEY = 'abcd1234' && myfile.py


    then I searched again and tried this:

    >MY_API_KEY='abcd1234' python myfile.py


    and the error is always highlighting the MY_API_KEY variable with this message: frozen os in getitem. I looked for the definition of the error which is 'you are trying to access an environment variable that is not currently set within the environment where your python script is running' but I am not sure how it is in a different environment if I am setting it at run time.

    Is there a way to set the key value in my run statement in Windows? If not, how can I send my api key without hard coding it? thanx for your help

    Continue reading...

Compartilhe esta Página