28

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".

Z77
  • 1,097
  • 6
  • 19
  • 30
  • 4
    [Correct syntax is](http://docs.python.org/2/tutorial/modules.html#more-on-modules): `from script1 import x` – Ashwini Chaudhary Oct 10 '13 at 07:06
  • 1
    Yea, I wrote so in my code actually :) as I said it does not work! – Z77 Oct 10 '13 at 08:56
  • Maybe you didn't understand me well. I wrote it correctly: from script1 import x and it does not work. – Z77 Oct 11 '13 at 05:39
  • Please explain *it does not work*? – Ashwini Chaudhary Oct 11 '13 at 05: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 ... – Z77 Oct 11 '13 at 06:08
  • Please, does anyone know where is the problem? It look like very simple,but I do not know! – Z77 Oct 14 '13 at 13:32
  • Post some small sample scripts that can reproduce your error. – Ashwini Chaudhary Oct 14 '13 at 19:25
  • Posted within edited question – Z77 Oct 15 '13 at 08:32
  • Try: `print subprocess.check_output([sys.executable, 'C:\\...\\Desktop\\script2.py'])`, this will print the output of `script2.py`. – Ashwini Chaudhary Oct 16 '13 at 10:22
  • I do not see any error message neither written BLAH nor HELLO words in the Python Interpreter window ?! But the script is still running and running... – Z77 Oct 16 '13 at 10:47
  • http://pastebin.com/K8wE2yeQ, Do you've an infinite loop in `script2.py?` – Ashwini Chaudhary Oct 16 '13 at 10:52
  • The code looks like it is written above (in the question). Nothing more. I wanted to test it with this small example. Is there a loop? I do not see anything. – Z77 Oct 16 '13 at 10:57

7 Answers7

37

The correct syntax is:

from script1 import x

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

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • 1
    What if i want to modify the value of x in script2 and print new value in script1? – human torch Feb 08 '16 at 13:28
  • @human torch did you find a solution for this? I have the exact same question – Quastiat Sep 26 '19 at 22:35
  • 2
    @Quastiat That sounds like bad design - you should always be importing constant values/functions. Perhaps you need to make a copy? – TerryA Oct 01 '19 at 20:57
  • @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 Oct 02 '19 at 09:12
  • 1
    @Quastiat It's a bit hard without context but classes do seem like the way to go yea – TerryA Oct 06 '19 at 17:05
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.

Mr.Zeus
  • 424
  • 8
  • 25
ppwt
  • 91
  • 5
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.

Mihai Stan
  • 1,052
  • 6
  • 7
2

script1.py:

x = 2
from script2 import *

script2.py:

from script1 import x
print x
user2753523
  • 473
  • 2
  • 8
  • 23
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.

0

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

Fabrice Jammes
  • 2,275
  • 1
  • 26
  • 39
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
xfrmrs
  • 1