python-decouple icon indicating copy to clipboard operation
python-decouple copied to clipboard

Jupyter notebooks break decouple's default config

Open tbrodbeck opened this issue 3 years ago • 3 comments

Hi. For some reason jupyter notebooks of vscode the behaviour of decouple recently break decouple default config for me:

from decouple import config
config('SECRET')
---------------------------------------------------------------------------
/var/folders/hv/j2pkf20x1qb1c0sc23yd58y40000gn/T/ipykernel_9171/3485114881.py in <module>
----> 1 decouple.config('SECRET')

/usr/local/lib/python3.9/site-packages/decouple.py in __call__(self, *args, **kwargs)
    197             self._load(self.search_path or self._caller_path())
    198 
--> 199         return self.config(*args, **kwargs)
    200 
    201 

/usr/local/lib/python3.9/site-packages/decouple.py in __call__(self, *args, **kwargs)
     81         Convenient shortcut to get.
     82         """
---> 83         return self.get(*args, **kwargs)
     84 
     85 

/usr/local/lib/python3.9/site-packages/decouple.py in get(self, option, default, cast)
     66         else:
     67             if isinstance(default, Undefined):
---> 68                 raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
     69 
     70             value = default

UndefinedValueError: SECRET not found. Declare it as envvar or define a default value.

I was able to debug the reason for that: I do not fully understand the purpose of the function, but AutoConfig._caller_path returns in a jupyter notebook /var/folders/hv/j2pkf20x1qb1c0sc23yd58y40000gn/T/ipykernel_9888. This causes my .env file in the same repository as the notebook to not be found.

Instead in the python REPL this function returns an string with a space ' ', that works.

Thus, in a notebook I can make decouple working again by setting config = decouple.AutoConfig(' ')

Versions: Jupyter Server 1.9.0 Python 3.9.6 python-decouple==3.4

tbrodbeck avatar Jul 13 '21 20:07 tbrodbeck

I have encountered the same problem and your suggestion did fix it. The functionality you expect is the env of the root folder where you started jupyter notebook to be picked up.

darkraisisi avatar Oct 07 '21 11:10 darkraisisi

Same issue here.

with : python-decouple==3.4 jupyter_server==1.11.1 jupyterlab==3.2.0 python 3.8.12

I tried with different versions of python-decouple (3.4 and 3.5) and Jupyter (2 and 3) that didn't change the behavior.

your fix is working though. Thanks!

jlequeux avatar Oct 19 '21 08:10 jlequeux

@tbrodbeck this is not a bug. Your fix is the proper way to use decouple with Jupyter. Maybe you can provide a PR to the README to instruct people to avoid this mistake.

henriquebastos avatar Feb 02 '22 19:02 henriquebastos

Is there any update on this issue? I've tried the workaround on Google Collab but it did not work. Am I not understanding the message correctly? from decouple import config config = decouple.AutoConfig(' ') config('SECRET')

LY081121 avatar Nov 14 '22 16:11 LY081121

Decouple's AutoConfig searches .env starting from the current working directory all the way up to your driver's root. Jupyter and Colab are servers, so the current working directory for its processes will not be the directory where your ipnb files are.

That is the problem.

You will have to tell decouple to load the right file explicitly:

from decouple import Config, RepositoryEnv
config = Config(RepositoryEnv("/Users/me/project/where/my/env/file/is/.env")

henriquebastos avatar Feb 15 '23 23:02 henriquebastos