user3808752
user3808752

Reputation: 169

launch python script in another xterm session

This line of code has my stymied. I've read about many ways to launch a python script in a new terminal window. I think I'm very close with this statement but it seems to be choking on my path.

the line of code is:

os.system("xterm -hold 'sudo python /home/pi/newcode/newcode/hms2-v2.5.py'")

the error message is:

xterm: no absolute path found for shell: sudo python /home/pi/newcode/newcode/hms2-v2.5.py

Upvotes: 1

Views: 6167

Answers (3)

Er Saurav Singh
Er Saurav Singh

Reputation: 11

subprocess.Popen(['xterm', '-e', 'bash -c \"python pythonFile.py; exec bash\"'])

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54583

As noted, you should use the -e option:

-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in the xterm window. It also sets the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on the command line.
NOTE: This must be the last option on the command line.

There's a subtle point in the manual page: when you omit the -e, xterm tries to interpret the whole quoted string as the name of a shell interpreter. That's indirectly due to the way it implements the localFilter:

When using a locale-filter, e.g., with the -e option, or the shell, xterm first tries passing control via that filter. If it fails, xterm will retry without the locale-filter. Xterm warns about the failure before retrying.

There's some additional discussion at the beginning of the OPTIONS section:

One parameter (after all options) may be given. That overrides xterm's built-in choice of shell program:

  • If the parameter is not a relative path, i.e., beginning with "./" or "../", xterm looks for the file in the user's PATH. In either case, this check fails if xterm cannot construct an absolute path.

So... when you tried

os.system("xterm -hold 'sudo python /home/pi/newcode/newcode/hms2-v2.5.py'")

xterm tried finding a program named

sudo python /home/pi/newcode/newcode/hms2-v2.5.py

which was not what you intended.

Upvotes: 0

larsks
larsks

Reputation: 312360

It's telling you the truth. When you run xterm <thing>, xterm will try to find a binary named <thing> in your $PATH. In this case, you've passed a long quoted string but it's still a single argument and xterm is predictably failing to find it.

For what you're trying to do you need the -e option. From the man page:

   -e program [ arguments ... ]
           This option specifies the program (and its command line arguments) to be run in the xterm window.  It also sets
           the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on
           the command line.

So:

os.system("xterm -hold -e sudo python /home/pi/newcode/newcode/hms2-v2.5.py")

Upvotes: 2

Related Questions