pyinstaller ModuleNotFoundError [英] pyinstaller ModuleNotFoundError

查看:350
本文介绍了pyinstaller ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 tensorflow 构建了一个 python 脚本,现在我正在尝试将其转换为 .exe 文件,但遇到了问题.使用 pyinstaller 并从命令提示符运行程序后,我收到以下错误:

I have built a python script using tensorflow and I am now trying to convert it to an .exe file, but have ran into a problem. After using pyinstaller and running the program from the command prompt I get the following error:

File "site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 25, in <module> ModuleNotFoundError: No module named 'tensorflow.python.platform'

我尝试过 --hidden-import tensorflow.python.platform 但它似乎没有修复任何问题.(程序在解释器中运行得很好)您的帮助将不胜感激.

I have tried --hidden-import tensorflow.python.platform but it seems to have fixed nothing. (The program runs just fine in the interpreter) Your help would be greatly appreciated.

推荐答案

最新版本的 PyInstaller (4.0+) 现在包括对 tensorflow 开箱即用的支持.

The latest versions of PyInstaller (4.0+) now include support for tensorflow out of the box.

创建一个这样的目录结构:

Create a directory structure like this:

- main.py  # Your code goes here - don't bother actually naming you file this
- hooks
  - hook-tensorflow.py

将以下内容复制到hook-tensorflow.py:

from PyInstaller.utils.hooks import collect_all


def hook(hook_api):
    packages = [
        'tensorflow',
        'tensorflow_core',
        'astor'
    ]
    for package in packages:
        datas, binaries, hiddenimports = collect_all(package)
        hook_api.add_datas(datas)
        hook_api.add_binaries(binaries)
        hook_api.add_imports(*hiddenimports)

然后在编译的时候添加命令行选项--additional-hooks-dir=hooks.

Then, when compiling, add the command line option --additional-hooks-dir=hooks.

如果您遇到更多未找到的错误,只需将完整的导入名称添加到packages列表中即可.

If you come across more not found errors, simply add the full import name into the packages list.

PS - 对我来说,main.py 只是 from tensorflow import *

PS - for me, main.py was simply from tensorflow import *

这篇关于pyinstaller ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆