29

I have script1.py which calls script2.py (subprocess.call([sys.executable, "script2.py"]). But script2.py needs variable x that is known in script1.py. I tried a very simple import x from script1, but it seems not to work.

Is that the right approach to use? For example:

#script1.py
import subprocess, sys
##subprocess.call([sys.executable, 'C:\\...\\Desktop\\script2.py'], shell=True)
##os.system("C:\\...\\Desktop\\script2.py")
subprocess.Popen("C:\\...\\Desktop\\script2.py", shell=True)
print "BLAH"
x = BO

#script2.py
from script1 import x
print "HELLO"
print x

All 3 cases of calling script2 (subprocess.call, os.system, subprocess.Popen ) do not work. I get "BLAH" but not "HELLO".

12
  • 4
    Correct syntax is: from script1 import x Commented Oct 10, 2013 at 7:06
  • 1
    Yea, I wrote so in my code actually :) as I said it does not work! Commented Oct 10, 2013 at 8:56
  • Maybe you didn't understand me well. I wrote it correctly: from script1 import x and it does not work. Commented Oct 11, 2013 at 5:39
  • Please explain it does not work? Commented Oct 11, 2013 at 5:40
  • ok. Both scripts are not small scripts, doing a lot of things. The second one which is importing variable from first one is doing some transformations according to the variable t. If I set t as a fix value, for instance t=50 than everything works fine, but If I set t=x where x is imported from script1 than this process never stops, it works and works ... Commented Oct 11, 2013 at 6:08

7 Answers 7

37

The correct syntax is:

from script1 import x

So, literally, "from script1.py import the "x" object."

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

5 Comments

What if i want to modify the value of x in script2 and print new value in script1?
@human torch did you find a solution for this? I have the exact same question
@Quastiat That sounds like bad design - you should always be importing constant values/functions. Perhaps you need to make a copy?
@TerryA Yeah thanks I figured that as well, I implemented a class DataManipulater() where i input my raw data and can apply some adjustments to it via the classes methods. Does that sound reasonable to you?
@Quastiat It's a bit hard without context but classes do seem like the way to go yea
6

Try this:

from script1 import x

I just ran the following pieces of code and it worked

script1:

c = 10

script2:

from script1 import c
print c

The second script printed the integer 10 as you should expect.

Oct 17 Edit: As it stands the code will either not produce the "Hello" as indicated or will go into an infinite loop. A couple of issues:

As it stands, BO is undefined. When you execute script1, the subprocess of script2 is opened. When script2 calls script1, it will print out blah but fail on x=BO as BO is undefined.

So, if you fix that by specifying BO with say a string, it will go into an infinite loop (each script calling the other one and printing x, Hello and Blah).

One potential way to fix it is to pass x through a function call. So, script2 could take x as a function parameter and do whatever you need to do with it.

1 Comment

this is probably no longer valid since the question has been edited
5

Your code is looping because the subprocess.Popen call is in the module initialization code, so it will be called by script2 when importing script1 (creating a new script2 process that also imports script1 ...)

The recommended way of having a python file usable as both a script and a module is to use the __name__ variable

#script1.py

x = BO

if __name__ == "__main__":
    import subprocess, sys
    subprocess.Popen("C:\\...\\Desktop\\script2.py", shell=True)
    print "BLAH"

But also consider this only works for constants. If x can change at runtime you will need an actual interprocess communication method.

Comments

2

script1.py:

x = 2
from script2 import *

script2.py:

from script1 import x
print x

Comments

2

Script 0:

#!/usr/bin/env python
from script1 import x

print x

Script 1:

#!/usr/bin/env python
x = "Hello world"

Output:

Hello world

So yeah it does work, no need for sub processes.

Comments

0

I think you must refer at the variable by prefixing its module name (i.e. script1.x) in script2.py

Comments

0

The method being used is more complex than is necessary. After fixing BO (I assumed it is meant as a string literal, so I quoted it), and assuming the scripts are co-located, you can call script2 and get the result you are after.

See the following:

#script1.py
print "BLAH"
x = "BO"

#script2.py
from script1 import x
print "HELLO"
print x

$ python script2.py 
BLAH
HELLO
BO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.