`PyInstaller` 默认使用运行它的 Python 解释器来打包您的脚本¹。如果您想要指定一个不同的 Python 解释器,您可以使用完整路径来运行 `PyInstaller`³。例如,如果您想要使用位于 `/path/to/python3` 的 Python 3 解释器,您可以使用以下命令:
```bash
/path/to/python3 -m PyInstaller your_script.py
```
这样,`PyInstaller` 就会使用您指定的 Python 解释器来打包您的脚本³。
另外,如果您想要指定特定版本的 Python 和库,您可以使用虚拟环境²。例如,如果您使用 `conda`,您可以按照以下步骤操作:
1. 创建虚拟环境并指定 Python 版本:
```bash
conda create --name my_env_name python=3.6
```
2. 激活虚拟环境:
```bash
conda activate my_env_name
```
3. 在虚拟环境中安装您需要的库版本和 `PyInstaller`:
```bash
pip install pandas==0.23
pip install beautifulsoup4==4.9.1
pip install pyinstaller
```
4. 在虚拟环境中运行 `PyInstaller` 来打包您的脚本:
```bash
python -m PyInstaller your_script.py
```
这样,`PyInstaller` 就会使用虚拟环境中的 Python 解释器和库来打包您的脚本²。
希望这些信息对您有所帮助!如果您有其他问题,欢迎随时向我提问。¹²³
源: 与必应的对话, 2024/1/17
(1) Using PyInstaller — PyInstaller 6.3.0 documentation. https://pyinstaller.org/en/stable/usage.html.
(2) How to change python version for use with pyinstaller. https://stackoverflow.com/questions/43443088/how-to-change-python-version-for-use-with-pyinstaller.
(3) How to specify Python version and library versions for PyInstaller executable - Stack Overflow. https://stackoverflow.com/questions/63494904/how-to-specify-python-version-and-library-versions-for-pyinstaller-executable.
(4) Python打包工具 Pyinstaller使用教程(将.py以及Python解释器和相关库打包可执行文件) - CSDN博客. https://blog.csdn.net/Dontla/article/details/131474870.
(5) undefined. https://github.com/pyinstaller/pyinstaller/archive/develop.zip.
这个错误可能是由于 `PyInstaller` 在打包过程中没有正确地包含 `imp` 模块。以下是一些可能的解决方案:
1. **手动添加隐藏导入**:在运行 `PyInstaller` 时,使用 `--hidden-import` 参数手动添加 `imp` 模块³。例如,您可以尝试运行以下命令来打包脚本:
```bash
pyinstaller --hidden-import=imp your_script.py
```
这样可以手动将 `imp` 模块添加到 `PyInstaller` 的导入列表中,以确保它能够正确处理脚本中对该模块的引用³。
2. **修改 .spec 文件**:在运行 `PyInstaller` 后,会生成一个 `.spec` 文件。在该文件中,您会找到一个选项,如下:
```python
hiddenimports= []
```
您可以将其修改为:
```python
hiddenimports= ["imp"]
```
然后,像这样运行 `PyInstaller`:
```bash
pyinstaller app.spec
```
如果您的程序不仅仅是一个 "Hello World",可能还需要添加更多的模块。只需将它们全部添加到 `hiddenimports` 列表中,直到不再出现 `ModuleNotFoundError`¹²。
3. **重新安装 PyInstaller**:有时,重新安装 `PyInstaller` 也可以解决问题²。例如,您可以尝试卸载当前的 `PyInstaller`,然后重新安装:
```bash
pip uninstall pyinstaller
pip install pyinstaller
```
请注意,如果您修改了 `.spec` 文件,不要再运行 `pyinstaller` 对 `.py` 文件进行操作,因为这将创建一个新的 `.spec` 文件并覆盖它,忽略之前的修改²。
希望这些信息对您有所帮助!如果问题仍然存在,或者您有其他问题,欢迎随时向我提问。¹²³
源: 与必应的对话, 2024/1/17
(1) Python Pyinstaller “Failed to execute script pyi_rth_pkgres” 和缺少的包 - 极客教程. https://geek-docs.com/python/python-ask-answer/567_python_pyinstaller_failed_to_execute_script_pyi_rth_pkgres_and_missing_packages.html.
(2) python - Pyinstaller "Failed to execute script pyi_rth_pkgres" and missing packages - Stack Overflow. https://stackoverflow.com/questions/37815371/pyinstaller-failed-to-execute-script-pyi-rth-pkgres-and-missing-packages.
(3) Pyinstaller Failed to execute script pyi_rth_pkgres. https://stackoverflow.com/questions/62926929/pyinstaller-failed-to-execute-script-pyi-rth-pkgres.
(4) python - Pyinstaller 'failed to execute' script - Stack Overflow. https://stackoverflow.com/questions/62017437/pyinstaller-failed-to-execute-script.
(5) 亲测有效!python:Failed to execute script ‘pyi_rth_win32comgenpy‘ due to unhandled exception! - CSDN博客. https://blog.csdn.net/abcdefg5_555/article/details/128636706.
(6) undefined. https://github.com/pyinstaller/pyinstaller/archive/develop.zip.
(7) undefined. https://pypi.tuna.tsinghua.edu.cn/simple.
如果您需要在 `PyInstaller` 中隐藏导入多个库,可以使用 `--hidden-import` 参数,并为每个需要隐藏的库重复该参数¹。例如,如果您需要隐藏 `pandas` 和 `pyautogui` 这两个库,可以使用以下命令:
```bash
pyinstaller main.py --hidden-import pandas --hidden-import pyautogui
```
这样,`PyInstaller` 就会将 `pandas` 和 `pyautogui` 都添加到隐藏导入列表中¹。
另外,您也可以在 `.spec` 文件中添加多个隐藏导入。在 `.spec` 文件中,您会找到一个选项:
```python
hiddenimports= []
```
您可以将其修改为:
```python
hiddenimports= ["pandas", "pyautogui"]
```
然后,像这样运行 `PyInstaller`:
```bash
pyinstaller app.spec
```
这样,`PyInstaller` 就会将 `pandas` 和 `pyautogui` 都添加到隐藏导入列表中²。
希望这些信息对您有所帮助!如果您有其他问题,欢迎随时向我提问。¹²
源: 与必应的对话, 2024/1/17
(1) python - Multiple Hidden Imports in Pyinstaller - Stack Overflow. https://stackoverflow.com/questions/67661226/multiple-hidden-imports-in-pyinstaller.
(2) Using PyInstaller — PyInstaller 6.3.0 documentation. https://pyinstaller.org/en/stable/usage.html.
(3) python - How to properly create a pyinstaller hook, or maybe hidden import? - Stack Overflow. https://stackoverflow.com/questions/27947639/how-to-properly-create-a-pyinstaller-hook-or-maybe-hidden-import.
(4) [转]pyinstaller打包讲解 -F,-D,-w,-c,以及--hidden-import的用法 - CSDN博客. https://blog.csdn.net/qq_39621009/article/details/122308590.
(5) Hidden imports from a top-level module - Google Groups. https://groups.google.com/g/pyinstaller/c/FnwpTM9hyRI.
标签:pyinstaller,script,python,imp,导入,https,PyInstaller,com From: https://blog.51cto.com/u_16055028/9291353