Python 打包 setup.py 通用模版

本文详细介绍了如何使用setuptools和Cython将pyx文件打包为Python扩展模块,包括直接打包pyx文件和涉及C++源码的情况。内容涵盖设置Extension、包含依赖库和头文件目录、编译参数等关键步骤,并提供了打包成so动态库和whl包的命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

setup.py 通用简易模版

快速打包(只有pyx文件)

from setuptools import setup, Extension
import numpy as np # if using cimport numpy in pyx file

extensions = [
	Extension(
		“extention_name”, # name
		sources=[“pyx_file_name.pyx”], # pyx file
		include_dirs=[np.get_include()] # if using cimport numpy
	)
]

setup(ext_modules=cythonize(extensions, language_level=3))

打包cpp/c文件

这个文件也可以是用pyx生成的

from setuptools import setup, Extension

extensions = [
	Extension(
	“test”, # name
	sources=[“test.cpp”], # pyx file
	language='c++',
	include_dirs=['/usr/local/include', np.get_include(), 'some_package/include'], # folders that have all .h files, -I option
	libraries=['EGL', 'glfw'], # -l option
	library_dirs=['usr/local/lib'], # -L option
	extra_compile_args=['-std=c++11'] # other options if needed
	)
]

setup(
	name='test-package',
	version='1.0.0',
	setup_requires=['numpy'],
	install_requires=['numpy>=1.18.0'],
	python_requires='~=3.6', # >= 3.6, < 4.0
	ext_modules=extensions,
	packages=find_packages(), # if has other py files in this package
	include_package_data=True, # to include generated so file to directly use in other py files
	description='test packaging...'
)

打包命令

  • pyx --> cpp
cython -3 --fast-fail -v --cplus file_name.pyx
  • 打包成so文件
python setup.py build_ext --inplace
  • 打包成whl文件
python setup.py sdist bdist_wheel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值