handsome-sunset-98068
08/24/2022, 12:11 PMfrom decouple import config
DEBUG = config('DEBUG') == 'True'
I get this exception:
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
DEBUG=Trueenough-analyst-54434
08/24/2022, 12:31 PMDEBUG.
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-varshandsome-sunset-98068
08/24/2022, 12:33 PMhandsome-sunset-98068
08/24/2022, 12:36 PMhandsome-sunset-98068
08/24/2022, 12:39 PMenough-analyst-54434
08/24/2022, 12:45 PMrun?handsome-sunset-98068
08/24/2022, 12:46 PMenough-analyst-54434
08/24/2022, 12:46 PMenough-analyst-54434
08/24/2022, 1:18 PM$ 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/enough-analyst-54434
08/24/2022, 1:18 PM.env machanism for setting them in particular - I think.enough-analyst-54434
08/24/2022, 1:19 PMenough-analyst-54434
08/24/2022, 1:23 PM.env file, is it a python library or something like the direnv tool?enough-analyst-54434
08/24/2022, 1:24 PMhandsome-sunset-98068
08/24/2022, 1:24 PMenough-analyst-54434
08/24/2022, 1:25 PMhandsome-sunset-98068
08/24/2022, 1:31 PM./pants --subprocess-environment-env-vars="['DEBUG=True']" run projects/appsflyer/:manage -- runserver --noreloadenough-analyst-54434
08/24/2022, 2:08 PMresource 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):
$ 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:
$ ./pants --python-resolves="+{'foo':'foo.lock'}" run :foo-bin
CWD: /home/jsirois/dev/pantsbuild/pants
barhandsome-sunset-98068
08/24/2022, 2:13 PMpython_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.enough-analyst-54434
08/24/2022, 2:14 PMresource - `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.handsome-sunset-98068
08/24/2022, 2:24 PM