I am migrating scripts from Python2.7 to Python3.6. I have a function that opens a python file and imports the variables. Works fine in 2.7 but in 3.6 I get an error and I can't figure out what the issue is.
Code:
def _load_rules(old_hash=''):
# Determine if the rules file has changed, and if so reload it
rules = open(rules_file, 'r').read()
new_hash = md5(b'rules').hexdigest()
if new_hash != old_hash:
exec(rules)
# C{ignore_rules} and C{notify_rules} come from the script execution
return (new_hash, ignore_rules, notify_rules)
return (old_hash, None, None)
This function checks the rules.py
file to see if any changes were made and if not it runs exec(rules)
which opens the rules.py
file and should take in the variables ignore_rules
and notify_rules
, which are dictionaries of various "rules" for the purposes of these scripts. The rules.py
file is formated like so.
import datetime
import re
notify_rules = \
[{'event': 'failure',
'access_point': None,
'expression': re.compile(r'some_regex'),
'addresses': ['some_email@email.com']},
{'event': 'failure',
'access_point': 'access_point_',
'addresses': ['some_email@email.com']},
ignore_rules = \
[{'access_point': 'something'},
{'access_point': 'something'},
{'access_point': 'something'},
{'access_point': 'something'},]
Traceback:
File "/app/eam/gaic/ai_mon/monitoring.py", line 174, in _load_rules
return (new_hash, notify_rules, ignore_rules)
builtins.NameError: name 'notify_rules' is not defined