[Python] openai.api_key = os.getenv("OPENAI_API_KEY") is not working in Jupyter

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am using a Mac. I am programming in Python using a Jupyter notebook (version 4.0.15) loaded from Anaconda Navigator 2.6.2. I want to load my OpenAI key from the environment. This is my code:

    import os
    import openai
    openai.api_key = os.getenv("OPENAI_API_KEY")
    print(os.getenv("OPENAI_API_KEY"))


    The above code returns "None"

    In terminal it says .zsh, not Bash at the top of the terminal window. (I think I had a message saying zsh was better than bash which was maybe deprecated, so I ran some code to change it to zsh.)

    I believe I have correctly got my openai key into the Terminal environment because when I type: echo $OPENAI_API_KEY, it correctly returns my openai api key starting with "sk-"....

    I have no problem making everything work if I hard-code the API key into my program. I have restarted my Mac many times, but no luck.

    I have looked at many of the answers here, but I still can't get this working. I couldn't find an answer relative to my specific setup. I just want my API code to be passed to the program from the environment.

    Continue reading...

Compartilhe esta Página