Cython编译python packages


Compile python package into a dynamic link library (.so in linux, .dll in windows) like C language.


A setup.py example


A python package named testpackage:

tree testpackage

Files Directory:
testpackage/
├── __init__.py
├── module1.pyx
├── module2.pyx
├── module3.pyx
└── extc
    ├── calc.c
    └── calc_ext1.c

install file setup.py for testpackage

snippet.python
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Description for the python packages. ## Could be accessed by testpackage.__doc__
"""
 
import sys
from distutils.core import setup,Extension
from Cython.Build import cythonize
 
package_list= ["testpackage"]
package_dir = {"testpackage": "testpackage"}
 
## pure C extension
permutation = Extension("testpackage.extc.calc",
		["testpackage/extc/calc.c",
			"testpackage/extc/calc_ext1.c",
			])
 
## install options
metadata = {
		"name":"testpackage",
		"version":"1.0.0a",
		'description': "testpackage",
		'long_description': __doc__,
		'author': "rongzhengqin",
		'author_email': "rongzhengqin@basepedia.com",
		'license': "MIT",
		'platforms': ["Linux","Mac OS-X","UNIX"],
		'url': "www.basepedia.com",
		'packages': package_list,
		'package_dir': package_dir,
		'data_files': [],
		'requires': ['cython (>=0.22)'],
		'ext_modules': cythonize("*/*.pyx")+[extc,],
		}
 
if __name__ == '__main__':
	dist = setup(**metadata)
  • 公共/tech/cython编译python_packages.txt
  • 最后更改: 8年前
  • 由 rongzhengqin