Search code examples
pythonvisual-studio-codeenvironment-variablespython-venvopenai-api

Setting Environment variables in .venv


I am using .venv to create a virtual environment to use for a project. Within this project, I need to access the OpenAI GPT3 environment. For this, i have an API key, but I want to store it as an environment variable. Because I use windows and VS-Code to work on my project, a lot of attempts have already failed. Does anyone have an idea how to solve this?


Solution

  • You can modify the system environment in the settings.json file like this:

      "terminal.integrated.env.windows": {
        "OPENAI_API_KEY": "xxx"
      },
    

    And add this in the .env file:

    OPENAI_API_KEY=xxx
    

    When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting will not have an effect on module look-up.

    When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.

    You can refer to the official docs for more detail.