Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Context manager for handling temporary variables in Jupyter Notebook, IPython, etc.

Project Description


A context manager for handling temporary variables in Jupyter Notebook, IPython, etc.

A frustrating aspect of working with Jupyter notebooks is debugging a worksheet for half an hour and discovering a carried-over variable name was hanging around in the notebook namespace and causing the misbehavior, or opening a notebook that “worked fine” the last time it was used because of random variables lingering in the namespace. The TempVars context manager avoids these pitfalls by clearing selected identifiers from the namespace for the duration of the with suite, then restoring them afterwards (or not, if desired). Further, any variables created within the managed context that match the criteria passed to TempVars are removed from the namespace upon exiting, ensuring these values do not spuriously contribute to following code. For convenience, all variables that were removed from the namespace at both entry and exit are stored with their values for later reference (see example code below).

NOTE: Due to the way Python handles non-global variable scopes, TempVars can only be used at the global scope. Any attempt to use TempVars in non-global contexts will result in a RuntimeError. Viable use-cases include Jupyter notebooks, the IPython and basic Python REPLs, and the outermost scope of executed and imported modules. Preliminary testing indicates it also works with cauldron-notebook, though it may be less helpful there due to the step-local scoping paradigm used (shared values must be passed around via cauldron.shared).

NOTE ALSO that tempvars is Python 3 only.

After installing with pip install tempvars, import as:

>>> from tempvars import TempVars

Example usage:

>>> t_var1 = 5
>>> t_var2 = 7
>>> x = 15
>>> y = 20
>>> with TempVars(names=['x']) as tv1:
...     with TempVars(starts=['t_'], restore=False) as tv2:
...         print('x' in dir())
...         print('t_var1' in dir())
...         print('t_var2' in dir())
...         print(y)
...         print(tv1.stored_nsvars)
...         print(sorted(tv2.stored_nsvars.keys()))
...         print(tv2.stored_nsvars['t_var1'])
...         print(tv2.stored_nsvars['t_var2'])
...         x = -3
...         t_var3 = -7
...         print((x, t_var3, y))
{'x': 15}
['t_var1', 't_var2']
(-3, -7, 20)
>>> print((x, y))
(15, 20)
>>> print('t_var1' in dir())
>>> print('t_var2' in dir())
>>> print('t_var3' in dir())
>>> print(tv1.retained_tempvars)
{'x': -3}
>>> print(tv2.retained_tempvars)
{'t_var3': -7}


Branches named with the prefix v. are volatile. The history there may be rewritten dramatically, without warning.

Available on PyPI: pip install tempvars.

Source on GitHub. Bug reports and feature requests are welcomed at the Issues page there. If you like the idea of an enhancement already in the Issues list, please comment to say so; it’ll help with prioritization.

Full documentation at Read the Docs.

Copyright (c) Brian Skinn 2017

License: The MIT License. See LICENSE.txt for full license terms.

Release History

This version
History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(9.0 kB) Copy SHA256 Hash SHA256
Wheel py3 Oct 19, 2017
(7.8 kB) Copy SHA256 Hash SHA256
Source None Oct 19, 2017

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting