python打包成mac程序_mac下的应用程序发布 及 打包(Python写的脚本,可打包第三方库)...

本文介绍了如何将Python脚本及依赖打包成Mac程序,特别针对包含QT库的情况。通过编写自动化Python脚本,实现对第三方库的检测和打包,简化了手动打包的复杂过程,以解决Mac下Python应用的发布问题。
摘要由CSDN通过智能技术生成

其实这个问题在网上能搜到大把的解决方案。大家的统一答案都是

otool -L yourapp.app/Contents/MacOS/yourapp

根据输出信息在运行 install_name_tool

install_name_tool的使用方法在这里

比较复杂的是当依赖了很多第三方库,尤其是向QT这样的库的时候。打包那叫一个麻烦啊。

QT有个官方的文档告诉你如何手动一步步打包程序,还提供了一个macdepotqt这么个程序。帮你进行打包。

可是啊,macdepotqt不支持其他第三方库,也不灵活,按照官方文档的方式手动打包,坑爹啊。

说他坑爹一点都不假,当你遇到了成片的符号需要你敲命令的话,想死的心情油然而生。

那么好了。自己动手吧。这里我给出一个我写的打包的Python脚本半成品,思想跟手动敲命令是一样的,只不过用脚本实现自动化了同时递归检查并将缺少的库考到bundle目录中。脚本中针对QT的库进行打包了,如果希望把其他的依赖库也打包的话在keywordList里边添加相应的关键字就好了。希望对大家能有帮助。

import os

import sys

import commands

binaryName = "yourapp"

bundleName = "yourapp.app"

bundleFrameworkDir = "Contents/Frameworks/"

bundleBinaryDir = "Contents/MacOS/"

bundleLibraryList = [];

systemFrameworkDir = "/Library/Frameworks/"

keyWordList = ["Qt"]

#add more keywords to work better

def hasKeyWord(word):

for it in keyWordList:

if word.find(it) != -1:

return True

return False

def findApp(name):

return name+bundleBinaryDir+binaryName

def getBundleDependsInfo(app):

dependList = commands.getoutput("otool -L " + app).replace("\t","").split("\n");

del(dependList[0])

dependList = [item.split(" ")[0] for item in dependList if hasKeyWord(item)];

return dependList

def copyLibrary(base, src, dst):

systemFullPath = src

print "library %s depend %s" % (os.path.basename(base), os.path.basename(dst))

if not os.path.exists(dst):

bundleFullPath = os.path.dirname(dst)

os.system("mkdir -p %s" % (bundleFullPath))

os.system("cp %s %s" % (systemFullPath, bundleFullPath))

infoList = getBundleDependsInfo(dst)

copyDependFiles(dst, infoList)

os.system("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))

os.system("install_name_tool -change %s @executable_path/../Frameworks/%s %s" % (src, src, base))

def getFrameworkName(dirname):

if dirname.find("framework") == -1:

return

while not dirname.endswith("framework"):

dirname = os.path.dirname(dirname)

return dirname

def copyFrameworkExtDir(src):

sysPath = systemFrameworkDir + src

destPath = ""

if not os.path.exists(sysPath):

return

frameworkPath = getFrameworkName(sysPath)

frameWorkName = getFrameworkName(src)

bundlePath = bundleName + "/" + bundleFrameworkDir + frameWorkName + "/"

for it in bundleFrameworkExtDir:

destPath = bundlePath + it

srcPath = frameworkPath + "/" + it

if not os.path.exists(destPath) and os.path.exists(srcPath):

print "copying %s %s" % (frameWorkName, it)

os.system("cp -r %s %s" % (srcPath, destPath))

def copyFramework(base, src, dst):

print "framework %s depend %s" % (os.path.basename(base), os.path.basename(dst))

systemFullPath = systemFrameworkDir+src

if not os.path.exists(dst):

bundleFullPath = os.path.dirname(dst)

os.system("mkdir -p %s" % (bundleFullPath))

os.system("cp %s %s" % (systemFullPath, bundleFullPath))

copyFrameworkExtDir(src)

infoList = getBundleDependsInfo(dst)

copyDependFiles(dst, infoList)

("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))

os.system("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))

os.system("install_name_tool -change %s @executable_path/../Frameworks/%s %s" % (src, src, base))

def copyDependFiles(base, infoList):

targetDir = ""

for it in infoList:

targetDir = bundleName + "/" + bundleFrameworkDir + it

if it.find("framework") != -1:

copyFramework(base, it, targetDir)

else:

copyLibrary(base, it, targetDir)

def makeBundleDirs():

os.system("mkdir -p " + bundleName + "/" + bundleFrameworkDir)

if __name__ == "__main__":

target = findApp(bundleName + "/")

makeBundleDirs()

infoList = getBundleDependsInfo(target)

copyDependFiles(target, infoList)

http://blog.csdn.net/livemylife/article/details/7294778

Mac下将C程序创建为动态链接库再由另一个C程序调用

写C的时候需要调用之前的一个C程序,想用动态链接库的方式.Mac下的动态链接库是dylib,与Linux下的.os或Windows下的.dll不同.由于之前没有接触过,所以翻了大量的博客,然而在编译过 ...

如何发布自己用python写的py模块

Python——怎么发布你的Python模块 我们在学习Python的时候,除了用pip安装一些模块之外,有时候会从网站下载安装包下来安装,我也想要把我自己编写的模块做成这样的安装包,该怎么办,如何发 ...

Mac下修改应用程序的菜单快捷键!

点击左上角苹果按钮,系统偏好设置 > 键盘 > 快捷键 > 应用快捷键 点击右下角添加按钮,选择chrome程序,输入菜单中文名以及快捷键 1.如何用F5刷新 鼠标悬停在左上角的刷新 ...

Mac下Homebrew将程序文件存在什么位置

一般情况是这么操作的: 1.通过brew install安装应用最先是放在/usr/local/Cellar/目录下. 2.有些应用会自动创建软链接放在/usr/bin或者/usr/sbin,同时也会 ...

linux安装配置apk打包程序gradle+jdk+Android_sdk+python自动化编译脚本

安装gradle: 1.下载gradle包 去这里下载需要的tar.gz包:https://services.gradle.org/distributions/ 2.解压 tar zxvf gradl ...

Mac下各种编程环境的配置问题(python java)

首先,去官网下载安装包.直接运行安装.安装完成后,启动器中会多两个应用程序IDLE和Python Launcher. 如果,你习惯在IDLE,直接运行即可. 但你在Terminal中运行python3 ...

在mac下使用python抓取数据

2015已经过去,这是2016的第一篇博文! 祝大家新年快乐! 但是我还有好多期末考试! 还没开始复习,唉,一把辛酸泪! 最近看了一遍彦祖的文章叫做 iOS程序员如何使用Python写网路爬虫 所以自 ...

Python将自己写的模块进行打包

将项目打包成模块的想法来自于flask文档教程,这不是在PyCon上和阿明合了照嘛,这不得多看看人家的东西.有兴趣的可以看看文档的项目可安装化部分,作者将flask项目打包成一个包,使其可以再任何地方 ...

mac下git+maven+jenkins自动打包发布

随着springboot+springcloud(dubbo)越来越多人使用,流行的微服务的概念越来越深入人心.分布式部署越来越复杂,给手动发布带来很大工作量.为了方便前期测试和后期线上部署更新,可使 ...

随机推荐

windows PHONE 开发-入门程序构筑

1:页面的扩展名为:.xaml文件类似于ASPX一样可以编写客户端显示内容和后台处理内容 一般的前台页面的形式为:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值