LoginSignup
1
3

More than 3 years have passed since last update.

PyWebViewを使ったプロジェクトをPyInstallerでexeにできない

Posted at

現在(PyWebView 3.1時点)、PyWebViewを使ったアプリをそのままPyInstallerでexeにしようとすると、Pyinstaller hook can't find WebBrowserInterop.x64.dllというエラーメッセージでコンパイルが中断する問題が発生しているようです。

大まかには上記Issueの通りなのですが、PyInstaller用のWebhookが正しいパスを指していないようで、エラーになってしまうとのこと。

上記を見た感じ最新版では修正は済んでいるものの、公開モジュールへの反映はまだ という感じのようです。

で、直るまで手作業でwebhookを直して回るのは面倒くさいので、PowerShellスクリプトを書きました。

うちでは、PyInstallerでのコンパイルにPowerShellスクリプトを使っているので、ファイル内でPyInstallerを使う前に、次のコードを呼び出します。

if(Select-String "library = join\(sitepack, 'lib', dll_name\)" -Path .\.venv\Lib\site-packages\PyInstaller\hooks\hook-webview.py){
  Write-Host "> Fix PyInstaller\hooks\hook-webview.py"
  $data = Get-Content .\.venv\Lib\site-packages\PyInstaller\hooks\hook-webview.py | % {$_ -replace "library = join\(sitepack, 'lib', dll_name\)","library = join(sitepack, 'webview', 'lib', dll_name)"}
  $data | Out-File .\.venv\Lib\site-packages\PyInstaller\hooks\hook-webview.py
}

ただし、PowerShell 5.xはBOMなしUTF-8を扱えないため、このままだとPyInstallerが正常に読めないwebhookファイルができてしまいます。

PowerShell 6.xなら問題なく使えるので、事前にChocolateyなどから`pwsh(PowerShell 6.x)をインストールし、PowerShell 6上でこのスクリプトを実行するようにしましょう。

うっかりPowerShell 5.xで実行しそうになってしまったときのために、PowerShell 5.xでスクリプトを起動しようとしたら落とすコードも追加しておきます。

if(!($PSVersionTable["PSCompatibleVersions"].Major -contains 6)){
  Write-Host @'
    This script must be PowerShell version 6 or higher before it will work properly.
    The shell currently running is PowerShell version 5 or lower.
    Use the `pwsh` command to change to PowerShell 6.
'@
  exit
}

これを先頭行に書いておけば安心です。警告が出るので気付くでしょう。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3