0

For my unittests I use a setup which is executed in a main.py in the top directory which is executed with several parameters:

python3.9 main.py -i f -f Schematic.xml -d Inno -p Testrun -x

How do I execute that correctly in the setUp of my unittests?

I am aware this question has been asked often already but I can't get this to work. Most of the solutions suggested on SO just make my unittests get stuck or result in an error.

This one is the closest I could bring it to work but the tests just get stuck and I manually need to continue them:

subprocess.Popen(shlex.split("python main.py -i f -f Schematic.xml -d Inno -p Testrun -x"), stdout=open("cmd.log", 'ab'))

What I don't understand is if there is an input to be or what is expected. I took the solution from subprocess.Popen shell=True to shell=False as the previous variant would only get stuck:

subprocess.Popen("python main.py -i f -f Schematic.xml -d Inno -p Testrun -x", shell=True)

Apparently setting shell=False alone is not possible, so I followed the instructions and modified my code (https://stackoverflow.com/a/25465793/2516892). If I just set it to false, I only get an error:

FileNotFoundError: [Errno 2] No such file or directory: 'python main.py -i f -f Schematic.xml -d Inno -p Testrun -x'

So the correct way is supposed to be:

subprocess.check_call(['main.py', '-i f -f Schematic.xml -d Inno -p Testrun -x'], shell=False)

But again all I get is an error:

    File "/usr/lib64/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'main.py

Apparently os shouldn't be used any more. Also it doesn't work:

    os.system('python main.py -i f -f Schematic.xml -d Inno -p Testrun -x')

Here I'm getting:

sh: main.py: command not found

Edit: It is now "solved", but the solution is not really what I like as I consider it as cheap and doesn't answer the question - why the previous trials didn't work. Despite me being in the home directory just calling main.py that didn't work. I used the absolute path:

os.system('python /home/docker/main.py -i f -f Schematic.xml -d Inno -p Testrun -x')

I will keep it like this for now, but again. This is not really what I had in mind

Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • : https://stackoverflow.com/questions/69536168/need-to-pass-parameter-to-unix-script-at-run-time this could help you just check –  Oct 15 '21 at 06:59
  • : https://stackoverflow.com/questions/69528903/how-to-pass-python-variable-to-shell-command this one as well –  Oct 15 '21 at 06:59
  • @wovef57249 - the first one is not what I'm looking for and doesn't relate to calling a script. Also using `os.system` is not recommended to be used for Python Scripts – Qohelet Oct 15 '21 at 08:21
  • If `os.system('python main.py')` doesn't find the file (i.e. the script file is not in the current directory), you need to specify the full path to the file. Perhaps see also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Oct 15 '21 at 08:35
  • Not running Python as a subprocess of itself is often a superior solution. See https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script – tripleee Oct 15 '21 at 08:36

0 Answers0