0

My use case : I am making a software that is able to run python scripts with some test cases . Just like the online editor that you see in Competitive Programming Websites like SPOJ/ Hackerrank /Code Forces etc.

I have searched SO and following are the solutions that I got for my problem :

  1. Using import statement to import the script and then test on the functions.
  2. Using os.system to run the script.
  3. Using subprocess.Popen() to run the script.
  4. Using runpy module.

I am a newbie and really don't know what will be the best option for my case. I would love some guidance from the SO community.

  • 3
    It really depends on what kind of script you want to be working at. I would say `import` can already handle lots of structured data scripts easily. So unless you have a multiple platforms integrating with your data, `import` should be working just fine! – Raghav Gupta Sep 28 '20 at 18:18
  • 3
    don't use `os.system()` otherwise all 3 other options may be used depending on specific features of your application. – jfs Sep 28 '20 at 18:21
  • 2
    related: [Getting return information from another python script](https://stackoverflow.com/q/16877323/4279) Also, [Catching python subprocess exception](https://stackoverflow.com/q/27790745/4279) might be useful. – jfs Sep 28 '20 at 18:25
  • 2
    You should avoid `subprocess.Popen()` in favor of `subprocess.run()` if you can. If `os.system()` is acceptable then definitely `run` should be, too. But generally don't run Python as a subprocess of Python if you don't have a particular reason (like forcing multiprocessing by crude means, or isolating e.g. signal handlers into one process without affecting another). – tripleee Sep 28 '20 at 18:35
  • 1
    @RaghavGupta @jfs , I used `import`. But now I am facing a logical error. The problem is that the `import` statements executes only once (even though you call it multiple times in the script)and so , suppose if someone had an error , and he modified the code , then also my script will not import the new code since the old code was imported earlier. Any tips and how to fix this? –  Oct 03 '20 at 09:20

0 Answers0