0

So basically I am running a script in Python that executes other 5 .py scripts as well, just like this:

exec(open('statcprs.py').read())
exec(open('dragndownf.py').read())
exec(open('lowprbubble.py').read())
exec(open('wshearstr.py').read())
exec(open('slices.py').read())

These .py files uses Paraview (another software) to run some stuff, so If I only run "statcprs.py", it will open Paraview's terminal and run the script. The problem is, from the first one "statcprs.py" to the second one "dragndownf.py" it doesn't interrupt the software, and it continue running it, interefering with scripts from both .py files.

I would like to execute the first one, stop and then start the second one from scratch without connection between them. is this somehow possible?

I think the problem is this line (line 1) which opens the terminal:

#!/usr/bin/env pvpython
Xaix
  • 15
  • 8
  • Why not just import the scripts and run them one at a time? – Alexander May 22 '22 at 23:40
  • Hey @alexpdev, thanks for the reply. How would I run them one at a time after importing them? – Xaix May 22 '22 at 23:45
  • I couldn't tell you without knowing their contents, but they must be using an entry point of some sort, so just use that same entry point as a function call – Alexander May 22 '22 at 23:46
  • @Xaix Possible duplicates! [a-way-to-wait-for-another-python-script-called-from-current-script-usi](https://stackoverflow.com/questions/64384795/is-there-a-way-to-wait-for-another-python-script-called-from-current-script-usi) – Mohamad Ghaith Alzin May 22 '22 at 23:52
  • 2
    @Xaix so it seems by importing subprocess `import subprocess` and `subprocess.check_output(...)` where you check and wait for the end of the execution of a .py script you can do that. [execute-a-python-script-on-it-and-then-wait-for-the-script-to-end](https://stackoverflow.com/questions/60847919/how-can-i-open-a-terminal-execute-a-python-script-on-it-and-then-wait-for-the-s) – Mohamad Ghaith Alzin May 22 '22 at 23:57
  • @MohamadGhaithAlzin I will take a look and delete this post later! Thanks for the warning. EDIT: saw you reply, I will try the subprocess lib, thanks =D – Xaix May 22 '22 at 23:58
  • 1
    @Xaix there is no need to delete your post, Your question has similarities to the other but it is not a duplicate, and might receive alternative answers, – Alexander May 23 '22 at 00:00
  • 1
    @Xaix Of course, if that's wouldn't solve your problem you can leave your post so that we can help you more. Thanks! – Mohamad Ghaith Alzin May 23 '22 at 00:00
  • @Xaix you can edit your question and add the contents of `statcprs.py` if you want to figure out how to make it importable. – Alexander May 23 '22 at 00:01
  • Don't try executing your scripts via exec, this is antipattern. Instead, import them and use python debugger, e g. pdb – mvp May 23 '22 at 00:09
  • Hey @alexpdev I edited it with what I think is the main reason for it not to work the way I want. But the answer martineau gave me, made it work. I will try doing some changes with the import as you said though. Ty a lot. – Xaix May 23 '22 at 00:16

1 Answers1

3

The following will execute a list of python scripts in the same folder as the driver script:

import os
from pathlib import Path
import subprocess
import sys

scripts = [
    'statcprs.py',
    'dragndownf.py',
    'lowprbubble.py',
    'wshearstr.py',
    'slices.py',
]

parent = Path(__file__).resolve().parent

for script in scripts:
    script_path = parent / script
    subprocess.call([sys.executable, script_path])
martineau
  • 119,623
  • 25
  • 170
  • 301