Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefixed Method Does Not Clear self._prefix On Error #78

rcuza opened this issue May 23, 2019 · 1 comment


Copy link

commented May 23, 2019

version: 4.1.3


When using the following code, the private variable self._prefix is not cleared on errors:

with env.prefixed(PREFIX):

If do_something() raises an error, then session attempts to run with env.prefixed() again it will behave like it is a nested with. This can happen in an interactive shell or if exceptions are caught.


You can do the following to reproduce.

$ export PREFIX_PNG="path/to/my.png"
$ ipython
/usr/local/Cellar/ipython/7.5.0/libexec/lib/python3.7/site-packages/IPython/core/ UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.7.3 (default, Mar 27 2019, 09:23:15)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from environs import Env

In [2]: env = Env()

In [3]: with env.prefixed("PREFIX_"):
   ...:     print(env.str("PNG"))

In [4]: with env.prefixed("PREFIX_"):
   ...:     raise
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-6c7273e50e26> in <module>
      1 with env.prefixed("PREFIX_"):
----> 2     raise

RuntimeError: No active exception to reraise

In [5]: with env.prefixed("PREFIX_"):
   ...:     print(env.str("PNG"))
EnvError                                  Traceback (most recent call last)
<ipython-input-5-60d974fcda59> in <module>
      1 with env.prefixed("PREFIX_"):
----> 2     print(env.str("PNG"))

~/dev/virtualenvs/tmp-da5b0d36b01c294/lib/python3.7/site-packages/ in method(self, name, default, subcast, **kwargs)
     51         if raw_value is ma.missing and field.missing is ma.missing:
     52             raise EnvError(
---> 53                 'Environment variable "{}" not set'.format(proxied_key or parsed_key)
     54             )
     55         if raw_value or raw_value == "":

EnvError: Environment variable "PREFIX_PREFIX_PNG" not set

This comment has been minimized.

Copy link
Contributor Author

commented May 23, 2019

The workaround for this issue is to set env._prefix = None if you are using env = Env().

@sloria sloria closed this Jun 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.