0

My GUI is a python script launcher that I would like to create a new console window that handles the selected script. But I can't get subprocess.Popen to handle the arguments sent correctly. It runs the script from the GUI's Python environment and not the user selected environment even though the arguments list is populating correctly (eg. ['C:\\Python\\python.exe', '-i', 'C:\\Script\\testscript.py'].

def run_script(self):
    # Create a list for arguments to be used in Popen
    args = []
    # Get the python path from config.ini
    # Just assume this is C:\Python\python.exe
    python_path = get_settings_value("Python_Path", "Default")
    # Add the python path to the argument list
    # Add '-i' to argument list, this will open an interactive python window
    args.append(python_path)
    args.append('-i')
    # For every script selected, create a subprocess and run with argument list
    for index in self.treeView_scripts.selectedIndexes():
        # Only run if selected item is a file
        if not self.tree_view_model.isDir(index):
            # Get the full path to the script
            script_path = os.path.abspath(self.tree_view_model.fileInfo(index))
            # Append script path to the argument list, run subprocess, and remove the script path from the argument list
            args.append(script_path)
            subprocess.Popen(args, executable = sys.executable, creationflags = subprocess.CREATE_NEW_CONSOLE)
            args.remove(script_path)
martineau
  • 119,623
  • 25
  • 170
  • 301
e4862
  • 19
  • 6
  • Run the scripts by passing them as an argument to the [`sys.executable`](https://docs.python.org/3/library/sys.html#sys.executable). – martineau Jan 07 '21 at 19:35
  • So instead of: subprocess.Popen(args, executable = sys.executable, ... do subprocess.Popen(executable = sys.executable(args), ... ? – e4862 Jan 07 '21 at 20:04
  • No. See [my answer](https://stackoverflow.com/a/64732155/355230) to anther question for an example. There's another [here](https://stackoverflow.com/a/60584125/355230). – martineau Jan 07 '21 at 20:07
  • I just set executable = python_path and that worked perfectly! Thanks! Do you know if there is any way to change the title of the newly spawned window? Right now it just displays the filepath of Python executing it. – e4862 Jan 07 '21 at 20:50
  • The `executable` argument is seldom needed (as the documentation says) and I've never used it myself. Regardless, there's no way anyone could know what the value of `python_path` was from the snippet of code in your question. Sorry, don't know how to how to change the title of the spawned window. – martineau Jan 07 '21 at 21:42
  • I apologize for not making what python_path was clearer. I thought the comment above it would be okay. – e4862 Jan 07 '21 at 22:04
  • It would be preferable to use `sys.executable` instead of hardcoding a path which could be something else on another computer. The former will always be right. – martineau Jan 07 '21 at 22:10

0 Answers0