Help get this topic noticed by sharing it on Twitter, Facebook, or email.
I’m confused

Python Cookbook 3rd: Cython

Question on Python Cookbook 3rd edition, chapter 15.10 'Wrapping existing C Code with Cython':
I am quite new to the topic but want to make a connection between C code and Python. I have been able to compile the code, within Ubuntu 12.04, with some remarks:
1. The file Makefile had to be changed since gcc does not accept the -undefined option. So I created a new build target: "gcc -shared sample.c -o libsample.so". The file libsample.so is created.
2. After executing "python setup.py build_ext --inplace" the file sample.so is created.
3. Finally I tried to import sample within Python (2.7), I got an error message. I appended the paths for both sample.so and libsample.so to the python sys.path. The error message I get is: ImportError: libsample.so: cannot open shared object file: No such file or directory.

Can you provide some guidance on the cause of the error and a resolution to finally be able to test access the C code from Python.

Kind regards,

Joost
1 person has
this question
+1
Reply
  • Hello Joost,

    I have forwarded your question on to our Book Tech staff, so they can help you find a solution. If you ever have any questions or issues with the content of a title, you can also contact them directly at booktech@oreilly.com or call them at:

    1-800-889-8969
    or
    1-707-827-7019 (outside the USA)
    7:30 am to 5:00 PST

    Best regards,
    Paul Fichera
    O'Reilly Media
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • Hi Joost,

    I pass your message on to the authors and one of them responded with the following note:
    ---

    The Makefile for linux has build the code using the following:

    linux::
    gcc -shared -fPIC sample.c -o libsample.so

    To find the resulting library in the example (15.10), you might have to set the LD_LIBRARY_PATH variable:

    bash % env LD_LIBRARY_PATH=.. python
    Python 2.7.4 (default, Apr 19 2013, 18:28:01)
    [GCC 4.7.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sample
    >>>

    To avoid setting the LD_LIBRARY_PATH variable, add the runtime_library_dirs argument to setup.py:

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext

    ext_modules = [
    Extension("sample",
    ["sample.pyx"],
    include_dirs=['..'],
    libraries=['sample'],
    library_dirs=['..'],
    runtime_library_dirs=['..'])]
    setup(
    name = 'Sample extension module',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
    )

    -Dave

    ---

    I hope that helps,
    Chris Olson
    O'Reilly Book Support
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly sad, anxious, confused, frustrated happy, confident, thankful, excited indifferent, undecided, unconcerned