Cloud Pak for Data

 View Only

Setting environment variables in notebook

  • 1.  Setting environment variables in notebook

    Posted Thu June 13, 2024 09:02 AM

    Hello, I'm wondering if there is a way to set environment variables in the notebooks? For example I'm using GPT-4 via OpenAI and in order to initialize the client I write


    import openai


    client = openai.OpenAI(api_key='MY_API_KEY')

    Locally I'd just create an .env file and place the key there, then fetch the key using os.environ.get("api key").

    The same thing goes for data connection passwords and username when reading data via sqlalchemy. Here is how I use sqlalchemy:


    from sqlalchemy import create_engine

    # Create a connection to the database
    engine = create_engine('mssql+pymssql://username:password@SQL2019DB-MKT.redcatsnordic.ad/ImageRecognitionProd')

    data = pd.read_sql('SELECT * FROM dbo.[IMAGE_DATA]', con=engine, index_col=None)

    But I'd like to put 'username' and 'password' as environment variable instead of pasting them into the code. Does anyone have a working solution for this, similar to the method one would use in a local setting (VScode) where a simple .env is created for each project and then fetched using os.getenv("SECRET")?



    ------------------------------
    Lirim Kadriu
    ------------------------------