python打包工具比较
来源:互联网 发布:淘宝网服饰香柳儿 编辑:程序博客网 时间:2024/05/16 12:41
前一段用python写了点小工具,希望能给同事用,这里总结一下python的打包以及构建的方法。
首先是一些需要安装依赖包的方法,这也是比较推荐的正统的方法。
1.setuptools or pip
在setup.py文件中写明依赖的库和版本,注意需要提前安装setuptools,然后运行
python setup.py install
文件大致如下,这里是selenium的安装文件:
import sysfrom distutils.command.install import INSTALL_SCHEMESfrom os.path import dirname, join, isfile, abspathfrom setuptools import setupfrom setuptools.command.install import installfrom shutil import copyfor scheme in INSTALL_SCHEMES.values(): scheme['data'] = scheme['purelib']setup_args = { 'cmdclass': {'install': install}, 'name': 'selenium', 'version': "2.52.0", 'description': 'Python bindings for Selenium', 'long_description': open(join(abspath(dirname(__file__)), "py", "README.rst")).read(), 'url': 'https://github.com/SeleniumHQ/selenium/', 'classifiers': ['Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'License :: OSI Approved :: Apache Software License', 'Operating System :: POSIX', 'Operating System :: Microsoft :: Windows', 'Operating System :: MacOS :: MacOS X', 'Topic :: Software Development :: Testing', 'Topic :: Software Development :: Libraries', 'Programming Language :: Python', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4'], 'package_dir': { 'selenium': 'py/selenium', 'selenium.common': 'py/selenium/common', 'selenium.webdriver': 'py/selenium/webdriver', }, 'packages': ['selenium', 'selenium.common', 'selenium.webdriver', 'selenium.webdriver.android', 'selenium.webdriver.chrome', 'selenium.webdriver.common', 'selenium.webdriver.common.html5', 'selenium.webdriver.support', 'selenium.webdriver.firefox', 'selenium.webdriver.ie', 'selenium.webdriver.edge', 'selenium.webdriver.opera', 'selenium.webdriver.phantomjs', 'selenium.webdriver.remote', 'selenium.webdriver.support', ], 'package_data': { 'selenium.webdriver.firefox': ['*.xpi', 'webdriver_prefs.json'], }, 'data_files': [('selenium/webdriver/firefox/x86', ['py/selenium/webdriver/firefox/x86/x_ignore_nofocus.so']), ('selenium/webdriver/firefox/amd64', ['py/selenium/webdriver/firefox/amd64/x_ignore_nofocus.so'])], 'include_package_data': True, 'zip_safe': False}setup(**setup_args)
如果双方都有pip工具,直接使用命令导出依赖并安装即可
$ pip freeze > requirements.txt$ pip install -r requirements.txt
2.pyintaller or cx_Freeze
pyintaller现在已经支持Python 2.7, 3.3–3.5。工具可以自动搜索依赖,并将文件打包成单独的exe。命令也非常简单,当然还有自己可以选择的一些命令,例如生成目录还是单个文件,生成路径等等:
具体的安装和使用参考
https://pyinstaller.readthedocs.io/en/stable/
最简单的使用如下:
pyinstaller myscript.py
PS:如果在linux下使用,需要root权限,因为会涉及到文件的创建,读写等等。同时,这个打包工具使用动态依赖库,需要打包方和使用方是相同的操作系统。
cx_freeze类似,这里不赘述。
3.zip
如果依赖的第三方库是纯python实现,python2.6+/python3支持直接运行zip文件。这里参考了知乎的回答。
- 首先子啊zip文件根目录建立一个main.py 文件,一个required目录。
- 把依赖的非标准模块从你自己的开发环境下的site-packages下拷贝到zip目录的required下
- 在main.py中利用 file 变量拿到执行路径,拼接出 site-packages 的绝对路径,添加到 sys.path 中,方法大致如下:
import sysimport ospath =os.path.realpath(__file__)path = os.path.dirname(path)path +=os.sep+"required"+os.sep+"site-packages"sys.path.append(path)
然后加载非标准模块,执行原来的代码即可。
这里有个限制:有一些操作不能执行,例如不能有操作文件的指令,因为文件还在zip里没有解压。
PSS:最后提一个docker,因为没有使用过,这里暂且不讲了。
0 0
- python打包工具比较
- python setuptools工具打包
- python 打包工具
- python打包工具
- python打包工具pyinstaller
- 打包python脚本为可执行文件的工具
- Python打包、安装与发布工具--setuptools
- 使用cx_freeze工具打包Python脚本
- python程序打包工具py2exe/pyInstaller
- Linux下Python打包工具PyInstaller使用说明
- android批量打包工具-python实现
- python打包工具distutils的使用
- python开发者打包发布工具Distutils | setuptools
- Python 打包工具对比,Nuitka vs Pyinstaller
- Python打包分发工具setuptools简介
- Python 官方推荐的一款打包工具
- Python扩展方法及工具比较
- Python扩展方法及工具比较
- 微信发红包UITextField金额输入格式化
- 牛人主页(主页有很多论文代码)
- Android中shape属性详解
- JAVA并发编程学习笔记之AQS源码分析(获取与释放)(r)
- PLSQL导出大量数据-超出excel限制,使用csv
- python打包工具比较
- TOP to Down设计简单例子 Creo3.0
- 腾讯高级工程师:一道面试题引发的高并发性能调试思考
- [Python]网络爬虫(五):urllib2的使用细节与抓站技巧
- MySQL配置参数详解
- Python-线程后篇生肉
- js返回上一页并刷新
- css继承、层叠、特殊
- poj 1873 The Fortified Forest