4ntoine
4ntoine

Reputation: 20422

How to pass args to embedded python sub-interpreter?

I need to interpret few files (scripts) by embedded python interpreter concurrently (to be more detailed one script executes another script as Popen and my app intercepts it and executes it itself). I've found it's called sub-interpreter and i'm going to use it. But i've read sub-interpreter does not have sys.argv:

The new environment has no sys.argv variable

I need to pass argv anyway so how can i do it?

Upvotes: 1

Views: 392

Answers (2)

John Zwinck
John Zwinck

Reputation: 249424

You can use environment variables. Have the parent set them by updating the dict os.environ if it's in Python, or setenv() if in C or C++ etc. Then the children can read os.environ to get whatever strings they need.

Upvotes: 1

Steve Barnes
Steve Barnes

Reputation: 28405

You might find it easier to modify each of the scripts follow the pattern:

def run(*posargs, **argdict):
   """ 
   This does the work and can be called with:
   import scriptname
   scriptname.run(someargs) 
   """
   # Code goes here and uses posargs[n] where it would use sys.argv[n+1]

if __name__ == "__main__":
   import sys
   run(sys.argv[1:])

Then your main script can just call each of the subscripts in turn by simply calling the run method.

Upvotes: 3

Related Questions