Cython on Mac?

1,085 views
Skip to first unread message

Mark Summerfield

unread,
Sep 21, 2012, 5:37:57 AM9/21/12
to cython...@googlegroups.com
Hi,

(1)

I'm trying to build some Cython modules on Mac OS X 10.5. These build
perfectly on Linux and they seem to build on Mac. However when I do

import MyMod

I get

MyMod.cpython-32m.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00

Can anyone tell me what I'm doing wrong?

(2)

I'm also trying to access a third-party .dylib on Mac; again it works
fine on Linux but on Mac it won't build because it says it can't find
the library. In setup.py I have

setup(
libraries=[("thing", {})],
ext_modules=cythonize(...

I tried adding library_dirs=["/path/to/"] to ext_modules but that didn't
help.

Nor can I find any documentation for setup()'s "libraries" option.

Can anyone point me to an example where Cython is used to access a Mac
.dylib?

Thanks!

--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Advanced Qt Programming" - ISBN 0321635906
http://www.qtrac.eu/aqpbook.html

Chris Barker

unread,
Sep 21, 2012, 12:04:34 PM9/21/12
to cython...@googlegroups.com
A) I, an many others have no issues with Cython )or other C
extensions) on OS-X, so this certainly can be solved.

B) There are a LOT of different builds of Python for OS-X, and a
couple common compilers, so you'll need to tell us what you are using.

> (1)
>
> I'm trying to build some Cython modules on Mac OS X 10.5.

For 10.5(getting quite old now -- Apple is no longer supporting it!, I
recommend the python.org 32 bit PPC+Intel build.

For compiler, XCode 3.*

> import MyMod
>
> I get
>
> MyMod.cpython-32m.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00
>
> Can anyone tell me what I'm doing wrong?

no -- this is a strange one, I've never seen it.

> (2)
>
> I'm also trying to access a third-party .dylib on Mac; again it works
> fine on Linux but on Mac it won't build because it says it can't find
> the library. In setup.py I have
>
> setup(
> libraries=[("thing", {})],
> ext_modules=cythonize(...
>
> I tried adding library_dirs=["/path/to/"] to ext_modules but that didn't
> help.

I think you need to add that to the Extension modeul itself. I havne't
used cythonize (I wonder why not?), but I do use something like this:

the_ext = Extension('the_ext'
[the_ext.pyx],
language="c++",
libraries = ['first_lib', 'second_lib'],
library_dirs = ['path_to_lib_dir1','path_to_lib_dir2',],
)

> Can anyone point me to an example where Cython is used to access a Mac
> .dylib?

we're doing it, but it's a bit of a mess, so I think the above is
probably more helpful.

Do take a look at the actual compiler command that setup.py is
spitting out -- you'll probably see what's going wrong that way.

Also, if you can boil this down to a small example, post it here so we
can see all the pieces.

-Chris


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Robert Bradshaw

unread,
Sep 21, 2012, 2:05:51 PM9/21/12
to cython...@googlegroups.com
On Fri, Sep 21, 2012 at 9:04 AM, Chris Barker <chris....@noaa.gov> wrote:
> A) I, an many others have no issues with Cython )or other C
> extensions) on OS-X, so this certainly can be solved.

I'll second this. I do most of my Cython development on OS-X.
cythonize is the "new" way of doing things. Note that you can do

extension_list = cythonize(
Extension("*"
["*.pyx"],
language="c++",
libraries = ['first_lib', 'second_lib'],
library_dirs = ['path_to_lib_dir1','path_to_lib_dir2',],
))

to add these options directly to each extension module.

>> Can anyone point me to an example where Cython is used to access a Mac
>> .dylib?
>
> we're doing it, but it's a bit of a mess, so I think the above is
> probably more helpful.
>
> Do take a look at the actual compiler command that setup.py is
> spitting out -- you'll probably see what's going wrong that way.
>
> Also, if you can boil this down to a small example, post it here so we
> can see all the pieces.

Yes, this would be very helpful. Also note that these are not Cython
issues per se, so you could also try asking on the general
Python/distuitls lists.

- Robert

Mark Summerfield

unread,
Sep 23, 2012, 3:31:50 AM9/23/12
to cython...@googlegroups.com, robe...@gmail.com, Chris Barker
Hi Robert, Chris,

On Fri, 21 Sep 2012 11:05:51 -0700
Robert Bradshaw <robe...@gmail.com> wrote:
> On Fri, Sep 21, 2012 at 9:04 AM, Chris Barker <chris....@noaa.gov>
[snip]
[snip]

I'm glad to know that you're both successfully using Cython on Mac OS X.

It seems that really old 10.5's Xcode builds 32-bit and that "newer"
one's build 64-bit (even if both are on 64-bit machines). So I've got an
Xcode that builds 32-bit even though my machine is 64-bit. Also, I've
lost my Xcode disk.

Anyway, the important thing from my POV is that for people using modern
OS X machines it works fine.

mark florisson

unread,
Sep 23, 2012, 5:11:09 AM9/23/12
to cython...@googlegroups.com
On 21 September 2012 10:37, Mark Summerfield <li...@qtrac.plus.com> wrote:
> Hi,
>
> (1)
>
> I'm trying to build some Cython modules on Mac OS X 10.5. These build
> perfectly on Linux and they seem to build on Mac. However when I do
>
> import MyMod
>
> I get
>
> MyMod.cpython-32m.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00
>
> Can anyone tell me what I'm doing wrong?

Did you clean your build directory first? Make sure you don't have
object files from Linux lying around. Here is the clue:

>>> [chr(x) for x in (0x7f, 0x45, 0x4c, 0x46)]
['\x7f', 'E', 'L', 'F']
Reply all
Reply to author
Forward
0 new messages