Hi, I wish to configure settings using env variabl...
# general
h
Hi, I wish to configure settings using env variable in like django like so: settings.py
Copy code
from decouple import config
DEBUG = config('DEBUG') == 'True'
I get this exception:
Copy code
decouple.UndefinedValueError: DEBUG not found. Declare it as envvar or define a default value.
I have added .env file as prescribed by this library with content
Copy code
DEBUG=True
e
Pants is hermetic by default and, towards that end, filters out almost all environment variables by default. You'll need to tell Pants to expose
DEBUG
. I'm going to assume you hit this error in tests, but if not and its during run? You'll need to provide your Pants version to debug further. Continuing with the tests assumption: If you just need the env var for tests in some
python_tests
targets, you can use: https://www.pantsbuild.org/docs/reference-python_tests#codeextra_env_varscode If you need the env var exposed for all tests use: https://www.pantsbuild.org/docs/reference-test#section-extra-env-vars
h
Yeah pants version is 2.12.0 And I not using for test.. but general env var which includes both settings and secrets.
I have a .env file in the proj root, you can refer the image, which contain vars each in separate line, also mentioned above.
for more context the library that I am using to manage env vars is python-decouple .
e
What Pants goal gives you the failure? Is it
run
?
h
./pants run projects/appsflyer/:manage -- runserver --noreload
e
Ok, run - thanks. This may be fixed in newer Pants versions - looking...
I do not repro on the 2.12.x branch:
Copy code
$ cat BUILD.foo 
python_source(name="foo", source="foo.py")
pex_binary(
    name="foo-bin",
    entry_point="foo"
)
$ cat foo.py 
import os


print(os.environ.get("DEBUG", "<>"))

$ ./pants run :foo-bin
<>
$ DEBUG=bar ./pants run :foo-bin
bar
Let me try exactly 2.12.0. 2.12.x is https://pypi.org/project/pantsbuild.pants/2.12.1rc3/
Oh, wait, your problem is not env vars in general but the
.env
machanism for setting them in particular - I think.
In other words, .env needs to be present side-by-side with the code you run for this whole thing to work - is that right?
So, @handsome-sunset-98068 what is responsible for reading the
.env
file, is it a python library or something like the
direnv
tool?
h
e
Ok - just a sec...
h
Yeah mechanism for a .env that cuz the following command works:
Copy code
./pants --subprocess-environment-env-vars="['DEBUG=True']" run projects/appsflyer/:manage -- runserver --noreload
e
This works for me on 2.12.x. The key was adding a
resource
target for the
.env
file and manually adding that as a requirement of the
pex_binary
target: Setup (the resolve complexity is ignorable but needed to get a self-contained demo working sanely):
Copy code
$ cat BUILD.foo
python_requirement(
    name="decouple",
    requirements=[
        "python-decouple",
    ],
    modules=["decouple"],
    resolve="foo",
)
python_source(
    name="foo",
    source="foo.py",
    resolve="foo",
)
resource(
    name=".env",
    source=".env",
)
pex_binary(
    name="foo-bin",
    entry_point="foo",
    dependencies=[
        ":.env",
    ],
    resolve="foo",
)
$ cat .env 
DEBUG=bar
$ cat foo.py 
import os
from decouple import config

print(f"CWD: {os.getcwd()}")
print(config("DEBUG", "<>"))
Pants run:
Copy code
$ ./pants --python-resolves="+{'foo':'foo.lock'}" run :foo-bin
CWD: /home/jsirois/dev/pantsbuild/pants
bar
h
I got it work for now. By adding a new python source (not a file source) and adding that as a dependency for manage target.
Copy code
python_sources()

python_source(
    name="env",
    source=".env"
)

pex_binary(
    name="manage",
    entry_point="manage.py",
    dependencies=[
        "./appsflyer:appsflyer",
        ":env"
    ],
    restartable=True
)
Though I am not very sure how this worked, but it did.
e
Yeah, that's why I used
resource
- `python_source`works too. There is currently oddness where PEXes built by Pants cannot depend on
file
targets even though Pex the tool accepts plain old files just fine. So you have to lie and call the file a
resource
or
python_source
.
h
Yeah resource worked too. Thanks John.