#[Python] How to import dependencies like python-dotenv in a Dagger script?

1 messages · Page 1 of 1 (latest)

pastel wolf
#

I'm using Docker and in my Dockerfile, I've fired "RUN pip install python-dotenv". Going into the container, I can start my "main.py" script which read a .env file and read a variable from there. It's works.

In my Dagger function, I'm trying to do the same but every time, he told me no module name 'dotenv'. Is it possible that I need to inform Dagger that he need to install my dependencies?

I'm running Dagger like this: dagger call lint

My objective is to have a .env file with global variables like the type of project (python, php, javascript, ...) and a lot of variables like RUN_LINT=True, RUN_FORMAT=False, ... so I can start dagger call run-all in my CI and I'll start all functions (lint, format, fix, document, tests, ...) depending on my environment variables.

Thanks!

#

In case of interest, below some parts of my main.py script.

The error I got with dagger call lint is this one: File "/src/sha256:714ac049d3210e885ad29c312d570890c59b03a3daac9c77f052cec6cf2dbb53/.pipeline/src/src/main.py", line 6, in <module> from dotenv import load_dotenv ModuleNotFoundError: No module named 'dotenv'

from dotenv import load_dotenv

def read_environment(self, variable_name: str, default_value: Any) -> Any:
   load_dotenv(".env", override=True) 
   return os.environ.get(variable_name, default_value)

@function
async def lint(self) -> str:
   run_job = bool(self.read_environment("RUN_LINT", True))

   if not run_job:
      return None

   return await (
      dag.container()
      .from_("python:3.13-slim")
      .with_exec(["pip","install","pylint"])
      .with_mounted_directory("/app/src", self.source)
      .with_mounted_directory("/app/config", self.config)
      .with_workdir("/app/src")
      .with_exec(["pylint",".","--rcfile","/app/config/.pylintrc"])
      .stdout()
   )
trail gulch
pastel wolf
#

Thanks. I've tried without uv (didn't know that tool) but already tried using requirements.txt or by creating the pyproject.toml file by hand. Will take a look to uv. Thanks for the confirmation it's possible.