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文件。这里参考了知乎的回答。

  1. 首先子啊zip文件根目录建立一个main.py 文件,一个required目录。
  2. 把依赖的非标准模块从你自己的开发环境下的site-packages下拷贝到zip目录的required下
  3. 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
原创粉丝点击