我正在尝试使用 pyinstaller 将 python 游戏构建到二进制文件中。我可以正常使用,但今天我改用 Arcade 而不是 pygame。我可以在本地构建这个罚款,但是当 pyinstaller 构建时,pyglet 会抛出一个异常,说它找不到 X11 库。我在构建 xorg 之前安装了它,并且它不会抛出异常,只是说它无法连接到显示服务器。这是完整的回溯:
$ pyinstaller -F biogame.spec
39 INFO: PyInstaller: 4.1
39 INFO: Python: 3.8.5
50 INFO: Platform: Linux-4.19.78-coreos-x86_64-with-glibc2.29
52 INFO: UPX is not available.
54 INFO: Extending PYTHONPATH with paths
['/builds/TabulateJarl8/biogame', '/builds/TabulateJarl8/biogame']
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/pyglet/__init__.py", line 337, in __getattr__
return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'Window'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/pyinstaller", line 8, in <module>
sys.exit(run())
File "/usr/local/lib/python3.8/dist-packages/PyInstaller/__main__.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "/usr/local/lib/python3.8/dist-packages/PyInstaller/__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/PyInstaller/building/build_main.py", line 720, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "/usr/local/lib/python3.8/dist-packages/PyInstaller/building/build_main.py", line 667, in build
exec(code, spec_namespace)
File "biogame.spec", line 8, in <module>
a = Analysis(['biogame.py'],
File "/usr/local/lib/python3.8/dist-packages/PyInstaller/building/build_main.py", line 212, in __init__
self.hookspath += list(entry_point.load()())
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/local/lib/python3.8/dist-packages/arcade/__init__.py", line 72, in <module>
from .window_commands import close_window
File "/usr/local/lib/python3.8/dist-packages/arcade/window_commands.py", line 106, in <module>
def get_window() -> pyglet.window.Window:
File "/usr/local/lib/python3.8/dist-packages/pyglet/__init__.py", line 343, in __getattr__
__import__(import_name)
File "/usr/local/lib/python3.8/dist-packages/pyglet/window/__init__.py", line 1897, in <module>
gl._create_shadow_window()
File "/usr/local/lib/python3.8/dist-packages/pyglet/gl/__init__.py", line 206, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False)
File "/usr/local/lib/python3.8/dist-packages/pyglet/window/xlib/__init__.py", line 173, in __init__
super(XlibWindow, self).__init__(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/pyglet/window/__init__.py", line 585, in __init__
display = pyglet.canvas.get_display()
File "/usr/local/lib/python3.8/dist-packages/pyglet/canvas/__init__.py", line 94, in get_display
return Display()
File "/usr/local/lib/python3.8/dist-packages/pyglet/canvas/xlib.py", line 123, in __init__
raise NoSuchDisplayException('Cannot connect to "%s"' % name)
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None"
我尝试运行
xinit
,但由于没有任何显示而出错,我尝试了xrandr
,但也失败了,我也尝试过执行export DISPLAY=:0
,但没有帮助。有没有办法让 X11 在 Gitlab CI 上运行,或者这是不可能的?这是我的 CI 文件中相关的部分:
ubuntubuild:
image: ubuntu:latest
stage: ubuntubuild
before_script:
- apt update
- DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends python3-pip python3-dev libjpeg-dev zlib1g-dev xorg
- python3 -m pip install pyinstaller
- python3 -m pip install -r requirements.txt
script:
- pyinstaller -F biogame.spec
artifacts:
paths:
- dist/biogame
更新1:
我设法让 Xvfb 运行,但现在 pyglet 抛出一个错误,提示
pyglet.gl.glx_info.GLXInfoException: pyglet requires an X server with GLX
。我尝试将 Xvfb 调整为 Xvfb :1 -screen 0 1152x900x8 +extension GLX +render -noreset &
,并且我尝试安装这些软件包:mesa-common-dev libgl1-mesa-glx libgl1-mesa-dev libglu1-mesa-dev
我终于知道如何解决它了。我安装了这些软件包并使用
xvfb-run
运行 pyinstaller
sudo apt install xorg-dev libglu1-mesa libgl1-mesa-dev xvfb libxinerama1 libxcursor1
xvfb-run -a -s "-screen 0 1400x900x24 +extension RANDR" -- pyinstaller -F biogame.spec
我在为 KiCAD 项目构建 交互式 HTML BOM 时遇到了同样的问题。
这是我最终的
.gitlab-ci.yml
文件,未对 /etc/gitlab-runner/config.toml
文件进行任何修改。
build-bom:
stage: build_bom
image: kicad/kicad:7.0.11
before_script:
- |
pwd
# /builds/<subgroup>/<project>
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y xorg xvfb
Xvfb :1 -ac &
export DISPLAY=:1
script:
- |
python3 SCRIPT_USING_DISPLAY.py ...
我使用标准的 docker 执行器来配置运行器,如下
[[runners]]
name = "someName"
url = "your-url"
id = 1234
token = "asdf"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "alpine:3.21"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
network_mtu = 0