Detailed view of a page, which is probably more useful for debugging than anything else.
Querying backend directly for 'Python extensions'
| get_pagedata('Python extensions') | |
| _cached_html | TransformedText Object
(
[_type] => PageType_wikitext Object
(
)
[_basepage] => Python extensions
[_content] => Array
(
[0] => <div class="wikitext"><h2>Overview</h2>
<p class="tightenable">As of 2007-05-15, using Python 2.4.4 or 2.5.1 and the MinGW 5.1.3 installer installing "current" (mingw-runtime 3.12, w32api 3.9, binutils 2.16.91-20060119-1, gcc-core 3.4.2-20040916-1), the
[1] => Cached_ExternalLink Object
(
[_url] => http://en.wikibooks.org/wiki/Python_Programming/Extending_with_C#A_minimal_example
[_label] => "minimal example"
)
[2] => Python module can be built easily with MinGW following the same instructions as for Linux, but invoking distutils with <tt>python setup.py build --compiler=mingw32</tt>.</p>
<p class="tightenable">But if you are using a win32 binary distribution of Python 2.3 or older (e.g. the one from www.python.org), then developing Python extensions from a <b>mingw/MSYS</b> (or <b>cygwin</b>) environment requires a few tricks that are not obvious. This is because the python distribution does not come with an export library, so you have to create one, and there's a couple of other gotchas too.</p>
<p class="tightenable">I spent a lot of time looking around for information on how to make this work. The most cited source seems to be
[3] => Cached_ExternalLink Object
(
[_url] => http://sebsauvage.net/python/mingw.html
)
[4] => but for some reason it didn't work for me (just bad luck?). Several other pages exist on the web that give some instructions, but I found that they are either out of date, or wrong, or too difficult to understand. The Python.org document,
[5] => Cached_ExternalLink Object
(
[_url] => http://www.python.org/doc/2.3.4/inst/tweak-flags.html
[_label] => "Installing Python Modules", section 6.2.2
)
[6] => explains the basics, and this is the only one that worked for me, at least with MinGW 3.1.0-1 (MSYS 1.0.10 w/ gcc 3.2.3) and Python 2.3.3.</p>
<p class="tightenable">While looking for something else, I came across this patch request made by
[7] => Cached_ExternalLink Object
(
[_url] => http://mail.python.org/pipermail/python-bugs-list/2004-June/023351.html
[_label] => Connelly (python-Bugs-957198)
)
[8] => that summarizes nicely what to do, and I so wish I had found that earlier, that I decided to create this page. I include the relevant parts of the instructions mentioned in the patch, and I add a few clarifications.</p>
<h2>Setup for Python 2.3 (or older)</h2>
<p class="tightenable">You only need to do this once, every time you change your Python interpreter (i.e. upgrade or re-install):</p>
<ol><li class="tightenable bottom">If your mingw does NOT contain pexports (e.g., do <tt>'which pexports'</tt> from msys command line),
[9] => Cached_ExternalLink Object
(
[_url] => http://starship.python.net/crew/kernr/mingw32/pexports-0.42h.zip
[_label] => download it
)
[10] => and extract <tt>pexports.exe</tt> to your Mingw <tt>bin</tt> directory.</li>
<li class="tightenable top bottom">Find <tt>pythonxx.dll</tt>. On my machine, with Python 2.3 it is in <tt>/c/WINNT/system32</tt>, as <tt>python23.dll</tt>. It has been mentioned in several web pages that it could also be in <tt>/yourPythonRoot/libs</tt>.</li>
<li class="tightenable top bottom">cd to <tt>/yourPythonRoot/libs</tt>. E.g. on my system I cd to <tt>/c/Python23/libs</tt>.</li>
<li class="tightenable top"><p class="tightenable top">Assuming <tt>pythonxx.dll</tt> is in <tt>/c/WINNT/system32</tt>, do the following (from <tt>/yourPythonDir/libs</tt>; also, adjust the version number to your version of Python):</p>
<pre class="tightenable bottom">
pexports /c/WINNT/system32/python23.dll > python23.def
dlltool --dllname python23.dll --def python23.def --output-lib libpython23.a</pre>
</li>
</ol>
<h2>Building an extension</h2>
<p class="tightenable">You are now ok to build python extensions. E.g. if your extension is in <tt>example.c</tt>,</p>
<pre class="tightenable">
gcc -c example.c -I/c/Python23/include
gcc -shared example.o -L/c/Python23/libs -lpython23 -o example.dll</pre>
<p class="tightenable">If you haven't done the above Basic Setup steps correctly, you could get several kinds of errors:</p>
<ol><li class="tightenable bottom">The link stage (<tt>gcc -shared ...</tt>) could fail, saying the python23 library was not found, is not the correct format, there are undefined symbols;</li>
<li class="tightenable top">When you run python and import your module, you may get a <tt>"Fatal Python error: Interpreter not initialized (version mismatch?)"</tt> and the interpreter will exit.</li>
</ol>
<p class="tightenable">If you use <b>SWIG</b>, remember that your dll extension must start with an underscore, because swig wraps your dll with Python code to make it more robust. E.g. the above example would become:</p>
<pre class="tightenable">
swig -python example.i
gcc -c example_wrap.c example.c -I/c/Python23/include
gcc -shared example.o example_wrap.o -L/c/Python23/libs -lpython23 -o _example.dll</pre>
<h2>Creating an extension with Distutils</h2>
<p class="tightenable">You must specify which compiler to use, so you would do:</p>
<pre class="tightenable">
python setup.py build --compiler=mingw32</pre>
<p class="tightenable">and then:</p>
<pre class="tightenable">
python setup.py install</pre>
<p class="tightenable">Note this
[11] => Cached_ExternalLink Object
(
[_url] => http://mail.python.org/pipermail/distutils-sig/2004-October/004194.html
[_label] => interesting post
)
[12] => on the distutils-sig mailing list:</p>
<pre class="tightenable">
Thomas Heller wrote:
> Scott David Daniels <Scott.Daniels at Acm.Org> writes:
>>I am having a bit of trouble creating a windows distributable
>>with a C module using MingW32. While I can do:
>> python setup.py build --compile=mingw32
>> python setup.py bdist_wininst --skip-build
>>I get an executable with no tag for the particular python version.
>
> You could use command chaining, if that's the correct word for that:
> python setup.py build --compiler=mingw32 bdist_wininst
This does the trick, both for using the mingw32 compiler, and for
tagging the result with the python version. Thanks for the help.
-- Scott David Daniels</pre>
<p class="tightenable">If you are wondering what the <tt>setup.py</tt> file looks like, here is an example for a C extension, YMMV:</p>
<pre class="tightenable">
# setup.py
from distutils.core import setup, Extension
setup (name = "_example",
version = "1.0",
maintainer = "Your Name",
maintainer_email = "your.name@domain.org",
description = "Sample Python C Extension",
ext_modules = [Extension('_example',
extra_compile_args=['-O2'],
library_dirs=['/mingw/lib'],
sources=['example.c','example_wrap.c'])])</pre>
<p class="tightenable">And here is an example for C++, YMMV:</p>
<pre class="tightenable">
# setup.py
from distutils.core import setup, Extension
setup (name = "_example",
version = "1.0",
maintainer = "Your Name",
maintainer_email = "your.name@domain.org",
description = "Sample Python C++ Extension",
ext_modules = [Extension('_example',
extra_compile_args=['-O2'],
library_dirs=['/mingw/lib'],
libraries=['supc++'],
sources=['example.cxx','example_wrap.cxx'])])</pre>
<h2>Extra Setup for Distutils in Python 2.3 (or older)</h2>
<p class="tightenable">You may want to use <b>distutils</b> to distribute or even just build your python extensions. If you are using Python 2.3 or older and do NOT have MSVC installed on your machine, then you need to patch <b>distutils</b> because it thinks your platform is nt, and the nt compiler defaults to MSVC (I haven't yet found how to say in <tt>setup.py</tt> that you want the 'unix' compiler type). So:</p>
<ol><li class="tightenable bottom">Locate <tt>msvccompiler.py</tt> in your Python distribution. On my machine, it is in <tt>/c/Python23/Lib/distutils</tt>.</li>
<li class="tightenable top bottom">Open it</li>
<li class="tightenable top"><p class="tightenable top">Find and DELETE the following lines (around line 211):</p>
<pre class="tightenable bottom">
if len (self.__paths) == 0:
raise DistutilsPlatformError, ("Python was built with version %s of Visual Studio, "
"and extensions need to be built with the same "
"version of the compiler, but it isn't installed." % self.__version)</pre>
</li>
</ol>
<p class="tightenable">You will notice that if you don't apply this patch, the above exception is raised when you do <tt>python setup.py install</tt>, because the condition is true when MSVC is not installed (the path doesn't contain anything).</p>
<p class="tightenable">NOTE: (added by Chris Barker).
Rather than editing the distutils code, you can just do:</p>
<p class="tightenable"><tt>python setup.py install --skip-build</tt></p>
<p class="tightenable">By skipping the build stage, you seem to skip the compiler check.
END NOTE</p>
<p class="tightenable">If you want to make <b>C++</b> extensions, you will likely have to create <tt>/mingw/bin/cc.exe</tt>. You will know, because the setup.py build will give an error about <tt>cc.exe</tt> not found. On my system, I have found that I had to create it by copying from <tt>/mingw/bin/dllwrap.exe</tt>, but the original Connelly post says to copy from <tt>/mingw/bin/gcc.exe</tt>, so maybe it depends on the version of gcc or mingw you have. Naturally, creating <tt>cc.exe</tt> only has to be done once, forever.</p>
<h2>Extra Setup for Swig/autotools</h2>
<p class="tightenable">If you have configure scripts that use AM_PATH_PYTHON or PYTHON_DEVEL (from Swig),
there's a bit more that you need to do if you want them working in MinGW.</p>
<p class="tightenable">First, make sure there is nothing from cygwin in your $PATH variable. Make a
/usr/local/bin (*) if you don't have one already and put that in your $PATH. Make
a file called 'python' in /usr/local/bin that contains simply:</p>
<pre class="tightenable">
#!/bin/sh
/f/Software/Python-242/python.exe "$@"</pre>
<p class="tightenable">replacing /f/Software/Python-242 with /yourPythonRoot/</p>
<p class="tightenable">On my machine, after doing this, AM_PATH_PYTHON was able to find the script and extension directories
in /f/Software/Python-242/Lib/, but not the includes or the libraries. So</p>
<pre class="tightenable">
cp -r /yourPythonRoot/include /usr/local/include/python2.4
cp -r /yourPythonRoot/Lib /usr/local/lib/python2.4
cp -r /yourPythonRoot/libs /usr/local/lib/python2.4/config</pre>
<p class="tightenable">You'll also need a dot in the version spec for the library you created above with dlltool, so (for example):</p>
<pre class="tightenable">
cp /usr/local/lib/python2.4/config/libpython24.a /usr/local/lib/python2.4/config/libpython2.4.a</pre>
<p class="tightenable">Then things should be peachy.</p>
<p class="tightenable">--
[13] => Cached_WikiLink Object
(
[_page] => TrentApted
)
[14] => 2005-10-27</p>
<h2>Python2.4 and MSVCR71</h2>
<p class="tightenable">This seems to be widely discussed with no clear answer.<br />
One approach is to just follow the directions above.<br />
If this doesn't work, or bothers you, then check these pages:<br />
[15] => Cached_ExternalLink Object
(
[_url] => http://article.gmane.org/gmane.comp.python.general/378028/match=msvcr71
)
[16] =>
[17] => Cached_ExternalLink Object
(
[_url] => http://jove.prohosting.com/iwave/ipython/issues.html
)
[18] => (section 4 - Extending the official distribution)</p>
<p class="tightenable bottom">If you run depends.exe (download it with Microsoft's platform SDK) on the windows binary (python.exe):<br /></p>
<pre class="tightenable top bottom">
Python.exe depends on: python24.dll, msvcr71.dll, and kernel32.dll
python24.dll depends on: kernel32.dll, user32.dll, advapi32.dll, shell32.dll, and msvcr71.dll
shell32.dll depends on: msvcrt.dll (and others)</pre>
<p class="tightenable top">If nothing else works, you can download Microsoft's toolkit and follow these directions:
[19] => Cached_ExternalLink Object
(
[_url] => http://www.vrplumber.com/programming/mstoolkit/
)
[20] => (Note that the author of this page gave up)</p>
<p class="tightenable">--
[21] => Cached_WikiLink Object
(
[_page] => KenStaton
)
[22] => 22Nov2005</p>
<h2>One Last Step...</h2>
<p class="tightenable top bottom">This may help some, it worked for me (I'm using distutils) for Python24xx and now for Python25rc1.
It requires the minimum setup and does not modify any Python files.</p>
<ol><li class="tightenable top bottom">Follow the instructions above under "Basic Setup" to create a compatable library.</li>
<li class="tightenable top bottom">Create a cfg file for distutils (distutils.cfg) eg: /c/Python2x/Lib/distutils/distutils.cfg</li>
</ol>
<p class="tightenable top bottom">containing:<br /></p>
<pre class="tightenable top bottom">
[build]
compiler=mingw32</pre>
<p class="tightenable top">Done. For whatever reason, using "--compiler=mingw32" never worked for me.</p>
<p class="tightenable bottom">--
[23] => Cached_WikiLink Object
(
[_page] => ShaneYuile
)
[24] => 2006-Sep-11</p>
</div>
)
[_description] => But if you are using a win32 binary distribution of Python 2.3 or older (e.g. the one from www.python.org), then developing Python extensions from a mingw/MSYS (or cygwin) environment requires a few tricks that are not obvious. This is because the python distribution does not come with an export library, so you have to create one, and there's a couple of other gotchas too.
)
|
| date | 1101768543 |
| hits | 50322 |
| get_versiondata('Python extensions',10) | |
| %content | !!!Overview As of 2007-05-15, using Pyt ... |
| author | UnDefined |
| author_id | UnDefined |
| is_minor_edit | false |
| markup | 2 |
| mtime | 1179262970 |
| pagetype | wikitext |
| summary | update for python >= 2.4, but leave and mark historical notes (python <= 2.3 ) as such |
| get_links('Python extensions') | |
| 0 | KenStatonArray |
| 1 | TrentAptedArray |
| get_backlinks('Python extensions') | |
| 0 | FAQArray |
| 1 | RealDumbQuickstartArray |


