All Python examples and tests have been written to be both Python 2 and Python 3
compatible, removing the need for 2to3 to run the examples or test-suite.
The 2to3 executable is not always available and even when available does not
always work, e.g. with pyenv. An alternative would be to use the lib2to3 Python
module instead, but this isn't available in some older versions of Python 3.
I had this problem on Ubuntu Bionic on Travis:
checking Examples/python/callback
pyenv: 2to3-3.8: command not found
The `2to3-3.8' command exists in these Python versions:
3.8
3.8.1
Reference issues:
https://github.com/pypa/virtualenv/issues/1399
https://travis-ci.community/t/2to3-command-not-found-in-venv-in-bionic/4495
99 lines
3 KiB
HTML
99 lines
3 KiB
HTML
<html>
|
|
<head>
|
|
<title>SWIG:Examples:python</title>
|
|
</head>
|
|
|
|
<body bgcolor="#ffffff">
|
|
<H1>SWIG Python Examples</H1>
|
|
|
|
<p>
|
|
The following examples illustrate the use of SWIG with Python.
|
|
|
|
<ul>
|
|
<li><a href="simple/index.html">simple</a>. A minimal example showing how SWIG can
|
|
be used to wrap a C function, a global variable, and a constant.
|
|
<li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
|
|
certain C declarations are turned into constants.
|
|
<li><a href="variables/index.html">variables</a>. An example showing how to access C global variables from Python.
|
|
<li><a href="value/index.html">value</a>. How to pass and return structures by value.
|
|
<li><a href="class/index.html">class</a>. Wrapping a simple C++ class.
|
|
<li><a href="reference/index.html">reference</a>. C++ references.
|
|
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
|
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
|
</ul>
|
|
|
|
<h2>Compilation Issues</h2>
|
|
|
|
<ul>
|
|
<li>To create a Python extension, SWIG is run with the following options:
|
|
|
|
<blockquote>
|
|
<pre>
|
|
% swig -python interface.i
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<li>
|
|
Please see the <a href="../../Doc/Manual/Windows.html">Windows</a> page in the main manual for information on using the examples on Windows. <p>
|
|
</li>
|
|
|
|
<li>On Unix the compilation of examples is done using the file <tt>Example/Makefile</tt>. This
|
|
makefile performs a manual module compilation which is platform specific. Typically,
|
|
the steps look like this (Linux):
|
|
|
|
<blockquote>
|
|
<pre>
|
|
% swig -python interface.i
|
|
% gcc -fpic -c interface_wrap.c -I/usr/local/include/python1.5
|
|
% gcc -shared interface_wrap.o $(OBJS) -o interfacemodule.so
|
|
% python
|
|
Python 1.5.2 (#3, Oct 9 1999, 22:09:34) [GCC 2.95.1 19990816 (release)] on linux2
|
|
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
|
|
>>> import interface
|
|
>>> interface.blah(...)
|
|
...
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<li>The politically "correct" way to compile a Python extension is to follow the steps
|
|
described at <a href="http://docs.python.org/2.0/ext/building-on-unix.html">www.python.org</a>:
|
|
|
|
<p>
|
|
<ol>
|
|
<li>Create a file called <tt>Setup</tt> that looks like the following where $(SRCS) is filled
|
|
in with any other source files you need to build the extension:
|
|
|
|
<blockquote>
|
|
<pre>
|
|
*shared*
|
|
interface interface_wrap.c $(SRCS)
|
|
</pre>
|
|
</blockquote>
|
|
<li>Copy the file <tt>Makefile.pre.in</tt> from the Python distribution. Usually it's located
|
|
in the directory <tt>/usr/local/lib/python1.5/config</tt> on a Unix machine.
|
|
|
|
<p>
|
|
<li>Type the following to build the extension:
|
|
|
|
<blockquote>
|
|
<pre>
|
|
% make -f Makefile.pre.in boot
|
|
% make
|
|
</pre>
|
|
</blockquote>
|
|
<li> And that's it. If you are preparing an extension for distribution, you may want
|
|
to look at the <a href="http://www.python.org/sigs/distutils-sig/">distutils</a>.
|
|
</ol>
|
|
</ul>
|
|
|
|
<h2>Compatibility</h2>
|
|
|
|
For Python 3, set the environment variable <tt>PY3=1</tt>.
|
|
|
|
<p>
|
|
Your mileage may vary. If you experience a problem, please let us know by
|
|
contacting us on the <a href="http://www.swig.org/mail.html">mailing lists</a>.
|
|
</body>
|
|
</html>
|
|
|
|
|