简体   繁体   中英

How could I use python to open gnome-terminal and then run python commands in a multiline manner?

I am attempting to get a subprocess call that will open a gnome-terminal and in that terminal enter python then execute some python commands and imports without the user needing to type them out.

I'm working on on some automated terminal opening code that will open a gnome-terminal window using subprocess.call ( Open new gnome-terminal and run command ) also ( Python syntax to open gnome-terminal and execute multiple commands )

My end goal is to open up a gnome-terminal window and with the same script that opened the gnome-terminal, enter the command to use python. And then in python import a package and run it.

My current usage is:

subprocess.call(['gnome-terminal', '-e', "python client.py"])

However what Im trying to get to is an importable package that I can open several gnome terminal windows for that will call different objects from a pypi package effectively doing the same thing that call client.py would do with the files. This doesnt work with packages installed in pip however.

What I want to do is something along the lines of:


subprocess.call(['gnome-terminal', '-e', "python && import <package> && c = <package>.obj.func()"])

So that a terminal would open and enter python, import the package I want, then call something from it, but all as instructed by a python file

This doesnt appear to work as multiline scripting works for stuff like bash scripting but doesnt seem to work when trying to enter commands after python has been entered.

Any advice would be greatly appreciated

I don't have Gnome Terminal installed, but if you can get that to start Python correctly, then you can use Python's -i flag to run a set of commands or a script.

The two usages are as follows:

  • python -i path/to/my/script run the script then enter the interpreter
  • python -i -c "# Some Python commands" run the command(s) then enter the interpreter

For example:

$ python -i -c "import this"
[poetry]
>>> 
# Ready for input!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM