# Buggy code
def run_python_script_in_terminal(fname, wdir, args, interact, debug, python_args):
    """Run Python script in an external system terminal"""

    # If fname has spaces on it it can't be ran on Windows, so we have to
    # enclose it in quotes. Also wdir can come with / as os.sep, so we
    # need to take care of it
    if os.name == "nt":
        fname = '"' + fname + '"'
        wdir = wdir.replace("/", "\\")

    p_args = ["python"]
    p_args += get_python_args(fname, python_args, interact, debug, args)

    if os.name == "nt":
        cmd = 'start cmd.exe /c "cd %s && ' % wdir + " ".join(p_args) + '"'
        # Command line and cwd have to be converted to the filesystem
        # encoding before passing them to subprocess, but only for
        # Python 2.
        # See http://bugs.python.org/issue1759845#msg74142 and Issue 1856
        if PY2:
            cmd = encoding.to_fs_from_unicode(cmd)
            wdir = encoding.to_fs_from_unicode(wdir)
        try:
            run_shell_command(cmd, cwd=wdir)
        except WindowsError:
            from qtpy.QtWidgets import QMessageBox

            from spyderlib.config.base import _

            QMessageBox.critical(
                None,
                _("Run"),
                _("It was not possible to run this file in an external terminal"),
                QMessageBox.Ok,
            )
    elif os.name == "posix":
        cmd = "gnome-terminal"
        if is_program_installed(cmd):
            run_program(cmd, ["--working-directory", wdir, "-x"] + p_args, cwd=wdir)
            return
        cmd = "konsole"
        if is_program_installed(cmd):
            run_program(cmd, ["--workdir", wdir, "-e"] + p_args, cwd=wdir)
            return
        cmd = "xfce4-terminal"
        if is_program_installed(cmd):
            run_program(cmd, ["--working-directory", wdir, "-x"] + p_args, cwd=wdir)
            return
        cmd = "xterm"
        if is_program_installed(cmd):
            run_program(cmd, ["-e"] + p_args + [wdir])
            return
        # TODO: Add a fallback to OSX
    else:
        raise NotImplementedError


# Traceback (FileNotFoundError)
Traceback (most recent call last):
File "(...)/site-packages/spyderlib/plugins/editor.py", line 2082, in edit_run_configurations
self.run_file()
File "(...)/site-packages/spyderlib/plugins/editor.py", line 2129, in run_file
self.re_run_file()
File "(...)/site-packages/spyderlib/plugins/editor.py", line 2172, in re_run_file
systerm, post_mortem)
File "(...)/site-packages/spyderlib/spyder.py", line 2483, in open_external_console
interact, debug, python_args)
File "(...)/site-packages/spyderlib/utils/programs.py", line 183, in run_python_script_in_terminal
cwd=wdir)
File "(...)/site-packages/spyderlib/utils/programs.py", line 64, in run_program
subprocess.Popen([path]+args, cwd=cwd)
File "(...)/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "(...)/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: ''

# Fixed code
def run_python_script_in_terminal(fname, wdir, args, interact, debug, python_args):
    """
    Run Python script in an external system terminal.

    :str wdir: working directory, may be empty.
    """

    # If fname has spaces on it it can't be ran on Windows, so we have to
    # enclose it in quotes. Also wdir can come with / as os.sep, so we
    # need to take care of it
    if os.name == "nt":
        fname = '"' + fname + '"'
        wdir = wdir.replace("/", "\\")

    p_args = ["python"]
    p_args += get_python_args(fname, python_args, interact, debug, args)

    if os.name == "nt":
        cmd = 'start cmd.exe /c "cd %s && ' % wdir + " ".join(p_args) + '"'
        # Command line and cwd have to be converted to the filesystem
        # encoding before passing them to subprocess, but only for
        # Python 2.
        # See http://bugs.python.org/issue1759845#msg74142 and Issue 1856
        if PY2:
            cmd = encoding.to_fs_from_unicode(cmd)
            wdir = encoding.to_fs_from_unicode(wdir)
        try:
            run_shell_command(cmd, cwd=wdir)
        except WindowsError:
            from qtpy.QtWidgets import QMessageBox

            from spyderlib.config.base import _

            QMessageBox.critical(
                None,
                _("Run"),
                _("It was not possible to run this file in an external terminal"),
                QMessageBox.Ok,
            )
    elif os.name == "posix":
        programs = [
            {
                "cmd": "gnome-terminal",
                "wdir-option": "--working-directory",
                "execute-option": "-x",
            },
            {"cmd": "konsole", "wdir-option": "--workdir", "execute-option": "-e"},
            {
                "cmd": "xfce4-terminal",
                "wdir-option": "--working-directory",
                "execute-option": "-x",
            },
            {"cmd": "xterm", "wdir-option": None, "execute-option": "-e"},
        ]
        for program in programs:
            if is_program_installed(program["cmd"]):
                arglist = []
                if program["wdir-option"] and wdir:
                    arglist += [program["wdir-option"], wdir]
                arglist.append(program["execute-option"])
                arglist += p_args
                if wdir:
                    run_program(program["cmd"], arglist, cwd=wdir)
                else:
                    run_program(program["cmd"], arglist)
                return
        # TODO: Add a fallback to OSX
    else:
        raise NotImplementedError