Linux下基于Pycharm的wxpython程序环境配置和打包

5 篇文章 0 订阅
3 篇文章 0 订阅

1.Linux自带的Python未包含include目录及头文件,首先根据代码所使用的Python版本安装 libpython*-dev,如果缺少头文件,使用Pyinstaller会报错,例如提示 找不到pyconfig.h 文件

2.打开Pycharm,配置venv环境,注意Python的版本号要与代码对应,在设置-Project Interpreter选项中安装依赖模块(若在此处搜索并安装wxpython会报错);

3.由于官方并没有发布统一兼容Linux的wxpython,所以需要根据官网提供的wheel地址,自行安装到venv环境:
1)参考wxpython官网描述,手动安装支持Ubuntu版本基于GTK3的wxpython,目前官网发布了Ubuntu16.04和18.04两个版本,非Ubuntu版本可以都尝试一下:

官网的描述:
Since there are various options for distro and wx port (GTK2 or GTK3) then the files can not all be located in the same folder for easy access by pip. This simply just means that you'll need to drill down a little further to find the URL to give to pip. For example, to get the GTK3 wxPython builds for Ubuntu 16.04 (and 16.10, LinuxMint 18, and probably others) you can use a pip command like this:

pip install -U \
    -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 \
    wxPython

Of course you can always download the wheel file yourself and then use pip to install your local copy of the file.

注意 官网提供的安装方法会在安装的时候自动选择wxpython模块包,如果本地的环境是Python2.7,自动选择可能下载的是基于Python3.*的模块包,例如会报 “无法识别 -> 操作符” 错误

解决方案: 手动选择模块包并安装 , 访问https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04,选择合适的包,执行如下命令:
pip install -U https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04\选择的包名
例如 我本地的环境python2.7.14,安装最新的wxpython4.0.3, 则执行:
pip install -U https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04\wxPython-4.0.3-cp27-cp27mu-linux_x86_64.whl

注意安装过程中提示的错误,有可能是系统缺失对应的包,可以尝试使用sudo apt-get 安装缺失的包后重新执行pip
或者更换地址中的Ubuntu版本,具体可以访问https://extras.wxpython.org/wxPython4/extras/linux/去看

4.安装后没有意外就可以运行代码了,如果运行时报缺少库,同上,使用sudo apt-get安装对应的库文件;

5.使用Pyinstaller打包,注意第一条,然后参考如下编写脚本:
../venv_linux/27/bin/pyinstaller -F main.spec --clean --distpath=./out_linux --workpath=./out_linux/tmp --key=1234567

spec 文件是在首次执行时自动生成的基础上修改得到的,首次执行的指令可以参考如下(windows下例子,linux下需要修正路径):
..\venv\Scripts\pyinstaller.exe -i aw_logo.ico -w -F ..\Main.py --add-data=..\venv\Lib\site-packages\xpinyin\Mandarin.dat;xpinyin --clean --distpath=.\out --workpath=.\out\main --key=1234567
:: --add-data=参数:windows下为“源路径;目标目录",linux下为“源路径:目标目录”

稍微复杂的程序一般情况下一次打包并不会成功,如果打包后运行提示缺少模块,需要修改自己的spec并重新打包尝试运行, 如下是一份比较完整的spec参考(兼容windows、linux):

# -*- mode: python -*-

block_cipher = None


a = Analysis(['../main/main.py'],
             pathex=['.'],
             binaries=[],
             datas=[('../venv/Lib/site-packages/xpinyin/Mandarin.dat', 'xpinyin')],
             hiddenimports=['gevent.__hub_local', 'gevent.__greenlet_primitives', 'gevent.__waiter', 'gevent.__hub_primitives', 'gevent._greenlet', 'gevent.__ident', 'gevent.libev.corecext', 'gevent.libuv.loop', 'gevent._event', 'gevent._queue', 'gevent.__semaphore', 'gevent.__imap'],
             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='upload_client_main',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False , icon='logo_upload.ico')
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值