PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

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

 May 14, 2022     gnome-terminal, python, subprocess, ubuntu     No comments   

Issue

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


Solution

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!


Answered By - Miguel Guthridge
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing