python打包工具比較

前一段用python寫了點小工具,希望能給同事用,這裏總結一下python的打包以及構建的方法。

首先是一些需要安裝依賴包的方法,這也是比較推薦的正統的方法。

1.setuptools or pip

  在setup.py文件中寫明依賴的庫和版本,注意需要提前安裝setuptools,然後運行
  

python setup.py install

文件大致如下,這裏是selenium的安裝文件:

import sys

from distutils.command.install import INSTALL_SCHEMES
from os.path import dirname, join, isfile, abspath
from setuptools import setup
from setuptools.command.install import install
from shutil import copy


for 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 sys
import os
path =os.path.realpath(__file__)
path = os.path.dirname(path)
path +=os.sep+"required"+os.sep+"site-packages"
sys.path.append(path)

  然後加載非標準模塊,執行原來的代碼即可。
  這裏有個限制:有一些操作不能執行,例如不能有操作文件的指令,因爲文件還在zip裏沒有解壓。
  
  PSS:最後提一個docker,因爲沒有使用過,這裏暫且不講了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章