3

I am trying to write a program that opens a gnome-terminal window and executes a python file in it.

When I call the gnome-terminal subprocess with the subprocess module like this:

import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3 '+filename])

I get the following error:

Failed to execute child process "python3 /home/user/Documents/test.py” (No such file or directory)

I have tried to cd to the directory /home/user/Documents/test.py first and then run the file, but it didn't work.

Smich
  • 345
  • 1
  • 7
  • 17

4 Answers4

2

You're trying to execute the literal command python3 /home/user/Documents/test.py which obviously doesn't exist on your system.

When you type that line in a shell, the shell will split it on spaces and in the end it will call python3 with /home/user/Documents/test.py as argument.

When using subprocess.call, you have to do the splitting yourself.

Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
0

I believe you need to pass your filename as another element in the array. I don't have gnome-terminal, but I replicated your issue with plain sh.

import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3', filename])
Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
-1

Try this (i assume that python3 is set in PATH)

from subprocess import Popen

command="gnome-terminal -x python3"+filename

proc=Popen(command)

if this not works then try to run your python file first , and see if it works or not

python filename 
pankaj mishra
  • 2,555
  • 2
  • 17
  • 31
-1

Try this:

from os import system
system("gnome-terminal -e 'bash -c \"python3 %s\"'"%filename)

Add other commands using a semicolon:

system("gnome-terminal -e 'bash -c \"python3 %s; [second command]\"'")
Smich
  • 345
  • 1
  • 7
  • 17
  • 1
    I downvoted because you just switched to using `system` rather than `subprocess`, and that doesn't seem to address the OP's question. – Greg Schmit Jun 16 '18 at 16:32