1

I have an IronPython application that does processing of records for exclusion rules. Essentially, I have a directory structure as such:

Lib\

Lib\DBCL\General.py

Lib\Exclusions\script.py

My program creates an instance of the IronPython runtime:

Dictionary<String, Object> oDic = new Dictionary<String, Object>();
oDic.Add("Debug", true);
iEngine = Python.CreateEngine(oDic);
iScope = iEngine.CreateScope(new Dictionary<String, Object> { { "sql", iSql } });
iScriptSource = aScript;
iSource = iEngine.CreateScriptSourceFromFile(iScriptSource);

iSql is a C# class for SQL interaction (just for the record). iScriptSource, in this case, is the python script: "Lib\Exclusions\script.py"

Now, inside my IronPython script, I import the DBCL folder and the General file:

import DBCL
from DBCL import General

So, I execute script.py:

iSource.Execute(iScope); // Edited 1/10/2014 to show I am passing the scope in.

Inside my script.py file, I make a call to General.py to perform a lookup in SQL (remember, I have added a variable called "sql" via iEngine.CreateScope()).

However, IronPython says that that global variable doesn't exist. But if I make the call in script.py, it exists as a global variable. How can I make my variables globally available to all scripts that I import?

If there is any confusion or if this question is poorly formed, please let me know and I'll try and fix it. I feel there's something simple I'm missing, but right now it's temporarily solved by simply passing "sql" around via arguments. I'd like to avoid that if possible, but if I can't then it's okay.

Community
  • 1
  • 1
Adam Sears
  • 355
  • 4
  • 14

2 Answers2

1

Please, check Using global variables in a function other than the one that created them, third answer. In short, the global variables in python are module scoped, not interpreter scoped.

I suspect in your case in script.py you need

import DBCL
from DBCL import General
General.sql = sql
Community
  • 1
  • 1
Pawel Jasinski
  • 796
  • 3
  • 10
  • I was hoping I could automate this somehow without needing to do that line. The issue is, is that a ton of people might be writing different script files and mandating they do certain bits of code is something I'd like to avoid if possible. Aside from that, though, good answer. I'll run a few things in and will accept this answer shortly. – Adam Sears Jan 05 '14 at 03:27
  • @AdamSears , you can always create a module "myglobals" where you add in the "sql" object, among other ones, and make the ton of people who write the scripts follow the convention that "sql" is into "myglobals", and so they'll write: `import myglobals` and then use/reference the "sql" object this way:`myglobals.sql` – gog Aug 30 '18 at 14:00
0

Try passing the scope you created when executing the script:

iSource.Execute(iScope);

That should automatically inject anything in the scope into the script's global namespace.

Jeff Hardy
  • 7,632
  • 24
  • 24
  • This was my mistake for not actually showing, but I do pass the scope in Execute. I will adjust my question to reflect that to avoid confusion. – Adam Sears Jan 10 '14 at 14:59