-1
 fw_test.py

 def _testmethod_():
     x = []
     y = 0
     while y !=5:
        x.append(y)
        y +=1
        return x

 t = _testmethod_()

 main_test.py

 import subprocess
 p = subprocess.call(['python', 'main_test.py'])

Due to the guidelines I cannot import fw_test.py into main_test.py. I want to be able to store the value returned by _testmethod_() from fw_test.py in a variable inmain_test.py. I learned that with subprocess I can run the fw_test.py, but that is not enough for me.Is there a way to go about this?

Edit: The reason why fw_test.py cannot be imported to main_test.py is that there are many test scripts like fw_test.py which keeps on changing according to test. The main_test.py is supposed to be a generic framework which has basic functions to evaluate the test passed or failed by reading the return value from fw_test.py(the return value is True/False). If I import the script in the top of the file it will not be generic anymore, I think. I'm open to other suggestions.

Why the downvotes?

ilaunchpad
  • 1,283
  • 3
  • 16
  • 32
  • Why have you named your function with double underscores? Don't do that, it's for Python's internal magic methods. – Daniel Roseman Nov 09 '15 at 19:50
  • Can you be more clear about the guidelines that suggest that you not import `fw_test` into `main_test`? – Brian Cain Nov 09 '15 at 19:52
  • can you modify `main_test.py`? e.g., `main_test.py` could use `sys.exit(1)` to return `False` and `sys.exit(0)` to return `True` to `fw_test.py`. Also, you could import the module dynamically using `importlib.import_module()`. – jfs Nov 09 '15 at 20:04
  • related: [Call python script with input with in a python script using subprocess](http://stackoverflow.com/q/30076185/4279) – jfs Nov 09 '15 at 20:09
  • You can always use dynamic imports, see [importlib](https://docs.python.org/2/library/importlib.html). This is how I have done exactly what you are trying to accomplish; the test script is not known until runtime, the name is loaded from a file, and then imported using `importlib`. – SiHa Nov 10 '15 at 12:38

3 Answers3

1

You could use subprocess.check_output to get the output written by another script

the file "b.py" writes its output to stdout

print("hi")
print(1)

the file "a.py" executes "b.py" as subprocess

import subprocess
a = subprocess.check_output(["python", "/tmp/b.py"])
print(a.decode('utf-8'))

Note: value returned by check_output is a byte array and should be decoded to convert it to string

Executing "a.py" from command line

$ python a.py
hi
1

Disclaimer: This is the simplest solution for the given (for training) problem (not the best). Technically I am printing the output to stdout and capturing it in parent process. To solve the same on a more serious problem mechanism such as IPC, RPC, etc should be used.

shanmuga
  • 4,329
  • 2
  • 21
  • 35
  • You are creating confusion: *printing a value* is not the same as *returning a value from a function*. I saw more than once that a complete beginner tries to use `print(x)` instead of `return x` and at best gets `AttributeError` on `None`. The answers like yours only make the problem worse. – jfs Nov 10 '15 at 01:15
  • I suppose I could use the exit code from the first script. But the the range of exit code is very small. Of-course There are other alternatives of IPC. INMHO such elaborate solutions are not required for this exercise, as this this purely a training exercise and not a production code. – shanmuga Nov 10 '15 at 06:56
  • the issue is not the fact that you use `check_output()` (it is ok if you can't import the module); the issue is that there is no disclaimer in your answer that `print(x)` and `return x` are different things (look at the title of the question) – jfs Nov 10 '15 at 07:16
  • it is not what I meant. (1) capturing stdout is already one of IPC method (as any method that allows to pass data between processes would be) (2) you can't expect a person who does not understand the difference between `print(x)` and `return x` to know what RPC is. At this point, my comments should be enough. – jfs Nov 10 '15 at 19:30
  • here's specific example that might illustrate what I mean: [Is it advisable to use print statements in a python function rather than return](http://stackoverflow.com/q/33639658/4279) – jfs Nov 10 '15 at 22:35
0

If you can't use import fw_test statement; you could use importlib.import_module() instead, to get the return value from fw_test.testmethod() function:

import importlib

fw_test = importlib.import_module('fw_test')
value = fw_test.testmethod()
jfs
  • 399,953
  • 195
  • 994
  • 1,670
-3

The simplest way is probably to drop the return value into a file, then have your main pick it up.

fw_test.py

def _testmethod_():
    x = []
    y = 0
    while y !=5:
       x.append(y)
       y +=1
       return x

t = _testmethod_()

with open("fileToStoreValue.txt", 'w') as f:
    f.write(t)

main_test.py

import subprocess
p = subprocess.call(['python', 'main_test.py'])
t = eval(open("fileToStoreValue.txt", 'r').read().strip())

Note that I included strip when reading to prevent you from picking up any trailing new line.

Also keep in mind that this will work as written if both python files are in teh same directory. If they differ in location, you will need to specify a path to fileToSTtoreValue.txt in the open statements.

wnnmaw
  • 5,444
  • 3
  • 38
  • 63