# Buggy code
def clear_pymathics_modules(self):
    from mathics.builtin import builtins, builtins_by_module

    # Remove all modules that are not in mathics
    # print("cleaning pymathics modules")
    for key in list(builtins_by_module.keys()):
        if key[:8] != "mathics.":
            print("removing module ", key, " not in mathics.")
            del builtins_by_module[key]
    # print("reloading symbols from current builtins.")
    for s in self.pymathics:
        if s in self.builtin:
            # If there was a true built-in definition for the symbol, restore it, else, remove he symbol.
            if self.pymathics[s]:
                self.builtin[s] = self.pymathics[s]
                builtins[s] = None
                for key, val in builtins_by_module:
                    for simb in val:
                        if simb.get_name() == s:
                            builtins[s] = simb
                            break
                    if builtins[s] is not None:
                        break
                if builtins[s] is None:
                    builtins.__delitem__(s)
            else:
                self.builtin.__delitem__(s)
                builtins.__delitem__(s)
    self.pymathics = {}
    # print("everything is clean")
    return None


# Traceback (ValueError)
Mathics 1.1.dev0
on CPython 3.6.9 (default, Jul 17 2020, 12:50:27)
using SymPy 1.6.2, mpmath 1.1.0

Copyright (C) 2011-2020 The Mathics Team.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
See the documentation for the full license.

Quit by pressing CONTROL-D

In[1]:= a = 3
Out[1]= 3

In[2]:= Quit[]
removing module  pymathics.natlang  not in mathics.
In[1]:= LoadModule["pymathics.natlang"]
Out[1]= pymathics.natlang

In[2]:= Quit[]
removing module  pymathics.natlang  not in mathics.
Traceback (most recent call last):
File "/home/pablo/.local/bin/mathics", line 315, in <module>
main()
File "/home/pablo/.local/bin/mathics", line 298, in main
result = evaluation.evaluate(query, timeout=settings.TIMEOUT)
File "/home/pablo/Documents/Mathics/mathics/core/evaluation.py", line 286, in evaluate
result = run_with_timeout_and_stack(evaluate, timeout)
File "/home/pablo/Documents/Mathics/mathics/core/evaluation.py", line 95, in run_with_timeout_and_stack
return request()
File "/home/pablo/Documents/Mathics/mathics/core/evaluation.py", line 264, in evaluate
result = query.evaluate(self)
File "/home/pablo/Documents/Mathics/mathics/core/expression.py", line 853, in evaluate
expr, reevaluate = expr.evaluate_next(evaluation)
File "/home/pablo/Documents/Mathics/mathics/core/expression.py", line 975, in evaluate_next
result = rule.apply(new, evaluation, fully=False)
File "/home/pablo/Documents/Mathics/mathics/core/rules.py", line 63, in apply
yield_match, expression, {}, evaluation, fully=fully)
File "/home/pablo/Documents/Mathics/mathics/core/pattern.py", line 203, in match
yield_head, expression.get_head(), vars, evaluation)
File "/home/pablo/Documents/Mathics/mathics/core/pattern.py", line 132, in match
yield_func(vars, None)
File "/home/pablo/Documents/Mathics/mathics/core/pattern.py", line 198, in yield_head
yield_func(head_vars, None)
File "/home/pablo/Documents/Mathics/mathics/core/rules.py", line 39, in yield_match
new_expression = self.do_replace(expression, vars, options, evaluation)
File "/home/pablo/Documents/Mathics/mathics/core/rules.py", line 124, in do_replace
return self.function(evaluation=evaluation, **vars_noctx)
File "/home/pablo/Documents/Mathics/mathics/builtin/assignment.py", line 2205, in apply
evaluation.definitions.clear_pymathics_modules()
File "/home/pablo/Documents/Mathics/mathics/core/definitions.py", line 157, in clear_pymathics_modules
for key, val in builtins_by_module:
ValueError: too many values to unpack (expected 2)

# Fixed code
def clear_pymathics_modules(self):
    from mathics.builtin import builtins, builtins_by_module

    # Remove all modules that are not in mathics
    # print("cleaning pymathics modules")
    for key in list(builtins_by_module.keys()):
        if not key.startswith("mathics."):
            print(f'removing module "{key}" not in mathics.')
            del builtins_by_module[key]
    # print("reloading symbols from current builtins.")
    for s in self.pymathics:
        if s in self.builtin:
            # If there was a true built-in definition for the symbol, restore it, else, remove he symbol.
            if self.pymathics[s]:
                self.builtin[s] = self.pymathics[s]
                builtins[s] = None
                for key, val in builtins_by_module.items():
                    for simb in val:
                        if simb.get_name() == s:
                            builtins[s] = simb
                            break
                    if builtins[s] is not None:
                        break
                if builtins[s] is None:
                    builtins.__delitem__(s)
            else:
                self.builtin.__delitem__(s)
                builtins.__delitem__(s)
    self.pymathics = {}
    # print("everything is clean")
    return None