gpt4 book ai didi

python - Ubuntu 20.04LTS GUI 上 Pynput 和 Pyinstaller 的问题

转载 作者:行者123 更新时间:2023-12-04 11:55:11 27 4
gpt4 key购买 nike

我有一个使用 Pynput 模块的 python 脚本。当我在 Ubuntu [20.04LTS GUI] 上从终端运行 python 脚本时,它运行完美。

$ pyinstaller --onefile vTwo.py
cd ./dist
./vTwo
运行 ./script 时发生错误:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'

Try one of the following resolutions:

* Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly
[5628] Failed to execute script vTwo
如果有人可以就可能出现的问题向我提出建议。我看过 Pynput 要求页面,他们提到它需要 X 服务器在后台运行,这应该不是问题,因为我安装了 GUI。
还有无论如何在没有gui的系统上使用Pynput吗?

最佳答案

解决方案
解决方法很简单。只需将此模块作为隐藏导入包含在 PyInstaller 程序中:

python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg
如果您还使用带有 pynput 的鼠标,那么您将在模块 pynput.mouse._xorg 上遇到相同的错误。 .所以这样做:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg
警告!如果您为 Windows 或 Mac 打包,您可能会得到一个它找不到的不同模块。这就是你在 Linux 上得到的。如果您希望您的程序是跨平台的,那么您必须打包该程序,例如,用于 Windows 并对其进行测试以查看它没有找到哪个模块并将其作为隐藏导入包含在内。
例如,如果您希望您的程序在 Linux 和 Windows 上运行,请使用以下命令:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg --hidden-import=pynput.keyboard._win32 --hidden-import=pynput.mouse._win32
如果你有很多隐藏的模块,那么你可以编辑 .spec 文件并将模块添加到 hiddenimports像这样列出(在 PyInstaller 4.1 上):
hiddenimports=['pynput.keyboard._xorg', 'pynput.mouse._xorg'],
为什么错误
当您看到 ImportError在 PyInstaller 打包的 Python 程序中,问题很可能是 PyInstaller 无法检测到该特定导入并且未将其包含在二进制文件中,因此出现导入错误。
在错误消息中,它告诉您它没有找到哪个模块:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'
这是 pynput.keyboard._xorg ,因为您使用的是 Linux。
它找不到该模块,因为它是以“非传统”方式导入的。查看 pynput/_util/__init__.py的源代码在 backend功能:
def backend(package):
backend_name = os.environ.get(
'PYNPUT_BACKEND_{}'.format(package.rsplit('.')[-1].upper()),
os.environ.get('PYNPUT_BACKEND', None))
if backend_name:
modules = [backend_name]
elif sys.platform == 'darwin':
modules = ['darwin']
elif sys.platform == 'win32':
modules = ['win32']
else:
modules = ['xorg']

errors = []
resolutions = []
for module in modules:
try:
return importlib.import_module('._' + module, package)
except ImportError as e:
errors.append(e)
if module in RESOLUTIONS:
resolutions.append(RESOLUTIONS[module])

raise ImportError('this platform is not supported: {}'.format(
'; '.join(str(e) for e in errors)) + ('\n\n'
'Try one of the following resolutions:\n\n'
+ '\n\n'.join(
' * {}'.format(s)
for s in resolutions))
if resolutions else '')
你可以看到它使用了 import_module来自 importlib 的函数模块为平台导入正确的模块。这就是为什么它找不到 pynput.keyboard._xorg模块。
你的第二个问题
还有无论如何在没有gui的系统上使用Pynput吗?
我不知道。

关于python - Ubuntu 20.04LTS GUI 上 Pynput 和 Pyinstaller 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64870717/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com