Re: Compiling disutil modules on Windows

Discussion in 'Python' started by Geoff Caplan, Aug 8, 2004.

  1. Geoff Caplan

    Geoff Caplan Guest

    Hi folks,

    I have S├ębastien Sauvage's example extension compiled and working.

    For the record, here is what I did, updated for Python 2.3.

    For the gory details, see:

    http://sebsauvage.net/python/mingw.html

    1) Install MinGW

    - Go to http://www.mingw.org/download.shtml

    - Find the current version of MinGW. At the time of writing, this is
    MinGW-3.1.0-1.exe. It's around 14 megs.

    - Run the install

    - Rename the MinGW directory to gcc, if desired


    2) Install SWIG

    - Go to http://sourceforge.net/project/showfiles.php?group_id=1645

    You are looking for SWIGWIN.

    - rename the installed directory to Swig, if desired


    3) Add gcc and Swig to your Autoexec.bat file

    This is normally found at C:\Autoexec.bat

    Mine looked like this:

    SET PATH=c:\gcc\bin;%PATH%
    SET PATH=c:\Swig;%PATH%

    I rebooted to activate these paths. If you knew Windoze better than
    I do, there is probably a more elegant way to do this.


    4) Copy python23.dll to your Python libs directory

    Search C:\ for python23.dll.

    On Win2000, you will find it in c:\WINNT\system32\

    Copy it to C:\Python23\libs\


    5) Compile a test extension

    ------------------------------------------
    - Create the setup.py file (note the underscore on the extension name:
    this seems to be a requirement)

    # setup.py
    import distutils
    from distutils.core import setup, Extension
    setup(name = "Simple example from the SWIG website",
    version = "2.3",
    ext_modules = [Extension("_example", ["example.i","example.c"])])

    ------------------------------------------
    - Create the example.c file

    /* File : example.c */
    #include <time.h>
    double My_variable = 3.0;

    int fact(int n) {
    if (n <= 1) return 1;
    else return n*fact(n-1);
    }

    int my_mod(int x, int y) {
    return (x%y);
    }

    char *get_time() {
    time_t ltime;
    time(&ltime);
    return ctime(&ltime);

    ------------------------------------------
    - Create the example.i file

    /* example.i */
    %module example
    %{
    /* Put header files here (optional) */
    %}
    extern double My_variable;
    extern int fact(int n);
    extern int my_mod(int x, int y);
    extern char *get_time()

    ------------------------------------------

    - Now launch your dos shell and cd to the directory containing your
    example files.

    - Run the command: python setup.py build -cmingw32

    If all goes well, this will create a directory "build" in the same
    directory as your files. You will find your new dll "_example.pyd"
    under lib.win32

    - Copy the dll to E:\Python23\DLLs\


    6) Run your test extension

    - Launch a Python shell and run:

    >>> import _example
    >>> _example.fact(5)

    120
    >>> _example.get_time()

    'Sun Aug 08 10:09:25 2004\n'

    I tried renaming the extension without the leading underscore but this
    generates an error. I am new to Python: perhaps a SWIG guru could
    explain why the underscore is required, or if not required, how it can
    be avoided?

    Hope someone finds this useful...

    ------------------
    Geoff Caplan
    Vario Software Ltd
    (+44) 121-515 1154
     
    Geoff Caplan, Aug 8, 2004
    #1
    1. Advertising

  2. Geoff Caplan wrote:
    > I tried renaming the extension without the leading underscore but this
    > generates an error. I am new to Python: perhaps a SWIG guru could
    > explain why the underscore is required, or if not required, how it can
    > be avoided?


    I am not a SWIG guru, but I can explain why you cannot rename an
    extension DLL. Inside the DLL, there is an entry point function with
    a name similar to the DLL name. When the DLL is renamed, the entry point
    is not - but Python expects that the entry point then matches the DLL
    name.

    As for the SWIG issue - I believe swig also generates a Python module
    example.py which you are meant to use instead.

    Regards,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 8, 2004
    #2
    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. Geoff Caplan

    Compiling disutil modules on Windows

    Geoff Caplan, Aug 7, 2004, in forum: Python
    Replies:
    3
    Views:
    311
    Neil Hodgson
    Aug 7, 2004
  2. Garrett Cooper
    Replies:
    0
    Views:
    558
    Garrett Cooper
    Feb 24, 2009
  3. Garrett Cooper
    Replies:
    0
    Views:
    500
    Garrett Cooper
    Feb 24, 2009
  4. r2d3
    Replies:
    1
    Views:
    1,396
  5. Ralph Heinkel
    Replies:
    6
    Views:
    918
    Ralph Heinkel
    Mar 22, 2012
Loading...

Share This Page