Running python script from Swift

I am trying to run a python script from swift in my cocoa application.
The script is executing, but there are some errors.
When I ran this file from the terminal, the script is working fine, no errors.

Here is the code that runs the python file:

let process = Process() process.launchPath = "/usr/bin/python" process.currentDirectoryPath = "\(NSHomeDirectory())" + "/tmp" process.arguments = [path.stringByAppendingPathComponent("pacman.py")] process.launch() 

Here is the error I am returning when I try to run a python file from my application using process ():

 Traceback (most recent call last): File "/Users/fflorica/Library/Developer/Xcode/DerivedData/Taylor-acpgvfjjherixnfchbrbrayxxvsm/Build/Products/Debug/Taylor.app/Contents/Frameworks/TaylorFramework.framework/Resources/pacman.py", line 439, in <module> g = Game() File "/Users/fflorica/Library/Developer/Xcode/DerivedData/Taylor-acpgvfjjherixnfchbrbrayxxvsm/Build/Products/Debug/Taylor.app/Contents/Frameworks/TaylorFramework.framework/Resources/pacman.py", line 251, in __init__ self._init_curses() File "/Users/fflorica/Library/Developer/Xcode/DerivedData/Taylor-acpgvfjjherixnfchbrbrayxxvsm/Build/Products/Debug/Taylor.app/Contents/Frameworks/TaylorFramework.framework/Resources/pacman.py", line 280, in _init_curses curses.cbreak() error: cbreak() returned ERR Traceback (most recent call last): File "/Users/fflorica/Library/Developer/Xcode/DerivedData/Taylor-acpgvfjjherixnfchbrbrayxxvsm/Build/Products/Debug/Taylor.app/Contents/Frameworks/TaylorFramework.framework/Resources/pacman.py", line 450, in <module> raw_input() EOFError 

What am I doing wrong?

Edit: in the past, I used the system () function:

 system("cd " + path) system("python " + path + "/pacman.py") 

Then I used NSTask and it worked perfectly.
NSTask is now a process, and I get these errors when using Process.
There are not many changes in the API, but for some reason this is not work.

Edit 1: after some investigation, I think that the problem may be caused by the fact that Process starts a new process in the background, so python is silent about these errors, but I'm not sure.

+6
source share

Source: https://habr.com/ru/post/1011942/


All Articles