Distutils appharently fails with a (working) SWIG extension

拜拜、爱过 提交于 2019-12-05 03:52:57

Practically, there are two options for distribution of such package: bdist_wheel and sdist.

Let's take SWIG's example from the docs for distutils.

example.h

int fact(int n);

example.c

#include "example.h"

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

example.i

%module example

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

int fact(int n);

Let's build a SWIG interface, which by default produces example_wrap.c.

swig -python example.i 

Wheel

Python Wheels is a modern distribution format which simplifies distribution of pre-built packages (i.e. users of the package will not need a compiler, development headers and the like to install it). To use it we'll need setuptools and wheel (can be installed with pip or from OS repositories, sudo apt-get install python-wheel python-setuptools).

setup.py

from setuptools import setup, Extension


setup(
  name = 'example',
  version = '0.1',
  author = 'SWIG Docs',
  description = 'Simple swig example from docs',
  ext_modules = [
    Extension('_example', sources = ['example_wrap.c', 'example.c'])],
  py_modules = ['example'],
  package_data = {'': ['example.h']}  # needed for sdist
)

You can build the wheel with:

python setup.py bdist_wheel

On my machine it produces example-0.1-cp27-cp27mu-linux_x86_64.whl which I can install with pip and test like python -c 'import example; print(example.fact(5))'. Note the filename. It encodes compatible Python version, ABI and platform. Here's listing of the contents (unzip -l ...).

Archive:  example-0.1-cp27-cp27mu-linux_x86_64.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
     2609  2018-02-24 11:06   example.py
    70968  2018-02-24 13:31   _example.so
       10  2018-02-24 14:58   example-0.1.dist-info/DESCRIPTION.rst
      290  2018-02-24 14:58   example-0.1.dist-info/metadata.json
       17  2018-02-24 14:58   example-0.1.dist-info/top_level.txt
      105  2018-02-24 14:58   example-0.1.dist-info/WHEEL
      193  2018-02-24 14:58   example-0.1.dist-info/METADATA
      617  2018-02-24 14:58   example-0.1.dist-info/RECORD
---------                     -------
    74809                     8 files

To build wheels with better compatibility, you can, for instance, look at manylinux.

Source

sdist stands for source distribution, which means your users will need to have a compiler and relevant development dependencies. Source distribution is built with:

python setup.py sdist

Produced example-0.1.tar.gz contains (tar -tf ...):

example-0.1/
example-0.1/example.c
example-0.1/PKG-INFO
example-0.1/example.egg-info/
example-0.1/example.egg-info/PKG-INFO
example-0.1/example.egg-info/dependency_links.txt
example-0.1/example.egg-info/top_level.txt
example-0.1/example.egg-info/SOURCES.txt
example-0.1/setup.cfg
example-0.1/example.py
example-0.1/setup.py
example-0.1/example_wrap.c
example-0.1/example.h
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!