我创建了一个svg2png.command
文件,它只是启动一个python文件。它以这种方式调用python文件:
shell1 = './scripts/cairosvg.py ../projects/%s/wip/skin/%s/screenLayout/staticScreens/%s -o ../projects/%s/wip/skin/%s/screenLayout/staticScreens/%s' % (projectName, projectName, filename, projectName, projectName, filename[:-4]+".png")
os.system(shell1)
python文件cairosvg.py
导入模块(cairo)。它看起来像:
import cairosvg
cairosvg.main()
当我双击svg2png.command
文件时,一切都按预期进行,并且从svg文件创建了一个png文件。好。
然后我尝试从OSX Swift应用程序启动命令:
let task = NSTask()
task.launchPath = "svg2png.command"
task.arguments = ""
task.launch()
task.waitUntilExit()
我已经使用更简单的python脚本多次完成了这些操作,并且它们都按预期工作。但是在那种情况下,我收到错误:
"Traceback (most recent call last):", " File \"scripts/svg2png.py\", line 4, in <module>", " import cairo", "ImportError: No module named cairo", "s"
为什么双击.command时可以找到这个cairo模块,但是从Swift启动时却找不到?此外,解决这个问题的方法很棒。
环境变量,工作时:
{'LC_CTYPE': 'UTF-8', 'TERM_PROGRAM_VERSION': '343.7', 'LOGNAME': 'username', 'USER': 'username', 'HOME': '/Users/username', 'PATH': '/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/minicom/2.2/bin:/opt/X11/bin:/usr/local/git/bin:/usr/local/bin/jamf', 'DISPLAY': '/private/tmp/com.apple.launchd.9lqHCFuqkq/org.macosforge.xquartz:0', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'Apple_PubSub_Socket_Render': '/private/tmp/com.apple.launchd.3vWnYf4EMF/Render', 'SHLVL': '2', 'XPC_FLAGS': '0x0', '_': '/usr/local/bin/python', 'TERM_SESSION_ID': '24056B9D-EAD4-4A77-A38A-A98909725024', 'XPC_SERVICE_NAME': '0', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.67dyQjSHcg/Listeners', 'SHELL': '/bin/bash', 'LC_ALL': 'en_US.UTF-8', 'TMPDIR': '/var/folders/4c/0qxnphys699fxty03712k30h0000gn/T/', 'OLDPWD': '/Users/username', '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0', 'PWD': '/Users/username/Documents/GIT/studio_design_delivery/UnifyStudioProcess/tools'}
不工作时:
{'_': '/usr/bin/python', 'LOGNAME': 'username', 'USER': 'username', 'XPC_SERVICE_NAME': '0', 'HOME': '/Users/username', 'PATH': '/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'DISPLAY': '/private/tmp/com.apple.launchd.9lqHCFuqkq/org.macosforge.xquartz:0', 'LANG': 'en_US.UTF-8', 'SHELL': '/bin/bash', 'SHLVL': '1', 'XPC_FLAGS': '0x0', '__XCODE_BUILT_PRODUCTS_DIR_PATHS': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug', 'MallocNanoZone': '1', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.67dyQjSHcg/Listeners', 'DYLD_FRAMEWORK_PATH': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug', 'NSUnbufferedIO': 'YES', 'DYLD_LIBRARY_PATH': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug:/usr/lib/system/introspection', 'Apple_PubSub_Socket_Render': '/private/tmp/com.apple.launchd.3vWnYf4EMF/Render', 'LC_ALL': 'en_US.UTF-8', 'TMPDIR': '/var/folders/4c/0qxnphys699fxty03712k30h0000gn/T/', 'OLDPWD': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug', '__XPC_DYLD_FRAMEWORK_PATH': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug', '__XPC_DYLD_LIBRARY_PATH': '/Users/username/Library/Developer/Xcode/DerivedData/USPReleaser-dhgtnfxnfhrtuzcwyapzztrkzifr/Build/Products/Debug', '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0', 'PWD': '/Users/username/Documents/GIT/studio_design_delivery/UnifyStudioProcess/tools'}
他们肯定是不同的,任何想法?