Python Forum
gnome terminal playing with python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: gnome terminal playing with python (/thread-32278.html)

Pages: 1 2


gnome terminal playing with python - jmex - Feb-01-2021

Hello everyone,

Hope you are fine and safe!
I have started learning and i want to play with gnome-terminal using python3 in ubuntu. I have googled and found a simple code using
import os
os.system("gnome-terminal -e 'sudo apt-get update'")
Actually I want to run a python program named 'pyFoamRunner.py' in gnome-ternimal.

Any idea how can i perform that?

Thanks,
Jmex


RE: gnome terminal playing with python - Axel_Erfurt - Feb-01-2021

better use Popen

from subprocess import Popen

Popen["gnome-terminal", "-e", "'python3 /path/to/pyFoamRunner.py'"]



RE: gnome terminal playing with python - jmex - Feb-01-2021

facing an error stating

Quote:TypeError: 'type' object is not subscriptable



RE: gnome terminal playing with python - Axel_Erfurt - Feb-01-2021

forgot brackets

Popen(["gnome-terminal", "-e", "python3 '/path/to/pyFoamRunner.py'"])



RE: gnome terminal playing with python - jmex - Feb-01-2021

Thanks for helping, it runs without any error but doesn't seem to work the way it should. It creates many files in the working folder if it is made to run with gnome-terminal but if I am running with the code, it doesn't make any file.


RE: gnome terminal playing with python - Axel_Erfurt - Feb-01-2021

try changing working dir to path of scrpipt

change path to the path of pyFoamRunner.py

import os
from subprocess import Popen

os.chdir(path)
Popen(["gnome-terminal", "-e", "python3 ./pyFoamRunner.py"])



RE: gnome terminal playing with python - jmex - Feb-02-2021

nope, nothing. same happens, it runs the code but doesn't execute.

There is no file named pyFoamRunner.py in that folder, but when i execute that command in the terminal in that folder, it will create few files automatically.

But it doesn't happen with python.


RE: gnome terminal playing with python - Axel_Erfurt - Feb-02-2021

I think there is something wrong with your paths

I've created a test.py in /tmp

then

import os
from subprocess import Popen
 
os.chdir("/tmp")
Popen(["xfce4-terminal", "-e", "python3 ./test.py"])
and it works as expected.


RE: gnome terminal playing with python - jmex - Feb-02-2021

no problem with my path i guess. Like you have a python file named test.py in the folder, i do not have such file named pyFoamRunner.py. But when i use to give command in terminal, 'pyFoamRunner.py .' with a dot at the end, it creates many files in that particular folder even though that file(pyFoamRunner.py) isn't in the folder.


RE: gnome terminal playing with python - Axel_Erfurt - Feb-02-2021

Then is it an installed module? You never mentioned that.