Integrating Python Library (with numpy) with Pre-existing iOS application

82 views
Skip to first unread message

tomdm...@gmail.com

unread,
Aug 23, 2017, 10:17:01 PM8/23/17
to Kivy users support

I am trying to utilize the kivy toolchain to run a Python module that uses numpy in a preexisting iOS application. I started by using the toolchain create tool to familiarize myself with the kivy environment. This worked great, I got Python up and running and was able to use numpy for basic calculations. I then isolated the toolchain into its own directory, did some set up and got another tutorial project running fine with the kivy create command.


Then, to learn how to integrate python and numpy into my preexisting application, I started a new Xcode project within the same kivy environment and used the following steps to set it up. I am able to run Python fine, but importing numpy gives me the errors specified by the stacktrace at the bottom of the post. The two projects appear identical to me, are pointing to the same code for their exterior libraries (numpy) as well as the same python code to run. The one set up with ./toolchain create works fine, but the one I set up independently does not.


  1. Set up standard Xcode Project 
  2. Create separate folder for the python code 
  3. Add the python headers file to your python project
  4. Update the Framework Search Paths, Header Search Paths, Library Search Paths, and User Header Search paths to reflect the example project 
  5. Add the 3 scripts to be run at build time

// Move all the python files into your project directory

rsync -av --delete “{path to python code}”/ "$PROJECT_DIR"/YourApp

//Compile all the python code

{kivy project directory}/dist/hostpython/bin/python -OO -m compileall "$PROJECT_DIR"/YourApp

//Remove py and pyc files

find "$PROJECT_DIR"/YourApp/ -iname '*.py' -exec rm {} \; -or -iname '*.pyc' -exec rm {} \;

  1. Add the sh.py file into your project directory
  2. Add a recipes folder with the following “recipes” : numpy, python, hostpython, host_setuptools, libffi, hostlibffi
  3. Build Python using the toolchain
  4. Build numpy using the keychain
  5. Update project using the toolchain (./toolchain.py update PythonGroundUp)
  6. Set up a Resources folder with the workspace, adding the following

{kivy project directory}/dist/hostpython/include

{kivy project directory}/dist/root/python/lib

  1. Add “Your App” directory to “Copy Bundle Resources”


Below is my Python Stacktrace as well as the Kivy error messages used to 

Traceback (most recent call last):

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/PythonGroundUp/YourApp/main.py", line 71, in <module>

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/build/numpy/i386/numpy-1.9.1/iosbuild/lib/python2.7/site-packages/numpy/__init__.py", line 170, in <module>

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/build/numpy/i386/numpy-1.9.1/iosbuild/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/build/numpy/i386/numpy-1.9.1/iosbuild/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/build/numpy/i386/numpy-1.9.1/iosbuild/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>

  File "/Users/tommy/development/DiamondKinetics/python_tests/toolchain_testing/build/numpy/i386/numpy-1.9.1/iosbuild/lib/python2.7/site-packages/numpy/core/__init__.py", line 6, in <module>

ImportError: dynamic module does not define init function (initmultiarray)

2017-08-23 16:24:57.607 PythonGroundUp[93146:2868366] Application quit abnormally!

2017-08-23 16:24:57.609 PythonGroundUp[93146:2868366] Leaving


The only difference that I can see is that the current working directory in the second project is simple “/” as opposed to “/Users/tommy/Library/Developer/CoreSimulator/Devices/36F3697E-733D-47A4-A1B0-2475216A402B/data/Containers/Bundle/Application/D8A12E3B-2752-49F8-9C05-44CD8949035F/test187.app/YourApp”. I tried to manually change the cwd location, but without success. 


I put the kivy environment into a GitHub repo. The working Xcode project (created by ./toolchain.py create) is test187. PythonGroundUp is the one I created that is currently not working.


https://github.com/tdmartin4/kivy-ios


Thanks for any help you can give

Mike Ressler

unread,
Aug 24, 2017, 9:41:37 AM8/24/17
to Kivy users support
Tom,

In looking at your projects, I thought for sure it would end up being an issue with a LD_LIBRARY_PATH or some other dynamic loading environment variable as discussed in this article.

I added some logging statements in your Objective-C and Python code to see if there was a difference between the two projects and everything seems to be set the same, except for that current working directory.

I don't have any suggestions for other ways to help debug this, unfortunately. Anyone else on this list have thoughts on ways of debugging dynamically loaded C python modules?

Mike

Mike Ressler

unread,
Sep 1, 2017, 1:04:41 PM9/1/17
to Kivy users support
Great news, I figured out your issue! This ended up being an XCode setting and not an environment discrepancy.

Use OTHER_LDFLAGS = "-all_load"; in your project.pbxproj in order to get the library loaded!

Reply all
Reply to author
Forward
0 new messages