使用PyInstaller打包webssh

时间:2022-06-18
本文章向大家介绍使用PyInstaller打包webssh,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

接前一篇博客,为了使webssh更容易安装部署,我在想是不是可以把webssh打包成一个独立的可执行文件,这样使用起来不是更方便呢。于是乎我想到了是不是可以使用PyInstaller来打包。

准备测试环境

这一步主要是准备一个干净的virtualenv环境,并安装pyinstaller和webssh。

$ cd ~
$ virtualenv myenv
$ cd myenv
$ . bin/activate
$ pip install pyinstaller
$ pip install webssh

准备spec文件

为了使用pyinstaller,需要准备一个spec文件并保存为 “~/myenv/lib/python2.7/site-packages/webssh/wssh.spec”,内容如下:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['webssh'],
             binaries=[],
             datas=[
              ('static', 'webssh/static'),
              ('templates', 'webssh/templates')
             ],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='wssh',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

打包

$ cd ~/myenv/lib/python2.7/site-packages/webssh/
$ pyinstaller wssh.spec

运行服务并测试

$ dist/wssh --port=12345

然后通过浏览器访问 http://<ip>:12345 来验证。