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 ...
随机推荐
- 记一次JDK升级带来的连环反应
公司之前有个很久以前的小项目,页面用到了flash. 现在要去掉flash, 前端使用公司自己开发的框架来展示数据, 使用该框架后台要引用一个jar包封装数据传递给前台. 但该框架是jdk1.8编译的 ...
- Tensorflow实战系列之三:
博主也是初学,能力有限,这个完全没想好..
- LINQ Expresstion Tree 表达式树
Expression trees represent code in a tree-like data structure, where each node is an expression, for ...
- 性能测试LR学习笔录2am pm -3
回顾day1: 1.什么是性能测试? 模拟真实的生产环境,以各种不同的压力(模拟大量用户)去测试被测系统,去”攻击“测试系统.同时 记录下被测系统中各台服务器的各种重要资源情况,包括cpu.内存.磁盘 ...
- AOP异常报错1
Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' ...
- Oil Deposit
题目描述: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...
- spring cloud使用Feign做消费端时的eureka.client.registerWithEureka/eureka.client.fetchRegistry是否配置的问题
记录一下今天工作中的一个小失误. 今天用Feign搭建服务消费者的时候,考虑消费者不需要再提供服务给其他服务,所以不需要注册到注册中心(eureka)中.结果把registerWithEureka和f ...
- 服务器tomcat/mysql的一些有关命令
停服务1.“ps -ef|grep java” # 查看tomcat进程id 若下面出现一大串内容,包含有tomcat的目录,前面的四位数的数字就是tomcat应用的进程id 2.“kill -9 进 ...
- mysql_pconnect 问题
不同于mysql_connect的短连接,mysql_pconnect持久连接的时候,将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接 ...
- myls
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unist ...