tags:

views:

107

answers:

3

hello,

i have two script name is A.py and B.py

i want to know how to send value from A.py to B.py.

for more detail,when run finished A.py script at the end of script ,A.py call B.py.

my question is i have to send some value from A.py to B.py.

anybody some help me how to send value A.py to B.py,so i can use some value in B.py.

"Do I assume correctly that you want to have B.py to use all the variables with values

that exist when A.py finishes?"

this is what i want exactly. i was upload my A.py and B.py to pastebin site.

http://elca.pastebin.com/m618fa852 <- A.py

http://elca.pastebin.com/m50e7d527 <- B.py

i want to use B.py 's xx value, xx value is come from A.py .

sorry my english

+2  A: 

Your question isn't quite clear.

import B
B.methodToExecute(argument)
notnoop
In case the OP uses `subprocess`: `subprocess.call(['B.py', argument])`
J.F. Sebastian
Hello,i was update my question. thanks for your reply :)
paul
whenever i import Bit whole B file execute....i want to only receive xx value from A file..how to only receive xx value ? thanks in advance
paul
+1  A: 

Do I assume correctly that you want to have B.py to use all the variables with values that exist when A.py finishes?

[edit]

Ok, the problem is you cannot easily do this without any variable assignments. What you'd like to achieve is an import statement done backwards. Normally, if you import a module in an other module or script, the importer (in this example, A) can access the importee's (B) variables, methods etc., but not backwards.

In A.py:

a = 2
print "A.a:", a
import B
print "B.b:", B.b
from B import *
print "b in our namespace:", b

In B.py:

b = 3

This will print:

A.a: 2
B.b: 3
b in our namespace: 3

If you are 100% sure you want this (why wouldn't you create some related classes with methods, or just put the methods in one big module?), you can import module A from module B, so if you modify B.py:

In B.py:

b = 3
from A import *
print "a in B's namespace:", a

... and run it again, you'll see some weird output with double lines and the desired a in B's namespace: 2 line (so it's 50% success). The key is that if you are importing simple scripts without functions and/or module/class declaration, whenever Python imports something, the necessary objects and references will be created inside the Python VM and the imported script gets executed, so you can run into troubles with the previously done circular imports. Use modules or classes and you'll be better, because in those only the parts after

if __name__ == "__main__":
  ...

will be executed on import.

Or, another good idea in this thread is to call a subprocess, but then you need to check for the variables on the command line, not directly in your script's namespace.

Tamás Mezei
I second this question
inspectorG4dget
hello,yes "Do I assume correctly that you want to have B.py to use all the variables with values that exist when A.py finishes?"this is what i want exactly. i was upload my A.py and B.py to pastebin site.http://elca.pastebin.com/m618fa852 <- A.pyhttp://elca.pastebin.com/m50e7d527 <- B.py
paul
thanks Tamas mezei :)
paul
A: 

Your question might have been phrased too abstractly for me to really know what you need.

Generally, what you would do is after you've done everything in A and are read for B, you will have import A at the top of your module and call a function in A that you what taking in all the values you want to pass. If B is currently using hard-coded globals you want to override, refactor it to use functions.

Mike Graham
hello i want to use 'xx' value in A.py from B.pythanks :)
paul