If I understand your question correctly, just use importlib. In a nutshell, what in python you write like:
from package import module as alias_mod
in importlib it becomes:
alias_mod = importlib.import_module('module', 'package')
or, equivalentelly:
alias_mod = importlib.import_module('module.package')
for example:
from numpy import random as rm
in importlib:
rm = importlib.import_module('random', 'numpy')
Another interesting thing is this code proposed in this post, which allows you to import not only modules and packages but also directly functions and more:
def import_from(module, name):
module = __import__(module, fromlist=[name])
return getattr(module, name)
For your specific case, this code should work:
import importlib
n_conf = 3
for in range(1, n_conf)
conf = importlib.import_module('config_file.config_'+str(i))
# todo somethings with conf
However, if I can give you some advice I think the best thing for you is to build a json configuration file and read the file instead of importing modules. It's much more comfortable. For example in your case, you can create a config.json
file like this:
{
"config_1": {
"folder": "raw1",
'start_date': '2019-07-01'
},
"config_2": {
'folder': 'raw2',
'start_date': '2019-08-01'
},
"config_3": {
'folder': 'raw3',
'start_date': '2019-09-01'
}
}
Read the json file as follows:
import json
with open('config.json') as json_data_file:
conf = json.load(json_data_file)
Now you have in memory a simple python dictionary with the configuration settings that interest you:
conf['config_1']
# output: {'folder': 'raw1', 'start_date': '2019-07-01'}