简体   繁体   中英

Linux, Python open terminal run global python command

Not sure if this is possible. I have a set of python scripts and have modified the linux PATH in ~/.bashrc so that whenever I open a terminal, the python scripts are available to run as a command.

export PATH=$PATH:/home/user/pythonlib/

my_command.py resides in the above path.

I can run my_command.py (args) from anywhere in terminal and it will run the python scripts.

I'd like to control this functionality from a different python script as this will be the quickest solution to automating my processing routines. So I need it to open a terminal and run my_command.py (args) from within the python script I'm working on.

I have tried subprocess:

import subprocess
test = subprocess.Popen(["my_command.py"], stdout=subprocess.PIPE)
output = test.communicate()[0]

While my_command.py is typically available in any terminal I launch, here I have no access to it, returns file not found.

I can start a new terminal using os then type in my_command.py, and it works

os.system("x-terminal-emulator -e /bin/bash")

So, is there a way to get the second method to accept a script you want to run from python with args?

Ubuntu 16

Thanks :)

Popen does not load the system PATH for the session you create in a python script. You have to modify the PATH in the session to include the directory to your project like so:

someterminalcommand = "my_command.py (args)"

my_env = os.environ.copy()
my_env["PATH"] = "/home/usr/mypythonlib/:" + my_env["PATH"]

combine = subprocess.Popen(shlex.split(someterminalcommand), env=my_env)

combine.wait()

This allows me to run my "my_command.py" file from a different python session just like I had a terminal window open.

If you're using Gnome, the gnome-terminal command is rather useful in this situation.

As an example of very basic usage, the following code will spawn a terminal, and run a Python REPL in it:

import subprocess

subprocess.Popen(["gnome-terminal", "-e", "python"])

Now, if you want to run a specific script, you will need to concatenate its path with python , for the last element of that list it the line that will be executed in the new terminal. For instance:

subprocess.Popen(["gnome-terminal", "-e", "python my_script.py"])

If your script is executable, you can omit python :

subprocess.Popen(["gnome-terminal", "-e", "my_script.py"])

If you want to pass parameters to your script, simply add them to the python command:

subprocess.Popen(["gnome-terminal", "-e", "python my_script.py var1 var2"])

Note that if you want to run your script with a particular version of Python, you should specify it, by explicitly calling "python2" or "python3".


A small example:

# my_script.py
import sys
print(sys.argv)
input()

# main.py
import subprocess
subprocess.Popen(["gnome-terminal", "-e", "python3 my_script.py hello world"])

Running python3 main.py will spawn a new terminal, with ['my_script.py', 'hello', 'world'] printed, and waited for an 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