黑龙江网站设计公司,广告海外推广,wordpress小说站数据库,我想创业做网站前提条件#xff1a;
确保电脑已经安装gcc且终端能检索到确保Python中已经安装cython包#xff0c;若未安装#xff0c;则先使用pip install cython进行安装
打包方法#xff1a; step1#xff1a;编写编译脚本setup.py#xff0c;代码如下#xff1a; # encoding ut…前提条件
确保电脑已经安装gcc且终端能检索到确保Python中已经安装cython包若未安装则先使用pip install cython进行安装
打包方法 step1编写编译脚本setup.py代码如下 # encoding utf-8
from distutils.core import setup
from Cython.Build import cythonize
setup(
name libname,
ext_modules cythonize([file1.py, file2.py, dir/file3.py], language_level 3)
)step2终端中执行: python3 setup.py build_ext需要注意的点
setup()中的ext_modules不是exe_modules千万不要打错了如果打错了你会发现只编译成了.c文件而没用生成.so生成的.so只能在打包计算机相同系统、相同平台及相同python版本环境下使用要想在其他平台或环境下使用需要在指定平台或环境下重新打包存在多个python版本时如在linux下博主python2和python3共存需要指定编译用的python版本否则终端会报警告FutureWarning: Cython directive ‘language_level’ not set, using 2 for now (Py2)。指定版本有两种方式 method 1: 在每个要打包的py文件头部写入# cython:language_level3 method 2: 像上述setup.py中一样在cythonize函数中加入language_level 3选项 打包完的.so文件结构会被打乱如上述dir中的file3.py生成的file3.so与其他两个同目录而不是在dir文件夹下如果不做调整执行脚本时可能存在import错误因此还要手动调整一下文件结构
附批量打包的代码 import os
import sys
import shutil
import numpy
import tempfilefrom setuptools import setup
from setuptools.extension import Extensionfrom Cython.Build import cythonize
from Cython.Distutils import build_ext
import platform# code from: https://blog.csdn.net/qq_33375598/article/details/118677130# 构建后存放的文件目录
build_root_dir build/lib. platform.system().lower() - platform.machine() - str(sys.version_info.major) . str(sys.version_info.minor)print(build_root_dir)extensions []
ignore_folders [build, test, tests]
conf_folders [conf]def get_root_path(root):if os.path.dirname(root) in [, .]: # 得到文件的文件路径return os.path.basename(root) # 返回path最后的文件名else:return get_root_path(os.path.dirname(root))def copy_file(src, dest):if os.path.exists(dest): # 目的文件存在返回returnif not os.path.exists(os.path.dirname(dest)): # 目的文件夹不存在递归创建文件夹os.makedirs(os.path.dirname(dest))if os.path.isdir(src): # 判断某一路径是否为目录shutil.copytree(src, dest) # 拷贝整个文件夹(目的文件夹需要不存在否则会失败)else:shutil.copyfile(src, dest) # 拷贝整个文件def touch_init_file(): # 在临时文件夹中创建initinit_file_name os.path.join(tempfile.mkdtemp(), __init__.py)with open(init_file_name, w):passreturn init_file_nameinit_file touch_init_file()
print(init_file)def compose_extensions(root.):for file_ in os.listdir(root): # 当前目录下的所有文件abs_file os.path.join(root, file_) # 路径拼接if os.path.isfile(abs_file):if abs_file.endswith(.py):extensions.append(Extension(get_root_path(abs_file) .*, [abs_file]))elif abs_file.endswith(.c) or abs_file.endswith(.pyc):continueelse:copy_file(abs_file, os.path.join(build_root_dir, abs_file))if abs_file.endswith(__init__.py): # 用空白的__init__.py替代原有的copy_file(init_file, os.path.join(build_root_dir, abs_file))else:if os.path.basename(abs_file) in ignore_folders: # 忽略的文件不拷贝continueif os.path.basename(abs_file) in conf_folders: # 配置文件一同拷贝copy_file(abs_file, os.path.join(build_root_dir, abs_file))compose_extensions(abs_file)compose_extensions()
os.remove(init_file)setup(nameyour_project_name,version1.0,ext_modulescythonize(extensions,nthreads16,compiler_directivesdict(always_allow_keywordsTrue),include_path[numpy.get_include()], language_level3),cmdclassdict(build_extbuild_ext))参考链接
https://blog.csdn.net/qq_33375598/article/details/118677130https://blog.csdn.net/weixin_36755535/article/details/127300870