[英]How do I run external programs in Python without using the entire path?
Now, I'm working on a voice controlling assistant application. 现在,我正在研究语音控制助手应用程序。
When I say "open Google Chrome", it should open Chrome. 当我说“打开Google Chrome”时,它应该打开Chrome。 I do not have any problems with speech recognition but I do have a problem with starting the program. 我的语音识别没有问题,但是启动程序确实有问题。
For example, the following code: 例如,以下代码:
import os
os.system("chrome.exe")
Works fine for the notepad.exe
or cmd.exe
programs but when I give external programs like chrome
, I need to give the entire path. 对于notepad.exe
或cmd.exe
程序工作正常,但是当我提供外部程序(例如chrome
,我需要给出整个路径。
However, in C# we can only give the direct name like chrome.exe
to run the program. 但是,在C#中,我们只能给直接名称(例如chrome.exe
运行该程序。
So, my problem is that, is there any ways to start external programs without giving the entire path like in C#? 因此,我的问题是,是否有任何方法可以启动外部程序而又不提供C#中的完整路径?
Giving path to start the program would be a serious problem. 提供启动程序的路径将是一个严重的问题。 Because when we move the program to another computer, we will face many code errors. 因为当我们将程序移至另一台计算机时,我们将面临许多代码错误。
The first part of this answer is OS specific and doesn't solve it in code or python 这个答案的第一部分是特定于操作系统的,不会在代码或python中解决
Add the path where Chrome exists to your PATH
variable (you'll likely need a restart), and then when you execute a command such as chrome.exe
it will try to run it from that path location 将Chrome存在的路径添加到PATH
变量中(您可能需要重新启动),然后在执行诸如chrome.exe
之类的命令时,它将尝试从该路径位置运行它
UPDATE: If you want to implement a search for the absolute path here are some good resources on path listing How do I list all files of a directory? 更新:如果要实现对绝对路径的搜索,则在路径列表中有一些不错的资源。 如何列出目录的所有文件?
But again you'll likely need some educated guesses and will need to do some OS specific things to find it. 但是同样,您可能需要一些有根据的猜测,并且需要做一些特定于操作系统的事情才能找到它。
The Chrome binary is often installed in the same set of locations Chrome二进制文件通常安装在同一位置
The PATH
system variable governs this inside Python just as outside. PATH
系统变量在Python内部像在外部一样控制它。 If you want to modify it from Python, it's os.environ
. 如果要从Python修改它, os.environ
。
As an aside, a better solution is probably to use subprocess
instead, as suggested in the os.system
documentation. os.system
,如os.system
文档中所建议的os.system
,更好的解决方案可能是使用subprocess
os.system
。
The PATH
manipulation will be the same regardless. 无论如何, PATH
操作都是相同的。
import os
import subprocess
os.environ['PATH'] = os.environ['PATH'] + r';c:\google\chrome\'
subprocess.run(['chrome.exe'])
... where obviously you need to add the directory which contains chrome.exe
to your PATH
. ...显然需要在其中将包含chrome.exe
的目录添加到PATH
。
Probably you will want to make sure the PATH
is correct even before running Python in your scenario, though. 但是,即使在您的方案中运行Python之前,您可能也要确保PATH
是正确的。
I solved this problem by using C#. 我通过使用C#解决了这个问题。 Like I already mentioned, we can directly call programs like "chrome.exe" in C#. 就像我已经提到的,我们可以在C#中直接调用“ chrome.exe”之类的程序。 I created a console application in C# which open Google Chrome via using bellow code. 我在C#中创建了一个控制台应用程序,该应用程序通过使用以下代码来打开Google Chrome。 Then I complied that console application as .exe, after that I link that exe file with my python program. 然后,我将该控制台应用程序编译为.exe,之后将该exe文件与python程序链接在一起。
C# C#
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "chrome.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
Python 蟒蛇
import os
os.system('console_application.exe')
Try os.path.abspath 尝试os.path.abspath
os.path.abspath("chrome.exe")
returns the following: C:\\Users\\Yourname\\chrome.exe 返回以下内容:C:\\ Users \\ Yourname \\ chrome.exe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.