I want to use a configuration file in the middle of my main Python script. In this file, there are variables already initialized in the main script.
Main script:
# -*- coding: utf-8 -*-
import imp
import optparse
from time import strftime, strptime
from datetime import date, timedelta
[...]
date_mois = date_de_ref.strftime("%m")
date_annee = date_de_ref.strftime("%Y")
date_moins2j = (date_de_ref - timedelta(days=2)).strftime("%d/%m/%Y")
date_moins7j = (date_de_ref - timedelta(days=7)).strftime("%d/%m/%Y")
imp.load_source("conf_file", "part_1.config")
import conf_file
for task in conf_file.list_tasks:
print task[1]
print "The end!"
"part_1.config":
list_tasks = [
["B", "BAT001", "Info batch 1"],
["B", "BAT002 DEBUT=21/01/2015", "Info batch 2"],
["B", "BAT003 FIN=" + date_moins2j, "Info batch 3"],
]
If I executed it like this, I got:
Traceback (most recent call last):
File "ordo_na.py", line 48, in <module>
imp.load_source("conf_file", "part_1.config")
File "part_1.config", line 9, in <module>
["B", "BAT003 FIN=" + date_moins2j, ""],
NameError: name 'date_moins2j' is not defined
I tried it with:
import ordo_na
list_tasks = [
["B", "BAT001", ""],
["B", "BAT002 DEBUT=21/13/12", ""],
["B", "BAT003 FIN=" + ordo_na.date_moins2j, ""],
]
The main script seems to be executed twice this way.
How can I use variable from the main script in my configuration file ?
I want to avoid the import ordo_na line if possible and keep the configuration file the cleansest possible.
I use Python 2.6.6 by the way (I can't upgrade to a newer version).
Thanks!