Pyinstaller可执行文件导入torchvision失败 [英] Pyinstaller executable fails importing torchvision

查看:537
本文介绍了Pyinstaller可执行文件导入torchvision失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 main.py :

import torchvision
input("Press key")

它在命令行中正确运行:python main.py

It runs correctly in the command line: python main.py

我需要Windows的可执行文件.所以我做了:pyinstaller main.py

I need an executable for windows. So I did : pyinstaller main.py

但是当我启动 main.exe 时,在/dist/main内出现了此错误:

But when I launched the main.exe, inside /dist/main I got this error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>

  ... (omitted)

  File "site-packages\torchvision\ops\misc.py", line 135, in <module>
  File "site-packages\torchvision\ops\misc.py", line 148, in FrozenBatchNorm2d
  File "site-packages\torch\jit\__init__.py", line 850, in script_method
  File "site-packages\torch\jit\frontend.py", line 152, in get_jit_def
  File "inspect.py", line 973, in getsource
  File "inspect.py", line 955, in getsourcelines
  File "inspect.py", line 786, in findsource
OSError: could not get source code
[2836] Failed to execute script main

似乎某些源代码未从pyinstaller正确导入.我不确定问题是 torch 模块还是 torchvision .

It seems that some source code is not correctly imported from pyinstaller. I am not sure if the problems is the torch module or torchvision.

其他信息:

  • 我最近安装了Visual Studio 2019

系统信息:

  • 窗口10
  • Python 3.7
  • 火炬1.1.0
  • torchvision-0.3.0

我发现问题出在torchvision中的类 FrozenBatchNorm2d 的定义中.以下脚本与之前发布的脚本产生相同的错误:

I found that the problem is in the definition of the class FrozenBatchNorm2d inside torchvision. The following script produce the same error as the one before posted:

main.py

import torch

class FrozenBatchNorm2d(torch.jit.ScriptModule):

    def __init__(self, n):
        super(FrozenBatchNorm2d, self).__init__()

    @torch.jit.script_method

    def forward(self):
        pass

我复制了所有的割炬源文件.但是我仍然遇到错误...

I copied all the torch source file. But I still got the error...

推荐答案

torchvision 降级到以前的版本可修复该错误.

Downgrade torchvision to the previous version fix the error.

pip uninstall torchvision
pip install torchvision==0.2.2.post3

这篇关于Pyinstaller可执行文件导入torchvision失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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