Pyenv 提示 error extracting the web portion from the installer 处理

Pyenv 提示 error extracting the web portion from the installer 处理

December 04, 2022
设计 ,

错误信息

最近因为需要用 Python 写个小程序给同事用,但是用 pyinstaller 打包好后在同事的 Win7 系统上运行出错,提示如下错误:

⚠️
无法启动此程序,因为计算机中丢失 api-ms-win-core-path-1-1-0.dll

缺少 DLL 一般都是因为没有运行库,我百度查了下资料,有些帖子说是因为没安装运行库,我试着重新安装了所有运行库以及那片帖子提到的库,但是好像并没有什么效果。

后来 https://cloud.tencent.com/developer/article/1848431 这篇帖子中说到是因为 3.8.6 以后的 Python 版本不在支持 Win7 了,所以需要将 Python 降级到 3.8.6 重新打包就能解决这个问题。

pyenv-win

直接安装肯定会出问题,因为我的电脑中已经安装了 3.11.0 ,因为以前了解过 nodejs 有多版本的环境管理工具,就试着搜索了一下,python 也有一款叫 pyenv-win 的 win 平台版本管理工具。

GitHub - pyenv-win/pyenv-win: pyenv for Windows. pyenv is a simple python version management tool. It lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.
pyenv for Windows. pyenv is a simple python version management tool. It lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of s...

通过 PowerShell 安装

pyenv 可以通过安装包、pip、powershell 等多种方式安装。

其中以 powershell 的方式最为简单,直接运行下面这个命令即可。

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

如果出错了可能是因为你没有设置远程脚本运行权限,执行下面这个代码后再执行上面的安装命令即可。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
参考:https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#installation

pyenv 安装后可以通过下面这些命令进行控制,如: pyenv local 3.10.5pyenv install 3.8.6 等等。

   commands     List all available pyenv commands
   local        (设置当前目录的python版本,没设置默认使用全局的)
   global       (设置全局python版本)Set or show the global Python version
   shell        Set or show the shell-specific Python version
   install      (安装python版本) 
   uninstall    Uninstall 1 or more versions of Python
   update       Update the cached version DB
   rehash       Rehash pyenv shims (run this after switching Python versions)
   vname        Show the current Python version
   version      Show the current Python version and its origin
   version-name Show the current Python version
   versions     List all Python versions available to pyenv
   exec         Runs an executable by first preparing PATH so that the selected Python
   which        Display the full path to an executable
   whence       List all Python versions that contain the given executable

python 安装错误

不过我在安装好 pyenv 后发现在使用 pyenv install 安装 python 版本时提示下面这个错误,我找遍了全网都没有相关的资料,最后还是自己改了它的安装脚本才得以解决。

error extracting the web portion from the installer.
couldn't install 3.8.6

分析 + 解决

pyenv 是使用一个 vbs 脚本安装 python 的(C:\Users\me.pyenv\pyenv-win\libexec\pyenv-install.vbs),我看了下脚本的执行流程,如下:

  1. 下载输入的 python 版本
  2. 将下载下来的文件存入临时文件夹 install_cache(在 pyenv 的安装目录下)
  3. 将安装包解包并移入 pyenv 目录下的 version 目录
  4. .. 后续其他执行流程

我检查了 pyenv 的安装目录,发现 install_cache 目录下是有文件的,但是 version 目录下没有文件,程序执行到第三部就出错了,我后来也通过在脚本里添加输出信息测试了出来。

经过我的测试,程序执行到 If deepExtract Then 便终止了

    If Not objfs.FolderExists(cachePath) Then
        deepExtract = objws.Run(""""& params(IP_InstallFile) &""" /quiet /layout """& cachePath &"""", 0, True)
        If deepExtract Then
            WScript.Echo "在这里出错了。"
            WScript.Echo ":: [Error] :: error extracting the web portion from the installer."
            Exit Function
        End If

结合上下文代码,似乎 deepExtract = objws.Run(""""& params(IP_InstallFile) &""" /quiet /layout """& cachePath &"""", 0, True) 在这一段执行结果没有返回预期值,所以提前进入了错误的判断中,并提前调用了 Exit Function 退出了函数。

所以我尝试注释 Exit Function ,并重新执行安装。

Good,这次成功运行。

其他注意事项

  • 环境变量里以前设置的路径配置都要删除,不然优先级太高,盖过了 pyenv 的设置。
  • 所有的包需要的重新安装。

加入评论