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". The file is created.
2. After executing "python build_ext --inplace" the file is created.
3. Finally I tried to import sample within Python (2.7), I got an error message. I appended the paths for both and to the python sys.path. The error message I get is: ImportError: 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,

1 person has
this question
  • 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 or call them at:

    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

    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:

    gcc -shared -fPIC sample.c -o

    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

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

    ext_modules = [
    name = 'Sample extension module',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules



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

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