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

2.7k Views Asked by At

I am developing an iOS single view app in Xcode 11.4.1 I added the pythonkit framework in File>Swift packages>Add package dependancy and added the git repo: https://github.com/pvieito/PythonKit.git

My project tree looks like this

When i try to import pythonkit and use it in 'ViewController.swift':

import UIKit
import PythonKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    myfunc()
    // Do any additional setup after loading the view.
}

func myfunc(){
    print("Hello...")
    print(Python.version)
    }
}

It gives the following error:

Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the 
path to a Python library.: file 
/Users/sayansen/Library/Developer/Xcode/DerivedData/TestApp1-cjzcpyjbchjbrlbzuhbvpjlsttfp/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, 
line 40
2020-05-06 17:42:27.320360+0530 TestApp1[35313:1897321] Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.: file /Users/sayansen/Library/Developer/Xcode/DerivedData/TestApp1-cjzcpyjbchjbrlbzuhbvpjlsttfp/SourcePackages/checkouts/PythonKit/PythonKit/PythonLibrary.swift, line 40
(lldb) 

I tried to build a "Command Line tool" for MacOS in xcode. In that project i added the pythonkit framework as above and it worked like a charm! Why is this error happening for iOS app but not Mac OS app?

Note: I have already tried this simmilar post Please help me!

2

There are 2 best solutions below

13
On BEST ANSWER

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 !!

0
On

I've forked PythonKit to support iOS. https://github.com/kewlbear/PythonKit This Swift package will embed Python in your app via dependency.