0

I am trying to run a python script from another python script. When I call the second script, I want to pass a variable to it.

For instance,

Let's say I have python module A (A.py) and it has the variable oDesktop. When I run a second module B.py, I want to be able to access that oDestkop variable. I am not sure how to do this.

pseudocode:

A.py

oDesktop = DesktopClass();
# RUN SOME CODE
run_module("B.py", oDesktop)

B.py

oDesktop.some_func();

To clarify, I do not have a main in B.py. I have some global variable that I need to keep global.

user2970916
  • 1,146
  • 3
  • 15
  • 37
  • Why don't you just import `B.py` and run its main (or another) method? – Selcuk Feb 05 '15 at 14:56
  • What's wrong importing it normally? `import A - A.oDesktop` or `from A import * - oDesktop` – GLHF Feb 05 '15 at 14:56
  • @Selcuk Note that variable is inside `A.py` not `B` – GLHF Feb 05 '15 at 14:57
  • Yes, but the pseudocode looks like `A` is going to be run manually, then it will execute `B`. – Selcuk Feb 05 '15 at 15:00
  • @Selcuk I don't have a main in B.py. To generate the main will require changing the code extensively which I am trying to avoid. – user2970916 Feb 05 '15 at 15:01
  • Why do you think you need a "main"? Python doesn't have any such concept. As the other commenters state, import B and call the relevant function with the variable. – Daniel Roseman Feb 05 '15 at 15:03
  • @DanielRoseman The variable in B is not contained in a function. It is in global scope. So, as soon as I import B, the code says that global variable oDesktop is not defined. – user2970916 Feb 05 '15 at 15:15
  • Then B can't possibly work at all. There's no point saying you don't want to change the code if the code doesn't work to begin with. – Daniel Roseman Feb 05 '15 at 15:19
  • @DanielRoseman B.py gets called from another program, and works independently of A. The variable oDesktop is given by that external program. So if I call A directly from that program, oDesktop is defined. If I call B directly from that program oDesktop is defined. But if I call A, and then call B from A, then oDesktop is not defined in B. – user2970916 Feb 05 '15 at 15:23
  • I can't understand how B can be called from anywhere if oDesktop is not defined. How is that "other program" defining it? Why can't you do the same in A? Maybe you should show that code. – Daniel Roseman Feb 05 '15 at 15:27
  • @DanielRoseman The other program, I believe, has some globally defined variables which it defines before running the script. Luckily, I did some digging in the other program's API and found they have a mechanism to achieve what I wanted to do. Thanks for the help – user2970916 Feb 05 '15 at 15:44

1 Answers1

1
from A import* 

That way your global variables should remain global to both program and module and you can run functions as you would should you have written them in the same program. NB if program A auto runs sections, just place it under

if __name__ == "__main__":

where this will only run should A be started initially.

user2589273
  • 2,379
  • 20
  • 29