Generating a Python 3.3.0 Wrapper with SWIG 2.0.9

With a few steps, you can build your own Python 3.3.0 wrapper.

On Windows:

Building LeapPython.pyd on Windows (32-bit Python 3.3.0 for this example) :

  1. Install Visual Studio (any of 2008, 2010, or 2012 should be fine), Python 3.3.0 for Windows x86, and swig-2.0.9.

  2. Create an empty C++ project. Copy Leap.h, LeapMath.h, Leap.i, and Leap.lib (x86) into this project folder.

  3. From that folder, run SWIG to generate LeapPython.cpp. For example: c:\leap_python33>"C:\Program Files (x86)\swigwin-2.0.9\swig.exe" -c++ -python -o LeapPython.cpp -interface LeapPython Leap.i

  4. Open up the project properties, select the Release configuration, and go to the Configuration Properties -> General page. From there, set the 'Target Name' to LeapPython and set the 'Configuration Type' to 'Dynamic Library (.dll)'. (screenshot)

  5. Go to to the C/C++ -> General property page. Add the path containing Python.h, typically C:\Python33\include. (screenshot)

  6. Go to the Linker -> Input property page. Add Leap.lib and the full path to python33.lib, typically C:\Python33\libs\python33.lib. (screenshot)

  7. Press F7 to build. (If you hit errors such as missing symbols, this tends to come from accidentally omitting one library or mixing x86 with x64. Double-check that you have the correct Leap.lib and python33.lib (from 32-bit Python) and are building a Win32 configuration.)

  8. Rename the output LeapPython.dll to LeapPython.pyd

If you need 64-bit Python, create the x64 configuration in Visual Studio and change some of the above steps to use x64 libraries accordingly.

The process on Mac OS X is generally easier.

On Mac:

Building on Mac :

  1. Install XCode along with its command-line tools, Python 3.3.0 for Mac, and swig-2.0.9 (likely using MacPorts).

  2. Copy Leap.h, LeapMath.h, Leap.i, and libLeap.dylib into one folder.

  3. Generate LeapPython.cpp withswig -c++ -python -o LeapPython.cpp -interface LeapPython Leap.i

  4. Compile and link with clang++ -arch i386 -arch x86_64 -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m LeapPython.cpp libLeap.dylib /Library/Frameworks/Python.framework/Versions/3.3/lib/libpython3.3.dylib -shared -o

Running the Sample Python app:

You can take your resulting LeapPython.pyd ( on Mac) and run the Sample application alongside and Leap.dll (libLeap.dylib on Mac). You'll naturally have to make some changes to adhere to Python 3, such as parentheses around arguments to the 'print' function and replacing the line using the '/=' operator with: pos = pos.__div__(numFingers)

Mac command-line:

`$ python3


Press Enter to quit...


Frame id: 98019, timestamp: 909519145, hands: 0, fingers: 0, tools: 0


On Linux:

Please see Akishin's tutorial on building LeapPython for Python 3+ on Ubuntu


In the attached zip file I have included sample wrappers built with the above procedure, as well as a modified for Python 3.

Our hope is that this approach will not only work for users interested in Python 3, but also serve as a solution for those who have requested other versions of Python for which we don't yet provide binaries. For example, we have heard requests for Python 2.7.1, Python 2.6.2, and even alternative builds of Python 2.7.3. We're looking for ways to best meet the needs of our Python users without letting this turn into a maintenance nightmare of Python versioning.

Have more questions? Submit a request


Article is closed for comments.