[Python] How to automatically setup a Conda virtual environment with environment variables...

Discussão em 'Python' iniciado por Stack, Novembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to automate to set up environment variables for a Conda environment from .env files during environment creation on a Linux system. I want to load environment variables from .env-files ".env.secrets1" and ".env.secrets2" into the environment "dummy" whenever the environment is activated. However, the activation and deactivation scripts are not being executed properly when I activate the environment. The environment variables are not getting set as expected. The script file "post-link.sh" does not create the expected folders and files for the virtual environment.

    Here is my project structure:

    PathToProject/dummy
    ├── dummy.yml
    ├── post-link.sh
    ├── .env.secrets1
    └── .env.secrets2


    dummy.yml (the Conda environment configuration YAML):

    name: dummy

    channels:
    - defaults

    dependencies:
    - python=3.12
    - pip
    - python-dotenv # For loading .env files in Python scripts

    files:
    - post-link.sh # Reference to the post-link script to be copied into the environment


    post-link.sh (the script to be executed after environment creation):

    #!/bin/bash

    # Get the environment's prefix path (this is where the environment is installed)
    CONDA_PREFIX=$(conda info --envs | grep '*' | awk '{print $3}')

    # Create the activate.d and deactivate.d directories if they don't exist
    mkdir -p $CONDA_PREFIX/etc/conda/activate.d
    mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d

    # Create the activation script to load environment variables from the .env files
    cat > $CONDA_PREFIX/etc/conda/activate.d/load_env_vars.sh <<EOL
    #!/bin/bash
    # Set the base directory for .env files
    export ENV_PATH="PathToProject/dummy" # Path where .env files are located
    # Load environment variables from .env.secrets1 and .env.secrets2
    python -c "
    from dotenv import load_dotenv
    load_dotenv('$ENV_PATH/.env.secrets1')
    load_dotenv('$ENV_PATH/.env.secrets2')
    "
    EOL

    # Make the activation script executable
    chmod +x $CONDA_PREFIX/etc/conda/activate.d/load_env_vars.sh

    # Optionally create a deactivation script to unset environment variables dynamically
    cat > $CONDA_PREFIX/etc/conda/deactivate.d/unset_env_vars.sh <<EOL
    #!/bin/bash
    # Dynamically unset all environment variables loaded by dotenv
    unset \$(python -c "
    from dotenv import dotenv_values
    config = dotenv_values('$ENV_PATH/.env.secrets1')
    config.update(dotenv_values('$ENV_PATH/.env.secrets2'))
    for key in config.keys():
    print(key)
    ")
    EOL

    # Make the deactivation script executable
    chmod +x $CONDA_PREFIX/etc/conda/deactivate.d/unset_env_vars.sh


    .env.secrets1:

    API_KEY_1=ABC
    API_KEY_2=XYZ


    .env.secrets2:

    PASS=1234

    Continue reading...

Compartilhe esta Página