昨天创建了一个简单的python 集成cython 的项目 master 但是有几个问题
目前的构建时基于make 同时需要本地执行,为了方便基于pip 的安装,做了如下调整

项目准备

项目使用venv 管理环境,初始化命令 python3 -m venv .

  • 项目结构
├── cli
│ ├── __init__.py
│ ├── app.pyx
│ └── ext
│ ├── Makefile
│ ├── add.c
│ └── add.h
├── pyvenv.cfg
└── setup.py
  • 代码说明
    cli 包含了代码以及cython 包装c 调用的代码, cli/ext 包含了一个c 静态库的代码(简单add)同时使用make 配置了一个简单的构建
    cli/ini.py
import click
# 导入cython 暴露的包
import add_app
@click.command()
@click.option("--scale", default=1, help="Number to scale.")
@click.option("--pod", prompt="pod name",
help="The Pod counts.")
def apply(scale, pod):
"""Simple program that scale pod."""
# 调用c 代码
results = add_app.py_add(scale,10)
print("pod scale with counts",pod,results)
if __name__ == '__main__':
apply()

cli/app.pyx: cython 包装c 代码

cdef extern from "./ext/add.h":
int add(int first,int second)
def py_add(first: int,second: int) -> int:
return add(first,second)

ext/add.h: c 方法的头文件

int add(int first,int second);

ext/add.c c 方法的实现

#include "add.h"

//  一个简单的add 方法
int add(int first,int second){
return first+second;
}

setup.py: 这个是核心,为了方便直接pip 安装的时候进行构建,添加了cmdclass

import setuptools
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
from distutils import extension as distutils_extension
from distutils.command import build as distutils_build
from distutils.command import build_ext as distutils_build_ext import subprocess
with open("README.md", "r") as fh:
long_description = fh.read() # cython extension 配置,添加依赖以及构建的包名称
add_extension = Extension(
name="add_app",
sources=["cli/app.pyx"],
libraries=["add"],
library_dirs=["cli/ext"],
include_dirs=["cli/ext"]
)
class build_ext_Library(distutils_build_ext.build_ext):
def run(self):
command = "cd cli/ext && make"
process = subprocess.Popen(command, shell=True)
process.wait()
distutils_build_ext.build_ext.run(self)
setuptools.setup(
name="dalongrong_cythoncli",
version="0.0.15",
author="dalongrong",
# 配置pip 包包含的文件,方便安装的时候进行代码构建
package_data={
'cli': ['*.pyx',"ext/add.c","ext/add.h","ext/Makefile"]
},
author_email="1141591465@qq.com",
description="a simple cli project",
long_description=long_description,
install_requires=['click',"Cython==0.29.7"],
ext_modules= cythonize([add_extension]),
long_description_content_type="text/markdown",
# 包名称
packages = [
"cli"
],
# 自定义的构建任务
cmdclass ={
"build_ext":build_ext_Library
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
project_urls={
'Documentation': 'https://github.com/rongfengliang/cython-c-pip-demo.git',
'Say Thanks!': 'https://github.com/rongfengliang/cython-c-pip-demo.git',
'Source': 'https://github.com/rongfengliang/cython-c-pip-demo.git',
'Tracker': 'https://github.com/rongfengliang/cython-c-pip-demo.git',
},
entry_points={
'console_scripts': [
'podcli=cli:apply',
],
}
)

push 私服

  • 安装依赖
python -m pip install --user --upgrade setuptools wheel
  • 构建source 包
python setup.py sdist

效果

running sdist
running egg_info
writing dalongrong_cythoncli.egg-info/PKG-INFO
writing dependency_links to dalongrong_cythoncli.egg-info/dependency_links.txt
writing entry points to dalongrong_cythoncli.egg-info/entry_points.txt
writing requirements to dalongrong_cythoncli.egg-info/requires.txt
writing top-level names to dalongrong_cythoncli.egg-info/top_level.txt
reading manifest file 'dalongrong_cythoncli.egg-info/SOURCES.txt'
writing manifest file 'dalongrong_cythoncli.egg-info/SOURCES.txt'
running check
warning: check: missing required meta-data: url creating dalongrong_cythoncli-0.0.15
creating dalongrong_cythoncli-0.0.15/cli
creating dalongrong_cythoncli-0.0.15/cli/ext
creating dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying files to dalongrong_cythoncli-0.0.15...
copying README.md -> dalongrong_cythoncli-0.0.15
copying setup.py -> dalongrong_cythoncli-0.0.15
copying cli/__init__.py -> dalongrong_cythoncli-0.0.15/cli
copying cli/app.c -> dalongrong_cythoncli-0.0.15/cli
copying cli/app.pyx -> dalongrong_cythoncli-0.0.15/cli
copying cli/ext/Makefile -> dalongrong_cythoncli-0.0.15/cli/ext
copying cli/ext/add.c -> dalongrong_cythoncli-0.0.15/cli/ext
copying cli/ext/add.h -> dalongrong_cythoncli-0.0.15/cli/ext
copying dalongrong_cythoncli.egg-info/PKG-INFO -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying dalongrong_cythoncli.egg-info/SOURCES.txt -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying dalongrong_cythoncli.egg-info/dependency_links.txt -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying dalongrong_cythoncli.egg-info/entry_points.txt -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying dalongrong_cythoncli.egg-info/requires.txt -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
copying dalongrong_cythoncli.egg-info/top_level.txt -> dalongrong_cythoncli-0.0.15/dalongrong_cythoncli.egg-info
Writing dalongrong_cythoncli-0.0.15/setup.cfg
Creating tar archive
removing 'dalongrong_cythoncli-0.0.15' (and everything under it)
  • push test pip 仓库

    注意需要先创建账户,同时需要安装twine ,这个工具可以全局安装,按照提示输入账户即可

twine upload --repository-url https://test.pypi.org/legacy/ dist/*
  • 效果

安装测试包

为了方便,使用centos 机器,同时也使用了venv

  • 安装系统环境
yum install -y python36  python36-devel
实际上使用默认自带的也可以,但是为了使用venv 安装了3版本
  • 初始话环境
python3 -m venv appdemo
  • 安装几个依赖

    主要是项目使用的,click 以及cython

cd appdemo && source bin/activate
pip install click cython
  • 安装包
pip install -i https://test.pypi.org/simple/ dalongrong-cythoncli
Looking in indexes: https://test.pypi.org/simple/
Collecting dalongrong-cythoncli
Downloading https://test-files.pythonhosted.org/packages/27/e2/56f135f3ee72d487fd073132d75195a2dd7a3c9122c53d87209640af554a/dalongrong_cythoncli-0.0.15.tar.gz
Requirement already satisfied: click in ./lib/python3.6/site-packages (from dalongrong-cythoncli) (7.0)
Requirement already satisfied: Cython==0.29.7 in ./lib/python3.6/site-packages (from dalongrong-cythoncli) (0.29.7)
Installing collected packages: dalongrong-cythoncli
Running setup.py install for dalongrong-cythoncli ... done
Successfully installed dalongrong-cythoncli-0.0.15
  • 使用
podcli --pod demoapp --scale 4
pod scale with counts demoapp 14
  • pip 包目录结构
ls lib/python3.6/site-packages/
add_app.cpython-36m-x86_64-linux-gnu.so cython.py __pycache__
cli dalongrong_cythoncli-0.0.15-py3.6.egg-info pyximport
click easy_install.py setuptools
Click-7.0.dist-info pip setuptools-39.0.1.dist-info
Cython pip-19.0.3.dist-info
Cython-0.29.7.dist-info pkg_resources

说明

代码比较简单,主要是setup.py 配置extension 以及添加自定义build task,同时需要注意应该使用源码的打包模式

参考资料

https://medium.com/@shamir.stav_83310/making-your-c-library-callable-from-python-by-wrapping-it-with-cython-b09db35012a3
https://github.com/stavshamir/cython-c-wrapper/
https://cython.readthedocs.io/en/latest/src/tutorial/external.html
https://cython.readthedocs.io/en/latest/src/tutorial/clibraries.html
http://pages.cs.wisc.edu/~yezheng/post/cython/
https://github.com/rongfengliang/cython-c-pip-demo/tree/local_source

 
 
 
 

python 集成cython && push 测试pip 仓库的更多相关文章

  1. python 集成cython 简单测试

      实际开发中我们可能需要集成c/c++ 编写的模块,我们可以通过cython 解决类似的问题 以下测试一个简单的c add 方法, 使用venv 同时构建为一个pip 包 环境准备 venv 初始化 ...

  2. Github Actions教程:运行python代码并Push到远端仓库

    我自己做了一个网站,这个网站会使用一个python脚本来生成. 具体生成的方法是python脚本会读取目录下的csv文件,将每一行数据解析成固定格式,然后生成html文件,最后需要将修改后的文件自动p ...

  3. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  4. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  5. 【转】windows和linux中搭建python集成开发环境IDE

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  6. 【Python开发】python集成开发环境IDE搭建

    http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...

  7. Python集成开发工具(IDE)推荐

    1.7 Python集成开发工具(IDE)推荐 1.7.1 Notepad++ Notepad++是Windows操作系统下的一套文本编辑器(软件版权许可证: GPL),有完整的中文化接口及支持多国语 ...

  8. 升级python到2.7版本pip不可用

    升级python到2.7版本pip不可用 [root@localhost pip-7.1.2]# pip Traceback (most recent call last): File "/ ...

  9. 使用python+pychram进行API测试(接口测试)初级STEP 1

    花了一天时间安装了解了下最基本的python+pychram进行API测试,下面这个可以指导自己以后入门:基本的开发级别还需要学习 1.python下载地址:https://www.python.or ...

随机推荐

  1. L1-064 估值一亿的AI核心代码

    以上图片来自新浪微博. 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多余空格:把相邻单词间的多个空格换成 1 个 ...

  2. RAID配置多阵列

    感谢: https://www.cnblogs.com/hystj/articles/8672029.html

  3. MySQL 性能优化的最佳20多条经验分享(收藏)

    1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...

  4. python 函数返回值(总结)

    关键字:return 没有返回值的叫过程 def test1(): msg="我是一个过程" print(msg) 有return的叫函数 def test02(): msg=&q ...

  5. zabbix添加IIS网站计数器(并发连接数)详解

    环境:windows server 2012 前提:IIS上要添加好配置   1,在被监控主机,powershell输入perfmon.msc   2,点击添加按钮     3,在下拉菜单中点击小箭头 ...

  6. svg的使用集合

    1.效果 https://www.zhangxinxu.com/wordpress/2017/03/offset-path-css-animation/ 2.用法 https://www.cnblog ...

  7. erlang link 与 monitor

    erlang设计中,通常会有这样一个需求: 某一个进程必须依赖于令一个进程的概念,在这样的情况下就必须对两个进程之间建立一个监控或者说连接关系,以监听对方的死亡情况. erlang 提供了两个这样的方 ...

  8. pycharm介绍

      PyCharm是一种Python IDE(集成开发软件),带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具. 1.1 PyCharm主界面 PyCharm包含4个主要区分,分别为 ...

  9. C#自定义事件模拟风吹草摇摆

    这是一个自定义事件的例子.C#.WinForm.Visual Studio 2017.在HoverTreeForm中画一块草地,上面有许多草(模拟).HewenqiTianyi类模拟天气,会引发“风” ...

  10. 【oracle】生成AWR报告

    [第一步]找到awrrpt.sql文件 [ora11g@vm-kvm11820-app ~]$ locate awrrpt.sql /DATA/opt/app/ora11g/product//rdbm ...