A textbook suggests I should be able to do this:
d = {}
exec("C://Users//Dave//Desktop//Bot//bot_config_data.py", globals(), d)
File "<string>", line 1 C://Users//Dave//Desktop//Bot//bot_config_data.py
^
SyntaxError: invalid syntax
I can do this:
d = {}
exec('from bot_config_data import price_data', globals(), d)
But, I would like to do the former.
I'm trying to write a method which overrides config data from various files.
Am I completely off base here?
Update The book was quite misleading. It posted part of the code, the result of the complete code block, and then gave the remainder. As I was referencing, rather than working through it cover to cover, I tripped over myself.
This is the code I now have:
data = {}
file = 'C:\\Users\\Dave\\Desktop\\Bot\\bot_config_data.py'
with open(file) as f:
code = compile(f.read(), file, "exec")
exec(code, globals(), data)
price_data = data["price_data"]
Update2 Using Mad Physicist's answer below, my code would be:
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
filepath = 'C:\\Users\\Dave\\Desktop\\Bot\\bot_config_data.py'
module_data = os.path.basename(filepath)
spec = spec_from_loader(module_data, SourceFileLoader(module_data, filepath))
bot_config_data = module_from_spec(spec)
spec.loader.exec_module(bot_config_data)
price_data = bot_config_data.price_data