python 编译so库_打包仅使用Cython编译的python库的二进制编译.so文件

我有一个名为mypackinside 的包,里面有一个模块mymod.py,而且__init__.py.出于某种原因,我不需要辩论,我需要打包这个模块(也不允许使用.py或.pyc文件).也就是说,它__init__.py是分布式压缩文件中允许的唯一源文件.

文件夹结构是:

.

?

??? mypack

? ??? __init__.py

? ??? mymod.py

??? setup.py

我发现Cython能够通过转换.so库中的每个.py文件来实现这一点,该文件可以直接用python导入.

问题是:setup.py文件必须如何才能轻松打包和安装?

目标系统有一个virtualenv,其中必须使用允许轻松安装和卸载的任何方法安装软件包(easy_install,pip等都是受欢迎的).

我尝试了所有触手可及的东西.我阅读setuptools和distutils文档,计算器所有相关的问题,并试图与所有类型的命令(sdist,bdist,bdist_egg等),有很多setup.cfg和MANIFEST.in文件条目的组合.

我得到的最接近的是下面的安装文件,它将子类化bdist_egg命令以删除.pyc文件,但这会破坏安装.

如果覆盖了正确安装中包含的所有辅助文件(我需要pip freeze在venv中运行并查看mymod==0.0.1),那么在venv中

"手动"安装文件的解决方案也很好.

运行它:

python setup.py bdist_egg --exclude-source-files

和(尝试)安装它

easy_install mymod-0.0.1-py2.7-linux-x86_64.egg

您可能会注意到,目标是使用python 2.7的linux 64位.

from Cython.Distutils import build_ext

from setuptools import setup, find_packages

from setuptools.extension import Extension

from setuptools.command import bdist_egg

from setuptools.command.bdist_egg import walk_egg, log

import os

class my_bdist_egg(bdist_egg.bdist_egg):

def zap_pyfiles(self):

log.info("Removing .py files from temporary directory")

for base, dirs, files in walk_egg(self.bdist_dir):

for name in files:

if not name.endswith('__init__.py'):

if name.endswith('.py') or name.endswith('.pyc'):

# original 'if' only has name.endswith('.py')

path = os.path.join(base, name)

log.info("Deleting %s",path)

os.unlink(path)

ext_modules=[

Extension("mypack.mymod", ["mypack/mymod.py"]),

]

setup(

name = 'mypack',

cmdclass = {'build_ext': build_ext,

'bdist_egg': my_bdist_egg },

ext_modules = ext_modules,

version='0.0.1',

description='This is mypack compiled lib',

author='Myself',

packages=['mypack'],

)

UPDATE.在@Teyras回答之后,可以按照答案中的要求构建一个轮子.该setup.py文件内容如下:

import os

import shutil

from setuptools.extension import Extension

from setuptools import setup

from Cython.Build import cythonize

from Cython.Distutils import build_ext

class MyBuildExt(build_ext):

def run(self):

build_ext.run(self)

build_dir = os.path.realpath(self.build_lib)

root_dir = os.path.dirname(os.path.realpath(__file__))

target_dir = build_dir if not self.inplace else root_dir

self.copy_file('mypack/__init__.py', root_dir, target_dir)

def copy_file(self, path, source_dir, destination_dir):

if os.path.exists(os.path.join(source_dir, path)):

shutil.copyfile(os.path.join(source_dir, path),

os.path.join(destination_dir, path))

setup(

name = 'mypack',

cmdclass = {'build_ext': MyBuildExt},

ext_modules = cythonize([Extension("mypack.*", ["mypack/*.py"])]),

version='0.0.1',

description='This is mypack compiled lib',

author='Myself',

packages=[],

include_package_data=True )

关键是要设置packages=[],.需要覆盖build_ext类run方法才能将__init__.py文件放在方向盘内.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值