Search code examples
pythoncpy2exerecipe

build python program with extensions using py2exe


I'm having a hard time finding py2exe recipes, especially for cases that require c extensions.

The following recipe works fine without the "ext_modules" part. With it I get "NameError: name 'Extension' is not defined.

from distutils.core import setup
import py2exe
import matplotlib
import os

s = os.popen('svnversion')
version = s.read()
f = open('cpa_version.py', 'w')
f.write('VERSION = "%s"\n'%(version.strip()))
f.close()

setup(console=['cpa.py'],
      options={
        'py2exe': {
            'packages' : ['matplotlib', 'pytz', 'MySQLdb', 'pysqlite2'],
            'includes' : ['PILfix', 'version'],
            "excludes" : ['_gtkagg', '_tkagg',
                          "Tkconstants","Tkinter","tcl"],
            "dll_excludes": ['libgdk-win32-2.0-0.dll',
                             'libgobject-2.0-0.dll', 
                             'libgdk_pixbuf-2.0-0.dll',
                             'tcl84.dll', 'tk84.dll']
            }
        },
      data_files=matplotlib.get_py2exe_datafiles(),
      # how to build _classifier.c???
      ext_modules = [Extension('_classifier',
                               sources = ['_classifier.c'],
                               include_dirs=[numpy.get_include()],
                               libraries = ['sqlite3'])]
)

_classifier.c includes the following


#include "sqlite3.h"
#include "Python.h"
#include "numpy/arrayobject.h"
#include <stdio.h>

any help would be greatly appreciated.


Solution

  • After fixing the small error created by forgetting to import Extension, I ran into other errors stating a problem with the -lsqlite3 flag. Turns out I needed to follow the steps outlined here: http://cboard.cprogramming.com/cplusplus-programming/82135-sqlite-questions.html

    1. Download sqlitedll-3_3_7.zip and sqlite-source-3_3_7.zip from sqlite.org/download.html
    2. Extract sqlitedll-3.3.7.zip and then run from the command line:

      dlltool -D sqlite3.dll -d sqlite3.def -l libsqlite3dll.a

    3. Place libsqlite3dll.a (just created) in the MinGW lib directory.
    4. Place sqlite3.dll in your system path (c:\Windows\System32\ worked for me)
    5. Extract sqlite-source-3_3_7.zip and place sqlite3.h in your MinGW include directory.
    6. When you link, you will need to supply the parameter: -lsqlite3dll (this meant changing libraries=['sqlite3'] to libraries=['sqlite3dll'])

    ...After that the build worked.

    Here's the setup file again:

    from distutils.core import setup, Extension
    import py2exe
    import matplotlib
    import os
    import numpy
    
    setup(console=['cpa.py'],
          options={
            'py2exe': {
                'packages' : ['matplotlib', 'pytz', 'MySQLdb', 'pysqlite2'],
                'includes' : ['PILfix', 'version'],
                "excludes" : ['_gtkagg', '_tkagg',
                              "Tkconstants","Tkinter","tcl"],
                "dll_excludes": ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll', 
                                 'libgdk_pixbuf-2.0-0.dll',
                                 'tcl84.dll', 'tk84.dll']
                }
            },
          data_files=matplotlib.get_py2exe_datafiles(),
          ext_modules = [Extension('_classifier',
                                   sources = ['_classifier.c'],
                                   include_dirs=[numpy.get_include()],
                                   libraries = ['sqlite3dll'])]
    )