dvc icon indicating copy to clipboard operation
dvc copied to clipboard

Setting static environment variables for stage commands in `dvc.yaml`

Open JulianJvn opened this issue 5 months ago • 3 comments

Scenario: In a DVC stage, I want to run the main function of a Python module that's located in src/my_package/main.py in my DVC repository, i.e., python -m my_package.main. In order to make Python find it, the environment variable PYTHONPATH needs to be set to src when running this command.

Currently, this doesn't seem to be possible in an easy way. dvc.yaml only supports cmd and doesn't have an env key. dvc.stage.run already supports environment variables in theory via run_env, but they can't be set from outside. I would like a platform-independent way, i.e., no shell-specific syntax and no shell script wrapper, so that my stage runs on any platform.

#1416 is related, but is about parametrization, which I don't need. #2042 and #5332 have been closed in favor of #1416. This issue is explicitly about static, i.e., non-parametrized and non-parametrizing, values of environment variables.

I think DVC would already benefit a lot if static environment variables were supported via an env key in dvc.yaml (see #2042) and this would be a good starting point to add parametrization later (#1416).

JulianJvn avatar Aug 01 '25 06:08 JulianJvn

You can already add envvars in the cmd. What does env key provide?

stages:
  hello:
    cmd: PYTHONPATH= python main.py

skshetry avatar Aug 01 '25 07:08 skshetry

That's POSIX shell syntax and doesn't work on Windows.

The env key would provide a platform-independent way of setting environment variables. There are numerous examples of popular tools that provide such a key for this reason, for example VS Code's tasks.json, GitHub Actions workflows, etc.

JulianJvn avatar Aug 05 '25 09:08 JulianJvn

@JulianJvn, we discussed this within the team. While we agree it’s a good feature request that dvc should support, we’ll hold off until we see more interest from the community.

skshetry avatar Aug 19 '25 12:08 skshetry