×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Python script calling another and passing all vars set in first
2

Python script calling another and passing all vars set in first

Python script calling another and passing all vars set in first

(OP)
I've seen many examples of python script A calling python script B, but in testing I can't figure out how ALL variables set in the script A can be referenced by the called script B. eg I set x = "foo" in the 1st script and then when I call the 2nd script, I want to reference x which has been set to "foo". If I have 50 + (an unknown # ) vars set in the first script, I don't want to explicitly hand the second script all 50+ variables since I can't really know what they are each time it runs, I just want it to script B to 'know' all 50+. Is that possible ? Some languages call this an 'include'. thanks !

RE: Python script calling another and passing all vars set in first

(OP)
It seems the answer is there is no way to do what I require and will just have to concatenate the scripts into one.

RE: Python script calling another and passing all vars set in first

Since you haven't said what you need to to do I can't comment further.

RE: Python script calling another and passing all vars set in first

(OP)
Hmm, I guess my original post wasn't clear. A.py runs and an unknown # of vars is set in it, A.py then 'reads' in B.py and it is executed, the code in B.py can reference any variable set by A.py before it was read in and any variable set or changed in B.py is known to A.py after the B.py code is at the end. The key here is each time it runs you don't know what variables will be set, so you can't explicitly 'hand' those variables to B.py. Anyway, it seems that splicing in B.py into A.py may be easiest.

RE: Python script calling another and passing all vars set in first

Do you have to convert PHP which does such questionable things as includes?
I don't know another language which does includes this way as just in place adding of the include file as if this was one script.

The only way this can make sense, also in PHP, is when this only happens conditionally and not always. If it's always it only may help with mainting code in two or more shorter files.
I would really think about refactoring the code to shorten it, make it OOP or at least procedural with several functions to which you pass the necessary vars.

For now the fastest solution you have is really just merging your two files into one, it's not good programming style anyway. My experience with such features only one or few languages offer is the feeling of superiority of this feature, but it really asks for bad programming style.

Actually the closest I know is exec(open("./filename").read()), but I don't use that. This will run the file in the same context, but I'm not sure whether that means having same variable access, what's for sure is it can access the same sys.argv arguments initially going onto the first py script call.

Chriss

RE: Python script calling another and passing all vars set in first

Chris - the question is about python not PHP.

RE: Python script calling another and passing all vars set in first

It seems like you need first to understand the important differences between:

A file containing Python statements.

A Python program.

Classes.

Functions.

RE: Python script calling another and passing all vars set in first

If you tell us what you need to do instead of how you think it should be done you will likely get better suggestions.

RE: Python script calling another and passing all vars set in first

(OP)
I needed to convert an archaic language into python quite similarly to what Chris mentioned. I have enough to go on now, so the lecturing can cease. As you were.

RE: Python script calling another and passing all vars set in first

Which archaic language

RE: Python script calling another and passing all vars set in first

Quote (AIXouch)


eg I set x = "foo" in the 1st script and then when I call the 2nd script, I want to reference x which has been set to "foo".
IMO, it is not possible without making substantial changes to the scripts.
I would try it as follows: At the end of the 1.script save the variables into a JSON-file and at the begin of the 2.script read the saved JSON into dictionary.

RE: Python script calling another and passing all vars set in first

(OP)
It is a proprietary IBM language without a name really. It is used to process snmp, syslog, etc events. Great regex capabilities, but costly to run.

RE: Python script calling another and passing all vars set in first

xwb,

I'm totally aware. I asked whether his job is to do a conversion from PHP to Python.

Chriss

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close