我创建了一个应用,只是像下面的代码一样打开浏览器,并且在cmd行中正常运行。
但是,当我用pyinstaller打包时,它不起作用..
出什么问题了? 感谢您在这种情况下的帮助。
# main.py
# -*- coding: utf-8 -*-
import os
from selenium import webdriver
if __name__ == '__main__':
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")
browser = webdriver.Chrome(executable_path=DRIVER_BIN)
browser.get('https://google.com/')
这是.spec文件。
# -*- mode: python -*-
import os
project_path = os.path.abspath(os.path.curdir)
block_cipher = None
a = Analysis(['main.py'],
pathex=[project_path],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False)
app = BUNDLE(exe,
name='test.app',
icon=None,
bundle_identifier=None)
文件树就是这样。
dist/
test.app
test
chromedriver <- add after packaged
错误也许就是这个。
Jun 26 14:42:01 MacBookPro ctkahp[67399]: objc[67399]: Class TKTokenRefCtkd is implemented in both /System/Library/Frameworks/Security.framework/Versions/A/Sec urity (0x7fff8a3cf0a0) and /System/Library/Frameworks/CryptoTokenKit.framework/ctkahp.bundle/Contents/MacOS/ctkahp (0x10ecbc760). One of the two will be used. Which one is undefined.
Jun 26 14:42:16 MacBookPro com.apple.xpc.launchd[1] (highlow.23356[67408]): Service exited with abnormal code: 255
答案 0 :(得分:0)
我找到了解决方法。
我试图将完整路径设置为可执行文件路径。然后工作正常。
但是我不明白为什么这是问题。
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")
browser = webdriver.Chrome(executable_path=DRIVER_BIN)
还有这个。
browser = webdriver.Chrome(executable_path="./chromedriver")
.app文件在哪里查看?
答案 1 :(得分:0)
命令执行的测试脚本的路径与“捆绑包”不同。
您可以尝试一下
main.py
import os
import sys
if __name__ == "__main__":
if getattr(sys, 'frozen', False):
chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver")
driver = webdriver.Chrome(chromedriver_path)
else:
driver = webdriver.Chrome()
main.spec
project_path = os.path.abspath(os.path.curdir)
a = Analysis(['main.py'],
pathex=[project_path],
binaries=[('/Users/xxx/xxx/chromedriver', './')],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
(参考号:https://github.com/pyinstaller/pyinstaller/issues/1726)
(ref .: Running pyinstaller another pc with Chromedriver)