So I thought I'd have a bit of fun writing a small app in Swift to batch convert .py files to .mpy, using mpy-cross. mpy-cross works fine from the command line - it takes a .py file and outputs a new .mpy file in the same location - but I get an error when I call it using Process in Swift. Here's the code -
func convert(filePath: String) -> Bool {
let task = Process()
let pipeOut = Pipe()
let command = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/mpy_cross/mpy-cross " + filePath
task.standardInput = nil
task.standardOutput = pipeOut
task.standardError = pipeOut
task.arguments = ["-c", command]
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
do {
try task.run()
let data = pipeOut.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
print(output)
} catch {
return false
}
return true
}
The error I get is OSError: (1, "can't open {path to myfile.mpy}")
and the .mpy file isn't created, but the task seems to have run ok. Any suggestions much appreciated!
So I thought I'd have a bit of fun writing a small app in Swift to batch convert .py files to .mpy, using mpy-cross. mpy-cross works fine from the command line - it takes a .py file and outputs a new .mpy file in the same location - but I get an error when I call it using Process in Swift. Here's the code -
func convert(filePath: String) -> Bool {
let task = Process()
let pipeOut = Pipe()
let command = "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/mpy_cross/mpy-cross " + filePath
task.standardInput = nil
task.standardOutput = pipeOut
task.standardError = pipeOut
task.arguments = ["-c", command]
task.executableURL = URL(fileURLWithPath: "/bin/zsh")
do {
try task.run()
let data = pipeOut.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)!
print(output)
} catch {
return false
}
return true
}
The error I get is OSError: (1, "can't open {path to myfile.mpy}")
and the .mpy file isn't created, but the task seems to have run ok. Any suggestions much appreciated!
- 1 Is your app in a sandbox? Which project template did you use to create the project? – Sweeper Commented Mar 18 at 7:03
1 Answer
Reset to default 0Turns out it was really simple - just disable the App Sandbox in the .entitlements - you live & learn