Hello. I have a problem with Pants in 2.13.0 when ...
# general
f
Hello. I have a problem with Pants in 2.13.0 when I try to use
.env
file and get the values with
python-dotenv
. Here is a quick example:
Copy code
import os
from dotenv import load_dotenv

load_dotenv()

if __name__ == "__main__":
    print(os.environ["ENVVAR"])
I have a
.env
file with
ENVVAR=test
. When I
run
using Pants 2.12.0 it prints
"test"
without problem but I get this in Pants 2.13.0:
Copy code
File "/tmp/pants-sandbox-9ZiWSR/tasks/helloworld/main.py", line 7, in <module>
    print(os.environ["ENVVAR"])
  File "/home/soheil/.pyenv/versions/3.10.6/lib/python3.10/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'ENVVAR'
Any recommendation?
b
How are you expecting to see the file in the sandbox? Do you have an associated target?
Oh yeah actually I think I know what's going on. Previously the
run
sandbox was under the build root
.pants.d
but now it's under
/tmp
. You should declare a
file
target for your
.env
file and make sure the right targets depend on it, so it shows up in the sandbox
f
Yes, and no. I have a
.env
file in the root folder and a
tasks/
folder in which I have several Python project which are configured with environment variables. I deploy the PEX files in Amazon ECS in container images, and the environment variables is automatically set in the container in production. But during development I want to rely on an
.env
file.
Alright, I will try. Thanks!
b
We could also consider supporting .env as a plug-in. If you're feeling really ambitious
f
Hi @bitter-ability-32190. I tried you solution using a
file
target and declaring a dependency to have the
.env
file in the
run
sandbox, and it worked. I have a problem now with CI/CD. When I try to run Pants in GitHub Actions (so without an
.env
file), it fails because there isn't an
.env
file available (the environment variables are already set up by GitHub Action). Do you have a solution for that?
b
Can you just
touch
one?
c
Did you find a way to use .env files you were happy with?
f
@curved-manchester-66006 Hello, yes I use direnv.