libimobiledevice / libplist

A library to handle Apple Property List format in binary or XML
https://libimobiledevice.org
GNU Lesser General Public License v2.1
535 stars 304 forks source link

Compilation issues with Python bindings #148

Closed dnicolson closed 4 years ago

dnicolson commented 4 years ago

The following error message appears when importing plist:

>>> import plist
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/dnicolson/libplist/cython/plist.cpython-38-darwin.so, 2): Symbol not found: _check_datetime
  Referenced from: /Users/dnicolson/libplist/cython/plist.cpython-38-darwin.so
  Expected in: flat namespace
 in /Users/dnicolson/libplist/cython/plist.cpython-38-darwin.so

Cython was setup using the following command and script:

python setup.py build_ext --inplace
from distutils.core import setup
from Cython.Build import cythonize

setup(name=‘Plist’,
      ext_modules=cythonize("plist.pyx"))

Alternatively using pyximport results in the following error:

>>> import pyximport
>>> pyximport.install()
(None, <pyximport.pyximport.PyxImporter object at 0x10d5d6940>)
>>> import plist
/Users/dnicolson/.pyxbld/temp.macosx-10.14-x86_64-3.8/pyrex/plist.c:602:10: fatal error: 'plist_util.h' file not found

macOS 10.14.6 (18G1012).

nikias commented 4 years ago

It works fine for me... something must have been built incorrectly. which cython version is it?

$ cython --version
Cython version 0.29.2
dnicolson commented 4 years ago

I'm using a different setup at the moment, is it possible to see some example code of how it is supposed to be used with Cython?

nikias commented 4 years ago

If cython is installed and you build libplist (and did not specify --without-cython during configure/autogen) it compiles a python module for you. When you install it appropriately so that python can find it (usually sudo make install does) you can just do:

import plist
f = open("test.plist", "r")
pl = plist.load(f)
print(pl)
dnicolson commented 4 years ago

First I removed the other instances of Python (including from Homebrew dependencies). This is running on macOS 10.15.2 (19C57).

sudo easy_install cython

cython --version
Cython version 0.29.14

git clone git@github.com:libimobiledevice/libplist.git cd libplist

./autogen.sh
make
sudo make install

At this point both python and python3 cannot run import plist.

cd cython

cat >setup.py <<END
from distutils.core import setup
from Cython.Build import cythonize

setup(name='Plist',
      ext_modules=cythonize('plist.pyx'))
END

python setup.py build_ext --inplace

Now, the following occurs:

python -c 'import plist'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(./plist.so, 2): no suitable image found.  Did find:
    file system relative paths not allowed in hardened programs
python3 -c 'import plist'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/Users/dave/libplist/cython/plist.so, 2): Symbol not found: _PyClass_Type
  Referenced from: /Users/dave/libplist/cython/plist.so
  Expected in: flat namespace
 in /Users/dave/libplist/cython/plist.so
dnicolson commented 4 years ago

The above compilation steps are not necessary. On Ubuntu no special steps are required, the compiled files are written to and read from the dist-packages directory.

On macOS the compiled files are written to the site-packages directory but the PYTHONPATH environment variable must be defined for imports to work correctly:

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages