需求:
经常要打包pyd,多个pyd一起打包,为了方便而自动化这项工作

实现代码:

import os
import shutildef split_filename(file):file_path, file_name = os.path.split(file)file_sname, file_ext = os.path.splitext(file_name)return file_path, file_name, file_sname, file_extdef pack_pyd_process(file, interpreter_path=r"D:\David\venvs\venv_anctest_ws_32\Scripts\python.exe", to_folder="."):if not interpreter_path.endswith("\python.exe"):interpreter_path = interpreter_path.rstrip("\\") + r"\python.exe"file_path, file_name, file_sname, file_ext = split_filename(file)# 1, 创建setup.pycreat_setup_file(file, file_sname)# 2, 在命令行执行如下代码cmd_str = f"{interpreter_path}setup.py build_ext --inplace"# os_system(cmd_str)os_popen(cmd_str)# 3, 将当前文件夹下的 build 文件夹、pyd文件(所有)剪切到指定目标路径if os.path.abspath(to_folder) != os.path.abspath("."):# 将 build 文件夹剪切到指定目标路径try:shutil.move("build",to_folder)  # shutil.Error: Destination path 'D:\David\rd\pack_del\build' already existsexcept shutil.Error as e:err_msg = f"e:{e}"print(f"{err_msg}")if "already exists" in err_msg:is_substitute = input("请问是否替换build?,输入y【Y】或者n【N】:")if is_substitute in ['y', 'Y']:# 递归删除文件夹,即:删除非空文件夹;如果报错  OSError: [Errno 39] Directory not empty 则设置参数 ignore_errorsshutil.rmtree(os.path.join(to_folder, "build"), ignore_errors=True)shutil.move("build", to_folder)elif is_substitute in ['n', 'N']:pass# 将当前文件夹下的pyd文件(所有)剪切到指定目标路径files = os.listdir(".")pyd_files = list()for file in files:if file.endswith(".pyd"):pyd_files.append(file)for pyd_file in pyd_files:try:shutil.move(pyd_file,to_folder)  # shutil.Error: Destination path 'D:\David\rd\pack_del\build' already existsexcept shutil.Error as e:err_msg = f"e:{e}"print(f"{err_msg}")if "already exists" in err_msg:is_substitute = input("请问是否替换?,输入y【Y】或者n【N】:")if is_substitute in ['y', 'Y']:_, file_name, _, _ = split_filename(pyd_file)os.remove(os.path.join(to_folder, file_name))shutil.move(pyd_file, to_folder)elif is_substitute in ['n', 'N']:continuedef os_system(cmd_str):ret = os.system(cmd_str)print(ret)def os_popen(cmd_str):f = os.popen(cmd_str, 'r')d = f.read()print(d)f.close()def creat_setup_file(file, file_sname):# 1, 新建一个 setup.py文件(文件名可以随意命名)里面代码如下【code_name为要打包的py文件】with open(f"setup.py", 'w') as fw:fw.write(f"""
from distutils.core import setup
from Cython.Build import cythonizesetup(name="{file_sname}",ext_modules=cythonize(r"{file}"),
)""")def pack_one(file, interpreter_path, to_folder):"""将传入的一个file(py文件)打包为pyd"""paras = {"file": file,"interpreter_path": interpreter_path,"to_folder": to_folder,}pack_pyd_process(**paras)def pack_all_in_txt(txt_file, interpreter_path, to_folder):"""将txt里面的所有py文件打包为pyd"""files = [file.strip() for file in open(txt_file, 'r').readlines()]print(f"files: 【开始】{files}【完】")for file in files:paras = {"file": file,"interpreter_path": interpreter_path,"to_folder": to_folder,}pack_pyd_process(**paras)def main():"""pack_interface"""file = input("请输入要打包的py文件或者txt文件(内容为n行py文件名)的全路径文件名:")interpreter_path = input("请输入解释器的全路径文件名:")to_folder = input("请输入打包好的pyd存放目标路径:")if file.endswith(".py"):pack_one(file, interpreter_path, to_folder)elif file.endswith(".txt"):try:pack_all_in_txt(file, interpreter_path, to_folder)except Exception as e:print(f"e:{e}")if __name__ == '__main__':main()

结果:

   """请输入要打包的py文件全路径文件名:\pack_del\pack_pys.txt请输入解释器的全路径文件名:D:\David\venvs\venv_anctest_ws_32\Scripts请输入打包好的pyd存放目标路径:\pack_delD:\David\venvs\venv_anctest_ws_32\Scripts\pythonw.exe D:/David/rd/code/rd_test/pack_pyd/test.py请输入要打包的py文件【所在txt】的txt全路径文件名:\pack_del\pack_pys.txt请输入解释器的全路径文件名:D:\David\venvs\venv_anctest_ws_32\Scripts请输入打包好的pyd存放目标路径:\pack_delfiles: 【开始】['\\pack_del\\aaa.py', '\\pack_del\\asio_sd88.py']【完】running build_exte: Destination path '\pack_del\build' already exists请问是否替换build?,输入y【Y】或者n【N】:ye: Destination path '\pack_del\aaa.cp36-win32.pyd' already exists请问是否替换?,输入y【Y】或者n【N】:yrunning build_extbuilding 'asio_sd88' extensioncreating buildcreating build\temp.win32-3.6creating build\temp.win32-3.6\Releasecreating build\temp.win32-3.6\Release\Davidcreating build\temp.win32-3.6\Release\David\rdcreating build\temp.win32-3.6\Release\David\rd\pack_delC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -ID:\David\venvs\venv_anctest_ws_32\include -IC:\Users\test\AppData\Local\Programs\Python\Python36-32\include -IC:\Users\test\AppData\Local\Programs\Python\Python36-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" /Tc\pack_del\asio_sd88.c /Fobuild\temp.win32-3.6\Release\David\rd\pack_del\asio_sd88.objasio_sd88.cC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:D:\David\venvs\venv_anctest_ws_32\libs /LIBPATH:C:\Users\test\AppData\Local\Programs\Python\Python36-32\libs /LIBPATH:C:\Users\test\AppData\Local\Programs\Python\Python36-32 /LIBPATH:D:\David\venvs\venv_anctest_ws_32\PCbuild\win32 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x86" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\um\x86" /EXPORT:PyInit_asio_sd88 build\temp.win32-3.6\Release\David\rd\pack_del\asio_sd88.obj /OUT:\code\rd_test\pack_pyd\asio_sd88.cp36-win32.pyd /IMPLIB:build\temp.win32-3.6\Release\David\rd\pack_del\asio_sd88.cp36-win32.libCreating library build\temp.win32-3.6\Release\David\rd\pack_del\asio_sd88.cp36-win32.lib and object build\temp.win32-3.6\Release\David\rd\pack_del\asio_sd88.cp36-win32.expGenerating codeFinished generating codee: Destination path '\pack_del\build' already exists请问是否替换build?,输入y【Y】或者n【N】:ye: Destination path '\pack_del\asio_sd88.cp36-win32.pyd' already exists请问是否替换?,输入y【Y】或者n【N】:yProcess finished with exit code 0"""

python 自动打包pyd相关推荐

  1. python382怎么用_手把手教你使用python自动打包 上传应用分发

    1)先上脚本 步骤很简单 ,如下文所示 本脚本是基于python3,如何在mac 电脑升级python3 参考我2天前写的文章 Mac 系统同时安装python2 和python3 #!/usr/bi ...

  2. 自动打包+ios+android,使用 python 自动打包 Android 和 iOS

    大端模式 VS 小端模式 简单点说,就是字节的存储顺序,如果数据都是单字节的,那怎么存储无所谓了,但是对于多字节数据,比如int,double等,就要考虑存储的顺序了.注意字节序是硬件层面的东西,对于 ...

  3. ios 自动打包命令_iOS自动打包上传脚本

    自从将swift2.2升级到swift3.0, 每次使用Xcode8编译都很慢,很是不爽,于是有了研究下xcodebuild命令行打包的想法,起初不知道用shell,还是用python, 在网上大概搜 ...

  4. iOS架构-自动打包并上传到App Store(python)(21)

    这里是摘自一篇用python 语法写的脚本,这里只是为了更好的理解自动化打包上传App Store的原理的. 一. iOS 自动打包并上传到App Store(python) 我们平时大都是用Appl ...

  5. python项目打包部署到ios_Python iOS 自动打包脚本(包含上传到fir)

    Python iOS自动打包脚本 使用说明 1.1 使用python3编写,没有python3 环境的需要下载python3 python官网下载 1.2 通过Homebrew安装Python3 1. ...

  6. python远程同步文件_Python实现的远程文件自动打包并下载功能示例

    Python实现的远程文件自动打包并下载功能示例 发布时间:2020-08-19 23:32:33 来源:脚本之家 阅读:103 本文实例讲述了Python实现的远程文件自动打包并下载功能.分享给大家 ...

  7. python程序打包成apk_利用Gradle+Python3自动打包Android APK上传到蒲公英。

    利用Gradle+Python3自动打包Android APK上传到蒲公英. 面对每次都要打包一个版本发布给测试,都要手动打包签名~然后登录上传文件.这些繁琐的事情.于是就想到一句很经典的话,(人生苦 ...

  8. 【转载】用cx_Freeze把Python代码打包成单个独立的exe可执行文件

    链接:用cx_Freeze把Python代码打包成单个独立的exe可执行文件 [记录]用cx_Freeze把Python代码打包成单个独立的exe可执行文件 背景 之前已经折腾过: [记录]用PyIn ...

  9. python文件打包成exe可执行文件

    步骤一.安装pyinsatller 打开命令行窗口,输入如下指令 pip3 install pyinstalle 等待pyinsatller库安装完成. 步骤二.使用pyinstaller打包Pyth ...

  10. Python项目打包发布到pypi

    最近心血来潮,想把自己的写的python项目打包到pypi,也让广大朋友能够通过pip来安装我的python包. 第一次尝试,中间遇到了一些问题,记录下来,方便其他感兴趣的朋友. 项目组织架构 # t ...

最新文章

  1. Toby Walsh教授:四个指数趋势解释人工智能威胁论!
  2. dw网页设计作品_10个富有特色的网页设计精选作品
  3. python测试代码运行时间_10种检测Python程序运行时间、CPU和内存占用的方法
  4. mysql高级查询 二_MySQL高级查询(二)
  5. edge android apk下载地址,edge app下载-edge完整版v7.2.0 安卓版 - 极光下载站
  6. WebLogic Clustering Overview Slides
  7. 设计模式之GOF23解释器模式
  8. 呼叫中心系统的解决方案
  9. 一份很棒的外设驱动库(基于STM32F4)
  10. TVS瞬变抑制二极管选型表
  11. sprintf和fprintf
  12. 我眼中的光明·第六周·蓝天·一
  13. open ai gpt_GPT-3:第一个人工智能?
  14. 【2014-08-23】Beyong Coding
  15. 怎么把好几行弄成一行_怎么在word文档里把很多行一下合并成一行
  16. python+django+动态生成word
  17. ESXi 6.7 封装驱动(Intel-I219V使用非vib的离线包驱动格式)
  18. 只有加法也能做深度学习,北大、华为等提出AdderNet,性能不输传统CNN
  19. 微信公众号数据分析报告
  20. java注解约束参数为固定值_Java学习 使用注解将参数的值限定

热门文章

  1. 打开word很慢(无网络时正常)
  2. 太厉害了,终于有人能把Ansible讲的明明白白了,建议收藏
  3. iis 设置php静态,PHP的Rewrite静态化服务器配置(包括IIS的静态华配置)
  4. 苹果开发者账号双重认证
  5. C语言嵌入式数据结构之链表
  6. aid learning安装应用_Aid Learning
  7. 使用Python调用OUTLOOK发邮件(带附件)
  8. python的加减乘除运算_python四则运算
  9. coldfusion_在ColdFusion中建立动态菜单
  10. 四、FFI和第三方模块