如何build一个stand alone 的Python程序

最近使用一些从 apt 下载的应用,用python写的,安装的时候没有问题,没有提示我有依赖错误
一到运行的时候就报错了
比如我运行 kazam

WARNING Kazam - Failed to correctly detect operating system.
/usr/local/lib/python3.8/dist-packages/kazam/backend/webcam.py:24: PyGIWarning: GUdev was imported without specifying a version first. Use gi.require_version('GUdev', '1.0') before import to ensure that the right version gets loaded.
  from gi.repository import GObject, GUdev
/usr/local/lib/python3.8/dist-packages/kazam/backend/gstreamer.py:35: PyGIWarning: Gst was imported without specifying a version first. Use gi.require_version('Gst', '1.0') before import to ensure that the right version gets loaded.
  from gi.repository import GObject, Gst, GstVideo
/usr/local/lib/python3.8/dist-packages/kazam/backend/gstreamer.py:35: PyGIWarning: GstVideo was imported without specifying a version first. Use gi.require_version('GstVideo', '1.0') before import to ensure that the right version gets loaded.
  from gi.repository import GObject, Gst, GstVideo
/usr/local/lib/python3.8/dist-packages/kazam/frontend/window_area.py:29: PyGIWarning: Wnck was imported without specifying a version first. Use gi.require_version('Wnck', '3.0') before import to ensure that the right version gets loaded.
  from gi.repository import Gtk, GObject, Gdk, Wnck, GdkX11
/usr/local/lib/python3.8/dist-packages/kazam/frontend/indicator.py:149: PyGIWarning: AppIndicator3 was imported without specifying a version first. Use gi.require_version('AppIndicator3', '0.1') before import to ensure that the right version gets loaded.
  from gi.repository import AppIndicator3
/usr/local/lib/python3.8/dist-packages/kazam/frontend/window_countdown.py:29: PyGIWarning: PangoCairo was imported without specifying a version first. Use gi.require_version('PangoCairo', '1.0') before import to ensure that the right version gets loaded.
  from gi.repository import Gtk, GObject, GLib, Gdk, Pango, PangoCairo

(kazam:117830): Gtk-WARNING **: 16:48:19.510: Theme parsing error: gtk.css:3:0: Expected a valid selector
/usr/local/lib/python3.8/dist-packages/kazam/frontend/indicator.py:98: PyGIWarning: Keybinder was imported without specifying a version first. Use gi.require_version('Keybinder', '3.0') before import to ensure that the right version gets loaded.
  from gi.repository import Keybinder
WARNING Main - No Xlib support in python3, unable to capture key and mouse clicks.
/usr/local/lib/python3.8/dist-packages/kazam/app.py:173: Warning: value "((GtkIconSize) 32)" of type 'GtkIconSize' is invalid or out of range for property 'icon-size' of type 'GtkIconSize'
  self.builder.add_from_file(os.path.join(prefs.datadir, "ui", "kazam.ui"))
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 110, in _builder_connect_callback
    handler, args = _extract_handler_and_args(obj_or_map, handler_name)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 90, in _extract_handler_and_args
    raise AttributeError('Handler %s not found' % handler_name)
AttributeError: Handler cb_check_cursor_pic not found

(kazam:117830): Gtk-WARNING **: 16:48:19.897: Can't set a parent on widget which has a parent

(kazam:117830): Gtk-WARNING **: 16:48:19.900: Can't set a parent on widget which has a parent
WARNING Prefs - Unable to find any audio devices.
Traceback (most recent call last):
  File "/usr/bin/kazam", line 149, in <module>
    app = KazamApp(datadir, dist, args.debug, args.test, args.nosound, args.silent)
  File "/usr/local/lib/python3.8/dist-packages/kazam/app.py", line 354, in __init__
    self.restore_UI()
  File "/usr/local/lib/python3.8/dist-packages/kazam/app.py", line 1050, in restore_UI
    self.chk_keypresses.set_active(prefs.capture_keys)

我也是醉了,想问下各位有没有办法构建一个独立运行的Python程序,这样运行的时候不再有错误了


草,刚才用 pyinstaller 打包程序,运行二进制文件的时候又出错了

我也在找这个问题的解决方案。pyinstaller 会把库打包进去,比如pandas,体积特别大,简直无法用。同时,还有一些其他问题。

不太明白你这个需求是啥, pyinstaller 本来就是用来生产独立安装包的, 所以要把 python 运行时和依赖库都打进去, 后面安装的时候就不需要 python 命令了.

如果单纯是打包 python 库或者 app 的话, 一般是用 setuptools. 需要在 setup.cfg 或者 setup.py 里面写好依赖, pip 安装的时候会把依赖自动装上.

2 个赞

打包成standalone程序一般是为了给别人使用,比如不会编程的同事,他们的电脑上一般没有python运行环境。有时就是一个用到pandas、Tk库的200行脚本,但分享给同事用时,如果用pyinstaller打包,里面因为会打包进依赖库,所以最后程序会高达几百兆。显然不合理。

1 个赞