Compiling modules in OSX, eg PyUSB?

Discussion in 'Python' started by Dr Mephesto, Mar 20, 2009.

  1. Dr Mephesto

    Dr Mephesto Guest

    Hi,

    I am using Leopard and MacPython, and I would like to access a USB
    device. I have installed libusb, and now I have tried to compile PyUSB
    from:
    http://sourceforge.net/projects/pyusb/

    But when I compile I get lots of errors, ie:

    pcfr147:pyusb-0.4.1 david$ python setup.py install
    running install
    running build
    running build_ext
    building 'usb' extension
    gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -
    fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -
    fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/
    Python.framework/Versions/4.1.30101/include/python2.5 -c pyusb.c -o
    build/temp.macosx-10.3-i386-2.5/pyusb.o
    In file included from pyusb.c:11:
    pyusb.h:6:17: error: usb.h: No such file or directory
    In file included from pyusb.c:11:
    pyusb.h:120: error: syntax error before 'usb_dev_handle'
    pyusb.h:120: warning: no semicolon at end of struct or union
    pyusb.h:122: error: syntax error before '}' token
    pyusb.h:122: warning: data definition has no type or storage class
    pyusb.h:131: warning: 'struct usb_endpoint_descriptor' declared inside
    parameter list
    pyusb.h:131: warning: its scope is only this definition or
    declaration, which is probably not what you want
    ....
    ....
    pyusb.c:2083: error: dereferencing pointer to incomplete type
    pyusb.c:2084: warning: passing argument 1 of 'new_Bus' from
    incompatible pointer type
    lipo: can't figure out the architecture type of: /var/folders/xt/
    xtY5vvWXEUyZv0KZrwtRz++++TI/-Tmp-//ccYg0qka.out
    error: command 'gcc' failed with exit status 1


    A long time googling found this similar problem:
    http://ubuntuforums.org/archive/index.php/t-325241.html
    The suggested answer was to first do:

    sudo apt-get install python-dev

    But that was for a debian machine. I dont use fink, and I prefer to
    use a standard osx python install if possible. I am still a python
    novice, and I'm not sure if I am using the "python setup.py install"
    command wrongly, or if I need this python-dev package, or something
    completely different.

    Has anyone had this problem, or know a solution? Thanks in Advance!
    Dr Mephesto, Mar 20, 2009
    #1
    1. Advertising

  2. On Mar 20, 2009, at 9:48 AM, Dr Mephesto wrote:

    > Hi,
    >
    > I am using Leopard and MacPython, and I would like to access a USB
    > device. I have installed libusb, and now I have tried to compile PyUSB
    > from:
    > http://sourceforge.net/projects/pyusb/
    >
    > But when I compile I get lots of errors, ie:


    Hi Dr. M.,
    The first error you get is that the compiler can't find usb.h:

    > pyusb.h:6:17: error: usb.h: No such file or directory



    It's not surprising that a compile produces lots of errors when it
    can't find a header file.

    This project is looking for a header file that's not on your system
    because PyUSB is Linux-specific. That doesn't mean it can't be made to
    run on OS X, but the amount of effort required to make that happen
    might range anywhere from trivial to difficult.

    PyUSB deals with hardware, and I wouldn't be surprised to find that a
    hardware interface varies from OS to OS. Since an entire header file
    is missing here, my guess is that making it work on OS X would be
    closer to difficult than trivial. It's also possible (likely?) that
    the author doesn't even have a Mac or access to one, and so can't
    experiment under OS X.

    You need to contact the package author to ask if he has considered OS
    X support. If not, you'll need to find another package. Google for
    something like this:
    python usb "os x"

    That should help you to find packages that will work under OS X. Be
    aware that there might not be such a package. :-/


    Hope this helps
    Philip
    Philip Semanchuk, Mar 20, 2009
    #2
    1. Advertising

  3. Dr Mephesto

    Dr Mephesto Guest

    Thanks.

    I found some more info that might help, if I understood it :)From the
    main PyUSB page, at http://pyusb.berlios.de/ , its says:

    "PyUSB uses the libusb to do its work, so, any system which has Python
    and libusb should work for PyUSB."

    I have installed the OSX version of libusb, and it puts the missing
    file "usb.h" in the directory: usr/local/include/usb.h
    In the instruction for installing PyUSB on windows, it says:

    "2) libusb-win32: a Windows version of the libusb C library available
    from http://libusb-win32.sourceforge.net.
    From within a Cygwin terminal, copy the libusb.a file from the
    libusb-win32 lib/ directory to $(CYGWINDIR)/usr/lib/, and copy the
    usb.h file from the libusb-win32 include/ directory to
    $(CYGWINDIR)/usr/include/. You can build and install PyUSB with the
    command: python setup.py install"

    As I have the required usb.h file, is there some way to let the
    compiler know where is it when I run python setup.py install? I
    already tried copying the "usb.h" file from usr/local/include/ to /usr/
    iinclude, but it still didnt find it.

    Dave
    Dr Mephesto, Mar 20, 2009
    #3
  4. On Mar 20, 2009, at 11:39 AM, Dr Mephesto wrote:

    > Thanks.
    >
    > I found some more info that might help, if I understood it :)From the
    > main PyUSB page, at http://pyusb.berlios.de/ , its says:
    >
    > "PyUSB uses the libusb to do its work, so, any system which has Python
    > and libusb should work for PyUSB."


    OK, great -- that moves your problem from PyUSB to libusb, which is
    useful to know. The PyUSB page lists supported operating systems only
    as "Linux" which is partly what threw me off.


    > I have installed the OSX version of libusb, and it puts the missing
    > file "usb.h" in the directory: usr/local/include/usb.h
    > In the instruction for installing PyUSB on windows, it says:
    >
    > "2) libusb-win32: a Windows version of the libusb C library available
    > from http://libusb-win32.sourceforge.net.
    >> From within a Cygwin terminal, copy the libusb.a file from the

    > libusb-win32 lib/ directory to $(CYGWINDIR)/usr/lib/, and copy the
    > usb.h file from the libusb-win32 include/ directory to
    > $(CYGWINDIR)/usr/include/. You can build and install PyUSB with the
    > command: python setup.py install"


    Whoa, why are we discussing Windows all of a sudden?


    > As I have the required usb.h file, is there some way to let the
    > compiler know where is it when I run python setup.py install? I
    > already tried copying the "usb.h" file from usr/local/include/ to /
    > usr/
    > iinclude, but it still didnt find it.


    You don't have to do anything special to get the Python compile step
    to look in /usr/include or /usr/local/include.

    Can you post the output from this command?
    find /usr -name usb.h -ls

    Also post the output you get when you run this for PyUSB:
    sudo python setup.py install
    Philip Semanchuk, Mar 20, 2009
    #4
  5. Dr Mephesto

    Dr Mephesto Guest

    windows? well, I thought that maybe the location of the "usb.h" thing
    was relevant, and I didnt see it mentioned on the linux instructions.

    find /usr -name usb.h -ls gives me:
    3545683 24 -rw-r--r-- 1 root wheel 8360 Mar 20
    16:37 /usr/include/usb.h
    3538549 24 -rw-rw-r-- 1 root wheel 8360 Feb 22
    11:28 /usr/local/include/usb.h



    sudo python setup.py install this gives me:
    pcfr147:pyusb-0.4.1 david$ sudo python setup.py install
    Password:
    running install
    running build
    running build_ext
    building 'usb' extension
    gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -
    fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -
    fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/
    Python.framework/Versions/4.1.30101/include/python2.5 -c pyusb.c -o
    build/temp.macosx-10.3-i386-2.5/pyusb.o
    In file included from pyusb.c:11:
    pyusb.h:6:17: error: usb.h: No such file or directory
    In file included from pyusb.c:11:
    pyusb.h:120: error: syntax error before 'usb_dev_handle'
    pyusb.h:120: warning: no semicolon at end of struct or union
    pyusb.h:122: error: syntax error before '}' token
    pyusb.h:122: warning: data definition has no type or storage class
    pyusb.h:131: warning: 'struct usb_endpoint_descriptor' declared inside
    parameter list
    pyusb.h:131: warning: its scope is only this definition or
    declaration, which is probably not what you want
    pyusb.h:135: warning: 'struct usb_endpoint_descriptor' declared inside
    parameter list
    pyusb.h:140: warning: 'struct usb_interface_descriptor' declared
    inside parameter list
    pyusb.h:144: warning: 'struct usb_interface_descriptor' declared
    inside parameter list
    pyusb.h:149: warning: 'struct usb_config_descriptor' declared inside
    parameter list
    pyusb.h:153: warning: 'struct usb_config_descriptor' declared inside
    parameter list
    pyusb.h:171: warning: 'struct usb_bus' declared inside parameter list
    pyusb.h:239: error: syntax error before '*' token
    pyusb.h:241: warning: data definition has no type or storage class
    pyusb.c: In function 'getBuffer':
    pyusb.c:143: warning: passing argument 3 of 'PyString_AsStringAndSize'
    from incompatible pointer type
    pyusb.c: In function 'installModuleConstants':
    pyusb.c:236: error: 'USB_CLASS_PER_INTERFACE' undeclared (first use in
    this function)
    pyusb.c:236: error: (Each undeclared identifier is reported only once
    pyusb.c:236: error: for each function it appears in.)
    pyusb.c:237: error: 'USB_CLASS_AUDIO' undeclared (first use in this
    function)
    pyusb.c:238: error: 'USB_CLASS_COMM' undeclared (first use in this
    function)
    pyusb.c:239: error: 'USB_CLASS_HID' undeclared (first use in this
    function)
    pyusb.c:240: error: 'USB_CLASS_PRINTER' undeclared (first use in this
    function)
    pyusb.c:241: error: 'USB_CLASS_MASS_STORAGE' undeclared (first use in
    this function)
    pyusb.c:242: error: 'USB_CLASS_HUB' undeclared (first use in this
    function)
    pyusb.c:243: error: 'USB_CLASS_DATA' undeclared (first use in this
    function)
    pyusb.c:244: error: 'USB_CLASS_VENDOR_SPEC' undeclared (first use in
    this function)
    pyusb.c:245: error: 'USB_DT_DEVICE' undeclared (first use in this
    function)
    pyusb.c:246: error: 'USB_DT_CONFIG' undeclared (first use in this
    function)
    pyusb.c:247: error: 'USB_DT_STRING' undeclared (first use in this
    function)
    pyusb.c:248: error: 'USB_DT_INTERFACE' undeclared (first use in this
    function)
    pyusb.c:249: error: 'USB_DT_ENDPOINT' undeclared (first use in this
    function)
    pyusb.c:250: error: 'USB_DT_HID' undeclared (first use in this
    function)
    pyusb.c:251: error: 'USB_DT_REPORT' undeclared (first use in this
    function)
    pyusb.c:252: error: 'USB_DT_PHYSICAL' undeclared (first use in this
    function)
    pyusb.c:253: error: 'USB_DT_HUB' undeclared (first use in this
    function)
    pyusb.c:254: error: 'USB_DT_DEVICE_SIZE' undeclared (first use in this
    function)
    pyusb.c:255: error: 'USB_DT_CONFIG_SIZE' undeclared (first use in this
    function)
    pyusb.c:256: error: 'USB_DT_INTERFACE_SIZE' undeclared (first use in
    this function)
    pyusb.c:257: error: 'USB_DT_ENDPOINT_SIZE' undeclared (first use in
    this function)
    pyusb.c:258: error: 'USB_DT_ENDPOINT_AUDIO_SIZE' undeclared (first use
    in this function)
    pyusb.c:259: error: 'USB_DT_HUB_NONVAR_SIZE' undeclared (first use in
    this function)
    pyusb.c:260: error: 'USB_MAXENDPOINTS' undeclared (first use in this
    function)
    pyusb.c:261: error: 'USB_ENDPOINT_ADDRESS_MASK' undeclared (first use
    in this function)
    pyusb.c:262: error: 'USB_ENDPOINT_DIR_MASK' undeclared (first use in
    this function)
    pyusb.c:263: error: 'USB_ENDPOINT_TYPE_MASK' undeclared (first use in
    this function)
    ....
    and lots lots more. do you want the whole lot? It still cant find that
    usb.h file.

    dave
    Dr Mephesto, Mar 20, 2009
    #5
  6. On Mar 20, 2009, at 12:36 PM, Dr Mephesto wrote:

    > windows? well, I thought that maybe the location of the "usb.h" thing
    > was relevant, and I didnt see it mentioned on the linux instructions.


    Oh, OK. Windows is a pretty different animal from Unix/Linux so it's
    not likely to be of much help here.

    >
    >
    > find /usr -name usb.h -ls gives me:
    > 3545683 24 -rw-r--r-- 1 root wheel 8360 Mar 20
    > 16:37 /usr/include/usb.h
    > 3538549 24 -rw-rw-r-- 1 root wheel 8360 Feb 22
    > 11:28 /usr/local/include/usb.h



    Looks good.


    > sudo python setup.py install this gives me:
    > pcfr147:pyusb-0.4.1 david$ sudo python setup.py install
    > Password:
    > running install
    > running build
    > running build_ext
    > building 'usb' extension
    > gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -
    > fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -
    > fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/
    > Python.framework/Versions/4.1.30101/include/python2.5 -c pyusb.c -o
    > build/temp.macosx-10.3-i386-2.5/pyusb.o
    > In file included from pyusb.c:11:
    > pyusb.h:6:17: error: usb.h: No such file or directory


    OK, I think I see what's going wrong, but I don't understand it. I
    think I was wrong; setup.py doesn't automatically add /usr/include
    and /usr/local/include to the include path when compiling. I'm basing
    this on the documentation here, which states that these paths have to
    be added explicitly:
    http://docs.python.org/distutils/setupscript.html#preprocessor-options


    However, the two C extensions I distribute compile just fine without
    that, and they certainly rely on /usr/include and /usr/local/include.
    Strange...


    But nevermind that. Looking in the setup.py for PyUSB, I see this
    special code added for OS X (a.k.a. Darwin):

    elif -1 != platform.find("darwin"):
    extra_link_args = ['-framework',
    'CoreFoundation',
    '-framework',
    'IOKit',
    '-L/sw/lib']
    extra_compile_args = ['-I/sw/include']


    The ['-I/sw/include'] tells the compiler what directories to search
    for include files. The /sw/ tree is specific to Fink, so if you'd used
    Fink to install libusb then the PyUSB setup would have found it. But
    you didn't use Fink (nothing wrong with that; I don't either) and so
    your usb.h landed in /usr/local/include. What you need to do, then, is
    add that directory to the list.

    So change line 32 in the PyUSB setup.py from this:
    extra_compile_args = ['-I/sw/include']
    to this:
    extra_compile_args = ['-I/sw/include', '-I/usr/local/include']


    The same assumption is made about the linker path. Note the '-L/sw/
    lib'. You'll need to track down your copy of libusb (it's probably in /
    usr/local/lib) and add that to the extra_link_args like so:

    extra_link_args = ['-framework',
    'CoreFoundation',
    '-framework',
    'IOKit',
    '-L/sw/lib',
    '-L/usr/local/lib']



    Run setup again and I bet you'll be off to the races.


    You should certainly report this to the package maintainer, and you
    might also want to point out that the if block starting on line 17 of
    setup.py and the if block starting on line 26 are both for OS X, but
    they lead to different results!


    Let us know how it works out,
    Philip
    Philip Semanchuk, Mar 20, 2009
    #6
  7. Dr Mephesto

    Dr Mephesto Guest

    On Mar 20, 6:23 pm, Philip Semanchuk <> wrote:
    > On Mar 20, 2009, at 12:36 PM, Dr Mephesto wrote:
    >
    > > windows? well, I thought that maybe the location of the "usb.h" thing
    > > was relevant, and I didnt see it mentioned on the linux instructions.

    >
    > Oh, OK. Windows is a pretty different animal from Unix/Linux so it's  
    > not likely to be of much help here.
    >
    >
    >
    > > find /usr -name usb.h -ls   gives me:
    > > 3545683       24 -rw-r--r--    1 root     wheel        8360 Mar 20
    > > 16:37 /usr/include/usb.h
    > > 3538549       24 -rw-rw-r--    1 root     wheel        8360 Feb 22
    > > 11:28 /usr/local/include/usb.h

    >
    > Looks good.
    >
    > > sudo python setup.py install         this gives me:
    > > pcfr147:pyusb-0.4.1 david$ sudo python setup.py install
    > > Password:
    > > running install
    > > running build
    > > running build_ext
    > > building 'usb' extension
    > > gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -
    > > fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -
    > > fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/
    > > Python.framework/Versions/4.1.30101/include/python2.5 -c pyusb.c -o
    > > build/temp.macosx-10.3-i386-2.5/pyusb.o
    > > In file included from pyusb.c:11:
    > > pyusb.h:6:17: error: usb.h: No such file or directory

    >
    > OK, I think I see what's going wrong, but I don't understand it. I  
    > think I was wrong; setup.py doesn't automatically add /usr/include  
    > and /usr/local/include to the include path when compiling. I'm basing  
    > this on the documentation here, which states that these paths have to  
    > be added explicitly:http://docs.python.org/distutils/setupscript.html#preprocessor-options
    >
    > However, the two C extensions I distribute compile just fine without  
    > that, and they certainly rely on /usr/include and /usr/local/include.  
    > Strange...
    >
    > But nevermind that. Looking in the setup.py for PyUSB, I see this  
    > special code added for OS X (a.k.a. Darwin):
    >
    > elif -1 != platform.find("darwin"):
    >      extra_link_args = ['-framework',
    >                         'CoreFoundation',
    >                         '-framework',
    >                         'IOKit',
    >                         '-L/sw/lib']
    >      extra_compile_args = ['-I/sw/include']
    >
    > The ['-I/sw/include'] tells the compiler what directories to search  
    > for include files. The /sw/ tree is specific to Fink, so if you'd used  
    > Fink to install libusb then the PyUSB setup would have found it. But  
    > you didn't use Fink (nothing wrong with that; I don't either) and so  
    > your usb.h landed in /usr/local/include. What you need to do, then, is  
    > add that directory to the list.
    >
    > So change line 32 in the PyUSB setup.py from this:
    >      extra_compile_args = ['-I/sw/include']
    > to this:
    >      extra_compile_args = ['-I/sw/include', '-I/usr/local/include']
    >
    > The same assumption is made about the linker path. Note the '-L/sw/
    > lib'. You'll need to track down your copy of libusb (it's probably in /
    > usr/local/lib) and add that to the extra_link_args like so:
    >
    >      extra_link_args = ['-framework',
    >                         'CoreFoundation',
    >                         '-framework',
    >                         'IOKit',
    >                         '-L/sw/lib',
    >                         '-L/usr/local/lib']
    >
    > Run setup again and I bet you'll be off to the races.
    >
    > You should certainly report this to the package maintainer, and you  
    > might also want to point out that the if block starting on line 17 of  
    > setup.py and the if block starting on line 26 are both for OS X, but  
    > they lead to different results!
    >
    > Let us know how it works out,
    > Philip


    Great, that did the job perfectly! Very much appreciated!

    I will write to the code maintainer to let them know the fix; I'm sure
    their are lots of other people out there that will find your
    modification useful.

    Thanks again,
    Dave
    Dr Mephesto, Mar 21, 2009
    #7
  8. On Mar 21, 2009, at 2:49 AM, Dr Mephesto wrote:

    > On Mar 20, 6:23 pm, Philip Semanchuk <> wrote:
    >> So change line 32 in the PyUSB setup.py from this:
    >> extra_compile_args = ['-I/sw/include']
    >> to this:
    >> extra_compile_args = ['-I/sw/include', '-I/usr/local/include']
    >>
    >> The same assumption is made about the linker path. Note the '-L/sw/
    >> lib'. You'll need to track down your copy of libusb (it's probably
    >> in /
    >> usr/local/lib) and add that to the extra_link_args like so:
    >>
    >> extra_link_args = ['-framework',
    >> 'CoreFoundation',
    >> '-framework',
    >> 'IOKit',
    >> '-L/sw/lib',
    >> '-L/usr/local/lib']
    >>
    >> Run setup again and I bet you'll be off to the races.
    >>
    >> You should certainly report this to the package maintainer, and you
    >> might also want to point out that the if block starting on line 17 of
    >> setup.py and the if block starting on line 26 are both for OS X, but
    >> they lead to different results!
    >>
    >> Let us know how it works out,
    >> Philip

    >
    > Great, that did the job perfectly! Very much appreciated!
    >
    > I will write to the code maintainer to let them know the fix; I'm sure
    > their are lots of other people out there that will find your
    > modification useful.


    You're welcome. Glad I could help.

    bye
    Philip
    Philip Semanchuk, Mar 21, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pablo Bleyer Kocik

    [ANN] PyUSB

    Pablo Bleyer Kocik, May 22, 2004, in forum: Python
    Replies:
    0
    Views:
    632
    Pablo Bleyer Kocik
    May 22, 2004
  2. Chris Withers

    Re: Pyusb

    Chris Withers, Oct 14, 2009, in forum: Python
    Replies:
    0
    Views:
    473
    Chris Withers
    Oct 14, 2009
  3. wander.lairson

    PyUSB 1.0.0 alpha 1 release

    wander.lairson, Dec 29, 2010, in forum: Python
    Replies:
    0
    Views:
    415
    wander.lairson
    Dec 29, 2010
  4. Aldo Ceccarelli

    Examples of Programming with PyUSB 1.0?

    Aldo Ceccarelli, Mar 4, 2011, in forum: Python
    Replies:
    1
    Views:
    4,187
    Aldo Ceccarelli
    Mar 4, 2011
  5. John Nagle
    Replies:
    1
    Views:
    411
    Dietmar Schwertberger
    Mar 10, 2012
Loading...

Share This Page