pihug12
pihug12

Reputation: 317

Use variables of main Python script in a config file

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!

Upvotes: 0

Views: 2026

Answers (1)

jedwards
jedwards

Reputation: 30210

Modules are separate, and you can't easily access the variables of the importing module from the imported module (what I think you're trying to do).

Consider:

part_1.config

def get_tasks(date_moins2j):

    list_tasks = [
        ["B", "BAT001", ""],
        ["B", "BAT002 DEBUT=21/13/12", ""],
        ["B", "BAT003 FIN=" + date_moins2j, ""],
    ]

    return list_tasks

Main Script

import imp

imp.load_source("conf_file", "part_1.config")
import conf_file

print( conf_file.get_tasks("foo") )

Alternatively, you could just rename part_1.config to something like my_config.py, then simply use import:

Main Script

import my_config as conf_file

pprint.pprint( conf_file.get_tasks("foo") )

If you didn't want to deal with explicitly passing arguments, you could always pass the importing module's (main script) global dictionary, and access that, then your files would look like:

part_1.config

def get_tasks(g):

    list_tasks = [
        ["B", "BAT001", ""],
        ["B", "BAT002 DEBUT=21/13/12", ""],
        ["B", "BAT003 FIN=" + g['date_moins2j'], ""],
    ]

    return list_tasks

Main Script

import imp

imp.load_source("conf_file", "part_1.config")
import conf_file

date_moins2j = "foo"

pprint( conf_file.get_tasks(globals()) )

Upvotes: 2

Related Questions