I Can't Import Python Modules in Xcode 11 Using Pythonkit

I can't import Python modules in Xcode 11 using PythonKit

First SO answer, so please forgive formatting / etc. I suffered through this for a while myself with different errors, but generally same issues. I hope this helps you -- a few resources to consider:

1) Pyto -- a fully embedded Python environment for iOS/Catalyst; with LXML and Python Library porting instructions <-- this is what you need to model after to run on iOS, my solution works for Mac Catalyst (Macs with Python preloaded)

2) Python Kit Tutorial -- this guy goes through, step by step, how to implement PythonKit

Here's what worked for me:

1) Disable App Sandbox in Signing and Capabilities:

In top right corner of App Sandbox, under Signing & Capabilities there is an "X", click that to remove App Sandbox

2) In "Hardened Runtime" under Signing and Capabilities: check "Disable Library Validation"

Image of checkbox for Disable Library Validation

Now, I haven't yet submitted an app to the App Store under these provisions, but at least my python files and libraries load / build / run!


UPDATE 05/15/2020:

For Mac Developer Distribution, you will have to sign all .so or .dylib's included with your app, along with the Python Interpreter and bin folder. I made a quick bash script to run through each one when done with dev.

function signThese() {
find . -name "*.$1" | while read line; do
codesign --force --verbose=4 --options=runtime --timestamp --sign "Developer ID Application: [INSERT YOUR CERT HERE]" $line
done
}


This will allow you to use AppSandbox in Signing and Capabilities, and all Hardened Runtime Options (as in not disabling library validation).

I can't use Python module in Swift

A year later, I got a solution.
The name of module was wrong.

import PythonKit
let np = Python.import("numpy")

I should have installed on this way and implemented this way exactly.

Thanks regards!

Xcode: 11.3.1

Swift: 4.2

Toolchain: Swift for TensorFlow 0.8 Release

Set the PYTHON_LIBRARY environment variable with the path to a Python library

We can install PythonKit as below. From the project folder , please create a macOS App and follow the below steps.

  1. Go to File -> Swift Packages ->Add Swift Packages.
  2. Enter the url as 'https://github.com/pvieito/PythonKit.git'
  3. Then continue the process.
  4. Disable App Sandbox in Signing and Capabilities.
  5. In "Hardened Runtime" under Signing and Capabilities: check "Disable Library Validation".

Also Pythonkit is for macOS, Linux and Windows. Its requirements does not specify iOS. iOS doesn't have any python interpreter so it cant recognize 'python'.
Hope this helps !!

Getting error No such module using Xcode, but the framework is there

I'm not sure why this happens, but one way to solve your issue is to go into your build settings and defining the Framework Search Paths to a folder which contains the frameworks in question. If the frameworks are placed in your project directory, simply set the framework search path to $(SRCROOT) and set it to recursive.

Call Python code from an existing project written in Swift

If anyone is ever interested in calling python from swift, here is some helpful material I found:

  • U the python framework - https://developer.apple.com/library/ios/technotes/tn2328/_index.html
  • PyObjC (a little more challenging) -
  • Cobbal - https://github.com/cobbal/python-for-iphone
  • Python docs (you would need to make C-Swift bridge)

Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

How to fix Realm warning Invalid Source (file not found primitive_list_notifier.cpp)

This is a known issue in Realm swift, there were tickets opened for this.

https://github.com/realm/realm-swift/issues/7393

One of the maintainers said it should be fixed in the next release but v5 of Realm is on a slower release cycle compared to v10.

I also noticed that the issue posted on GitHub mentioned it could be due to Swift Package Manager.

I don't think it should impact the project, however, solutions that should work include:

  • Upgrading Realm to 10.8.1 or greater
  • Using Xcode 12.5 since the release notes for Realm 5.5.2 mention it was built in Xcode 12.5
  • Use Cocoapods instead in the case you were using Swift Package Manager

Release Notes for Realm Swift 5.5.2: https://github.com/realm/realm-swift/releases/tag/v5.5.2



Related Topics



Leave a reply



Submit