#[SOLVED] How to read a configuration file in my Python function?

1 messages · Page 1 of 1 (latest)

gentle pulsar
#

First of all, my best wishes for 2025 and all the best for the future of Dagger.io.

I would like to be able to read a .env file from my Python Dagger function but I don't find how to achieve this.

To make things easy to illustrate, I would be able to do something like this:

@function
async def test(self) -> str:
    load_dotenv(".env", override=True) 
    if os.environ.get('RUN_LINT', True):
        return "Run it"
    return "Don't run it"

By running it (dagger call test --verbose ), it didn't work because the .env file is not retrieved.

By replacing my function body with just return os.getcwd(); I get /scratch as the current folder.

My problem, I suppose; is because Dagger didn't retrieve my .env file while that file well exists in my current folder while running the dagger call instruction.

Did you've any tips / tutorials here?

Thanks!

gentle pulsar
#

Got it.

By reading the cookbook once more; I've see this doc: https://docs.dagger.io/cookbook#copy-a-file-to-the-dagger-module-runtime-container-for-custom-processing

So, for my need:

from dotenv import load_dotenv

# [...]

@function
async def test(self, source: File) -> str:
    await source.export(".config/.pipeline")

    load_dotenv(".config/.pipeline", override=True) 

    print(f"Application title = {os.environ.get("APP_TITLE","Unknown title")}")
    print(f"Project type      = {os.environ.get("PROJECT_TYPE","Unknown type")}")
    print(f"Run lint feature? = {os.environ.get("RUN_LINT","True").upper()}")

    if os.environ.get("RUN_LINT", "TRUE").upper() == "TRUE":
        return "Run it"

    return "Don't run it"

And I can call it like this: dagger call test --source=../.config/.env -v

Here my .env file

PROJECT_TYPE=Python
RUN_LINT=true
APP_TITLE="This is the name of my application"

So, now, I can foresee as many RUN_xxx boolean flags I wil have features (lint, format, ruff, ... and many more)

If you've any tips / suggestions, please, don't hesitate 😉

Filesystem