简体   繁体   English

“process.currentDirectoryPath()”函数没有改变路径,我的python文件没有通过swift代码执行

[英]“process.currentDirectoryPath()” function is not changing the path and my python file doesn't get executed through swift code

I am working on a way to execute a python script through swift in a MacOS application in Xcode. 我正在研究一种在Xcode中的MacOS应用程序中通过swift执行python脚本的方法。 I am using a Process class to execute the python script. 我正在使用Process类来执行python脚本。 So when I am trying to change the process directory using process.currentDirectoryPath() to point it to the python file in system. 因此,当我尝试使用process.currentDirectoryPath()更改进程目录时,将其指向系统中的python文件。 But I am unable to change it as it throws an error in command module, saying unable to find the python file 但我无法更改它,因为它在命令模块中抛出一个错误,说无法找到python文件

     let process = Process()
     process.launchPath = "/usr/local/bin/python"
     process.currentDirectoryPath = "\(NSHomeDirectory())/Downloads/<pyhton-file>"
     process.arguments = ["recognize_video.py --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7 --recognizer output/recognizer.pickle --le output/le.pickle"]
     do {
        try process.run()
     }
     catch let error{
         print(error.localizedDescription)
     }
/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'recognize_video.py --detector face_detection_model --embedding-model openface_nn4.small2.v1.t7 --recognizer output/recognizer.pickle --le output/le.pickle': [Errno 2] No such file or directory

This is the output I am getting. 这是我得到的输出。 Can anyone help me. 谁能帮我。

Two problems. 两个问题。 First: 第一:

 process.currentDirectoryPath = "\\(NSHomeDirectory())/Downloads/<pyhton-file>" 

The current directory path must be set to a directory, not to the path of a file. 必须将当前目录路径设置为目录,而不是文件路径。

Second: 第二:

 process.arguments = ["recognize_video.py --detector face_detection_model ..."] 

The arguments must be passed as separate array elements: 参数必须作为单独的数组元素传递:

 process.arguments = ["recognize_video.py", "--detector", "face_detection_model", ... ]

Unlike the shell (which executes the commands which you type in the Terminal), Process does not separate the arguments before passing them to the process to be executed. 与shell(执行在终端中键入的命令)不同, Process在将参数传递给要执行的进程之前不会将参数分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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