I use pants to run django and I need to set enviro...
# general
h
I use pants to run django and I need to set environment variables when building the pex binary. PEX is able to use environment variables: https://github.com/pantsbuild/pex/issues/987. But how can I achieve this in a pants bex_binary target. I tried using 'environment' and local_environment(name="local-django-manage", subprocess_environment_env_vars=[...]), but this does not work. Is subprocess_environment_env_vars the correct field for this? Testing works fine because I can specify extra_env_vars but this does not work for running the pex file.
It is strange but running something like
./pants run django/manage.py
works but
pants run django:manage
where manage this target does not work and results in a SECRET_KEY not found error. How does the first command find my secret key (its not set in my bash environment but only in
env_vars_run_django()
)
Copy code
pex_binary(
    name="manage",
    entry_point="manage.py",
    restartable=True,
    environment="linux_devcontainer",
)
local_environment(name="local-django-manage", subprocess_environment_env_vars=env_vars_run_django())
e
For Pex #987 support, you need to wait for Pants 2.16 or use it now at 2.16.0.dev5+. Latest 2.16.x is: https://pypi.org/project/pantsbuild.pants/2.16.0.dev6/
h
Thanks! So the PR you linked adds functionality to package env vars inside the pex binary. This is something I don't need. I want to be able to only make the variables available when running the binary. Is there a way to define env variables for the
run
goal so that
pants run django:manage
builds the pex binary and then passes the environment variables not while building but during runtime. I thought
environment="linux_devcontainer"
would do this but it seems to do nothing.
e
Ok, well https://github.com/pantsbuild/pex/issues/987 , which you linked, is what https://github.com/pantsbuild/pants/pull/17905 enables in 2.16.x I'll let someone else look at the rest of your question.
Re-reading your follow up, env vars should propagate through
pants run
with no extra effort (targets or config) on your part.
h
Yes, but when they are not set in my env how can I pass env vars to pants run ...some pex binary target? I want to define the env vars in a BUILD file.
e
You do not want either of: 1.
FOO=bar BAZ=spam ./pants run ...
2. Define a pex_binary target for normal use + define another with the
FOO
and
BAZ
env vars sealed in You want instead ... ?
I'm sorry @happy-family-315 I'm not seeing what 3rd option you want. The only other way to configure Pants generally is via config options (as opposed to BUILD targets); so that would mean a
run
option to pass env vars. That's too broad a scope though since it sounds like you want to pass the env vars only to a certain
run
target. Options 1 & 2 cover that case.