Size: 2832
Comment:
|
← Revision 11 as of 2008-12-12 20:44:07 ⇥
Size: 2883
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
Install [http://www.python.org Python] (we used version 2.4.2) | Install [[http://www.python.org|Python]] (we used version 2.4.2) |
Line 9: | Line 9: |
Run the windows installer for [http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/) Pyrex] (e.g. {{{Pyrex-0.9.3.1.win32.exe}}}) | Run the windows installer for [[http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/|Pyrex]] (e.g. `Pyrex-0.9.3.1.win32.exe`) |
Line 12: | Line 12: |
Install [http://www.mingw.org/download.shtml Mingw], the gcc compiler for Windows (download and run {{{MinGW-5.0.2.exe}}} and choose only the "base tools": {{{mingw-runtime 3.9, w32api-3.6, binutils 2.15.91 and gcc-core 3.4.2}}}. Add Mingw path {{{C:\MinGW\bin}}} to the Windows {{{Path}}} environment variable. If you already have cygwin installed, add {{{C:\MinGW\bin}}} ''before'' the Cygwin path. |
Install [[http://www.mingw.org/download.shtml|Mingw]], the gcc compiler for Windows (download and run {{{MinGW-5.1.2.exe}}} and choose only the "base tools": `mingw-runtime 3.9`, `w32api-3.6`, `binutils 2.15.91` and `gcc-core 3.4.2`. Add Mingw path (`C:\MinGW\bin`) to the Windows `Path` environment variable. If you already have cygwin installed, add `C:\MinGW\bin` before the Cygwin path. |
Line 16: | Line 16: |
Edit the file {{{c:/Python2x/Lib/distutils/distutils.cfg}}} (if it does not exist, create it) and add the following lines: {{{[build] compiler = mingw32}}} |
Edit the file `c:/Python2x/Lib/distutils/distutils.cfg` (if it does not exist, create it) and add the following lines: {{{ [build] compiler = mingw32 }}} |
Line 21: | Line 23: |
In order to make minGW link with {{{msvcr71.dll}}}, edit the text file {{{c:\mingw\lib\gcc\mingw32\3.2.4\specs}}} and change {{{-lmsvcrt}}} to {{{-lmsvcr71}}}. Now MinGW will link with the same C runtime (CRT) as Python 2.4. |
MinGW links with `msvcrt.dll` whereas the main Python distribution links with `msvcr71.dll` (due to Visual C++ 2003). It is not safe to mix and blend these two different C runtime libraries. In order to make minGW link with `msvcr71.dll`, edit the text file `c:\mingw\lib\gcc\mingw32\3.2.4\specs` and change `-lmsvcrt` to `-lmsvcr71`. ----------- |
Line 27: | Line 28: |
Create a working directory (e.g. {{{D:\pyrex_module\}}}). Write a pyrex module and save it with a {{{pyx}}} extension (e.g. {{{primes.pyx}}}, code available [http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/) here]) | Create a working directory (e.g. `D:\pyrex_module\`). Write a pyrex module and save it with a `pyx` extension (e.g. `primes.pyx`, code available [[http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/|here]]) |
Line 30: | Line 31: |
Write the following python script and save it as {{{setup.py}}} in your working directory. | Write the following python script and save it as `setup.py` in your working directory. |
Line 32: | Line 33: |
{{{from distutils.core import setup | {{{ from distutils.core import setup |
Line 41: | Line 43: |
)}}} | ) }}} |
Line 43: | Line 46: |
If you want to compile several modules, duplicate the line starting with {{{Extension}}} and replace {{{primes}}} by your module names. | If you want to compile several modules, duplicate the line starting with `Extension` and replace `primes` by your module names. |
Line 46: | Line 49: |
In your working directory, create a batch file called {{{build_and_install.bat}}} containing the following lines. To run the batch, double-click the file. You will see many "Warning" messages during the building process: do not worry, it is normal. | In your working directory, create a batch file called `build_and_install.bat` containing the following lines. To run the batch, double-click the file. You will see many "Warning" messages during the building process: do not worry, it is normal. |
Line 48: | Line 51: |
{{{C:\Python24\python.exe setup.py build_ext install pause}}} |
{{{ C:\Python24\python.exe setup.py build_ext install pause }}} |
Line 52: | Line 57: |
Mission completed. The file {{{primes.pyd}}} (a {{{pyd}}} is a Python Extension DLL) is now located in {{{C:\Python24\Lib\site-packages}}} and the {{{primes}}} module is available in Python. In your working directory, you can delete the file {{{primes.c}}} and the {{{build}}} folder created by the building process. | Mission completed. The file `primes.pyd` (a Python Extension DLL) is now located in `C:\Python24\Lib\site-packages` and the `primes` module is available in Python. In your working directory, you can delete `primes.c` and the `build` folder created by the building process. |
Line 56: | Line 61: |
{{{>>> import primes | {{{ >>> import primes |
Line 58: | Line 64: |
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]}}} | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] }}} |
Step-by-step guide to install Pyrex on Windows and compile your first extension
A: Pyrex installation on Windows XP
step A.1
Install Python (we used version 2.4.2)
step A.2
Run the windows installer for Pyrex (e.g. Pyrex-0.9.3.1.win32.exe)
step A.3
Install Mingw, the gcc compiler for Windows (download and run MinGW-5.1.2.exe and choose only the "base tools": mingw-runtime 3.9, w32api-3.6, binutils 2.15.91 and gcc-core 3.4.2. Add Mingw path (C:\MinGW\bin) to the Windows Path environment variable. If you already have cygwin installed, add C:\MinGW\bin before the Cygwin path.
step A.4
Edit the file c:/Python2x/Lib/distutils/distutils.cfg (if it does not exist, create it) and add the following lines:
[build] compiler = mingw32
step A.5
MinGW links with msvcrt.dll whereas the main Python distribution links with msvcr71.dll (due to Visual C++ 2003). It is not safe to mix and blend these two different C runtime libraries. In order to make minGW link with msvcr71.dll, edit the text file c:\mingw\lib\gcc\mingw32\3.2.4\specs and change -lmsvcrt to -lmsvcr71.
B: Create your first Pyrex module
step B.1
Create a working directory (e.g. D:\pyrex_module\). Write a pyrex module and save it with a pyx extension (e.g. primes.pyx, code available here)
step B.2
Write the following python script and save it as setup.py in your working directory.
from distutils.core import setup from distutils.extension import Extension from Pyrex.Distutils import build_ext setup( name = "PyrexGuide", ext_modules=[ Extension("primes", ["primes.pyx"]) ], cmdclass = {'build_ext': build_ext} )
If you want to compile several modules, duplicate the line starting with Extension and replace primes by your module names.
step B.3
In your working directory, create a batch file called build_and_install.bat containing the following lines. To run the batch, double-click the file. You will see many "Warning" messages during the building process: do not worry, it is normal.
C:\Python24\python.exe setup.py build_ext install pause
step B.4
Mission completed. The file primes.pyd (a Python Extension DLL) is now located in C:\Python24\Lib\site-packages and the primes module is available in Python. In your working directory, you can delete primes.c and the build folder created by the building process.
You can now test your new module at the python shell:
>>> import primes >>> primes.primes(10) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]