python 集成cython && push 测试pip 仓库
昨天创建了一个简单的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 仓库的更多相关文章
- python 集成cython 简单测试
实际开发中我们可能需要集成c/c++ 编写的模块,我们可以通过cython 解决类似的问题 以下测试一个简单的c add 方法, 使用venv 同时构建为一个pip 包 环境准备 venv 初始化 ...
- Github Actions教程:运行python代码并Push到远端仓库
我自己做了一个网站,这个网站会使用一个python脚本来生成. 具体生成的方法是python脚本会读取目录下的csv文件,将每一行数据解析成固定格式,然后生成html文件,最后需要将修改后的文件自动p ...
- 【转】linux和windows下安装python集成开发环境及其python包
本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...
- windows和linux中搭建python集成开发环境IDE——如何设置多个python环境
本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...
- 【转】windows和linux中搭建python集成开发环境IDE
本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...
- 【Python开发】python集成开发环境IDE搭建
http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...
- Python集成开发工具(IDE)推荐
1.7 Python集成开发工具(IDE)推荐 1.7.1 Notepad++ Notepad++是Windows操作系统下的一套文本编辑器(软件版权许可证: GPL),有完整的中文化接口及支持多国语 ...
- 升级python到2.7版本pip不可用
升级python到2.7版本pip不可用 [root@localhost pip-7.1.2]# pip Traceback (most recent call last): File "/ ...
- 使用python+pychram进行API测试(接口测试)初级STEP 1
花了一天时间安装了解了下最基本的python+pychram进行API测试,下面这个可以指导自己以后入门:基本的开发级别还需要学习 1.python下载地址:https://www.python.or ...
随机推荐
- No mapping found for HTTP request with URI [/crmcrmcrm/css/bootstrap.min.css] in DispatcherServlet with name 'springMvc'
先把错误贴上来 No mapping found for HTTP request with URI [/crmcrmcrm/css/sb-admin-2.css] in DispatcherServ ...
- Tomcat配置及不依赖IDEA部署web应用
http:tomcat.apache.org 下载tomcat文件包 我使用的tomcat9的版本 Tomcat9014使用的是Servlet4.0 解压即可,目录如下 bin :启动和关闭tomca ...
- php优秀框架codeigniter学习系列——CI_Controller分析
该类是一个超级大的父类,它将在 CodeIgniter.php 中实例化化过的类,通通加载成它的类成员变量,所以可以方便的进行各种操作.各种应用控制器类,都会继承 CI_Controller 类. _ ...
- java第八周作业
分析代码: public final class LineItemKey implements Serializable { private Integer customerOrder; privat ...
- HTML语言发展史
.发展时间线 1982年,Tim Berners-Lee 建立 HTML 1993年,大學生的 Marc Andreessen 在他的 Mosaic 浏览器加入 标记,从此可以在Web頁面上浏览图片 ...
- MySQL在高内存、IO利用率上的几个优化点
以下优化都是基于CentOS系统下的一些MySQL优化整理,有不全或有争议的地方望继续补充完善. 一.mysql层面优化 1. innodb_flush_log_at_trx_commit 设置为2设 ...
- 1--Test NG--常见测试和注解
第一:注解 (1)@test (2)@BeforeMethod,@AfterMethod (3)@BeforeClass,@AfterClass (4)@BeforeSuite,@AfterSuite ...
- Linux中各个文件的作用
1.bin: 存放的是执行的常用指令 2.boot: 启动系统的核心文件 3.dev: Linux将设备映射成文件,而dev中放的就是这些设备文件 4.etc: 各种配置文件 5.home: 用户的主 ...
- v-echart 按需加载
import 'v-charts/lib/style.css';import VeBar from 'v-charts/lib/bar' // 条形图import VeRadar from 'v-ch ...
- python-markdown
python-markdown无法将“`生成标签问题解决方法 2种都是代码区块 ```swift is Int ``` is Int