6

I'm trying to run a python script from swift in my cocoa application.
The script does run but there are some errors.
When I run that file from terminal the script works ok, 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 that I am getting back when I'm trying to run the python file from my application with 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 system() function like that:

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

Then, I used NSTask and it worked perfectly fine.
Now, NSTask is Process and I get those errors when using Process.
There is not much modified in the API, but somehow, it doesn't work.

Edit 1 : After some investigation, I think that the problem may be because Process is starting a new process in the background that is why python is trowing that errors, but I am not sure.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
  • 1
    Nice, I just figured this out using Bundle.main.url(forResource...) from this post: https://stackoverflow.com/questions/34548771/swift-how-do-i-get-the-file-path-inside-a-folder . Leaving a breadcrumb trail for the next web surfer. – user3064009 Jul 01 '17 at 01:31

0 Answers0