简体   繁体   English

可可应用无法开始循环python脚本。 通过xcode构建/运行时有效

[英]Cocoa app cannot start looping python script. Works when built/run through xcode

I'm trying to use NSTask to start a looping python script from a cocoa app. 我正在尝试使用NSTask从可可应用程序启动循环python脚本。 The python script ends up in the app's Contents/Resources folder. python脚本最终位于应用程序的Contents / Resources文件夹中。

I get the path to the script and create the NSTask with: 我获得了脚本的路径,并使用以下命令创建了NSTask:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSString *scriptPath = [[NSBundle mainBundle] pathForResource:filename ofType:@"py"];
    NSTask* task = [[NSTask alloc] init];
    task.launchPath = PYTHON_PATH;
    task.arguments = [NSArray arrayWithObjects:scriptPath, nil];
    [task launch];
    [task waitUntilExit];
});

(I've omitted the stdOutPipe stuff for readability) (为了便于阅读,我省略了stdOutPipe内容)

When I build & run my app from xcode it works as expected, the task holds at waitUntilExit and the python script runs continuously. 当我从xcode构建和运行我的应用程序时,它可以按预期工作,该任务保留在waitUntilExit且python脚本连续运行。

When I try to run this from the produced .app file it gets to waitUntilExit , stalls for awhile, and then exits with an error. 当我尝试从生成的.app文件中运行该文件时,它会进入waitUntilExit ,停顿一会儿,然后退出并出现错误。 I have logging in the python script so I know that no part of the script is run during this time. 我已经登录了python脚本,所以我知道这段时间没有脚本的任何部分运行。 The script path is true to the location of the python scripts and returns the same path when run from xcode or from the .app. 脚本路径符合python脚本的位置,并且从xcode或.app运行时返回相同的路径。

I figure I must have some of the project settings set-up incorrectly, but it seems like its handling resources correctly. 我认为我必须对某些项目设置进行了不正确的设置,但是看来它正确地处理了资源。 Any idea? 任何想法?

Update 1 Running a non-looping python script with just a print statement works for the .app. 更新1对于.app,仅运行带有打印语句的非循环python脚本即可。 The followings non-looping script fails as well: 以下非循环脚本也会失败:

#!/usr/bin/python      
import os

def log(string):
    print(string)
    path = os.environ['PWD'] + '/smoke_screen_test.txt'
    f1=open(path, 'a')
    f1.write(string + "\n")

if __name__ == '__main__':
    log("Test works")
    exit(0)

Maybe the import statements mess it up? 也许导入语句搞砸了? Or the file creation? 还是文件创建?

The problem was the use of os.environ['PWD'] . 问题是使用了os.environ['PWD'] PWD does not return the local directory of the script being executed. PWD不返回正在执行的脚本的本地目录。 Duh. 咄。 Solution is to grab the path of the script from argv[0] and use the directory of that path: 解决方案是从argv [0]获取脚本的路径并使用该路径的目录:

path = os.path.dirname(sys.argv[0])

Then pass path around as needed. 然后根据需要传递路径。

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

相关问题 Python 脚本在 IDE 中工作,但在内置到可执行文件中时不起作用? - Python script works in the IDE but not when built into executable? 脚本在通过普通 python 解释器运行时工作,但在通过 jupyter 运行时失败 - Script works when run through normal python interpreter but fails when run through jupyter 遍历列表时缓慢的python脚本 - slow python script when looping through a list 使用systemd运行的Python脚本无法启动gedit - Python script run with systemd cannot start gedit Python脚本单独工作,但通过nodejs子进程运行时没有这样的文件 - Python script works alone but no such file when run through nodejs child process 无法通过任务计划程序运行 Python 应用程序 - Cannot run Python app through Task Scheduler 针对可可应用中的苹果库存python运行python脚本 - Run python script against Apple's stock python from Cocoa app 定期运行时,Python脚本运行良好,但使用PyInstaller编译时,效果不佳 - Python script works fine when run regularly, but not when compiled with PyInstaller 当我运行python脚本时,一个应用程序正在打开 - An app is opening when i run python script 无法安排 python 脚本通过 Windows 任务计划程序运行 - Cannot schedule a python script to run through Windows Task Scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM