1

I was under the impression that a calling script can access the namespace of the called script. Following is a code section from my calling script:

x= 'python precision.py'
args=shlex.split(x)
print args
p=subprocess.Popen(args)
p.wait()

result.write("\tprecision = "+str(precision)+", recall = ")

where "precision" is a variable in the called script "precision.py". But this gives a NameError. How could i fix this?

2 Answers 2

1

You can't access this. By the time you have arrived in the last line of your script, the called script has finished executing. Therefore its variables don't exist any more. You need to send this data to the calling script in some other way (such as the called script printing it on the standard output and the calling script getting it from there).

Even if it hadn't finished executing, I don't think you could access its variables. In other words, your impression is wrong :-)

Sign up to request clarification or add additional context in comments.

2 Comments

could you tell me how i could pass variable values between scripts? A link would suffice
Access variables of one script from another? It's not possible. You can pass data from caller to called with command-line arguments or by piping data to its stdin, in the opposite way using stdout, and so on. There's not much Python-specific stuff when doing this.
1

subprocess.Popen() allows you to run a command and read from its standard output and/or write to its standard input. It doesn't make much sense to popen a process and then wait for it to finish without communicating with it. That's pretty much like os.system()

If you want a variable in precision.py you do something like the following:

import precision

print "precision variable value =", precision.precision

of course, importing means executing any statements not inside classes or def's

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.