Pyinstaller executable fails importing torchvision - python

This is my main.py:
import torchvision
input("Press key")
It runs correctly in the command line: python main.py
I need an executable for windows. So I did : pyinstaller main.py
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
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.
Additional info:
I recently installed Visual Studio 2019
System info:
Window 10
Python 3.7
torch-1.1.0
torchvision-0.3.0
[EDIT]
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...

Downgrade torchvision to the previous version fix the error.
pip uninstall torchvision
pip install torchvision==0.2.2.post3

Try this monkey patch.
def script_method(fn, _rcb=None):
return fn
def script(obj, optimize=True, _frames_up=0, _rcb=None):
return obj
import torch.jit
torch.jit.script_method = script_method
torch.jit.script = script

The monkey patch didn't work for me since I had the error when importing torch.jit.
So before importing torch I used the following workaround in my main.py:
os.environ["PYTORCH_JIT"] = "0"
Obviously you lose the JIT optimization but at least, the executable works.

Related

"Failed to execute script " pyinstaller

I have a python code that I wanted to turn into an exe.
I used pyinstaller with : pyinstaller --onefile -w script.py
After it finished making the exe file I double clicked the file but I got "Failed to execute script".
I also tried running it from the cmd but it gives the same fatal error.
Stuff to add:
The code imports a couple of files packages including a python code I made, as well as it makes files referenced to it's location.
Is there something I'm doing wrong?
the script has those imports:
import socket
import os
from PIL import ImageGrab
import cv2
import time
import json
import myFile
I ran the code under cmd and it gives this error:
File "script.py", line 3, in <module>
from PIL import ImageGrab
ModuleNotFoundError: No module named 'PIL'
Might be unrelated but now that I tried to do pyinstaller --onefile -w client.py.
After I ran it windows defender found this inside :
after running it in the terminal in pycharm with :
pyinstaller --onefile --hidden-import=PIL --hidden-import=Pillow --hidden-import=pynput client.py
I get this error (note that i moved it from the dist directory to the main one):
Traceback (most recent call last):
File "client.py", line 7, in <module>
import myFile
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "myFile.py", line 1, in <module>
from pynput import mouse, keyboard
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 82, in backend
ImportError
[13364] Failed to execute script client
This may be due to pyinstaller not being able to properly find your dependencies and skipping some packages.
To fix any error like ModuleNotFoundError: No module named 'PIL' just add it as a hidden import:
pyinstaller --onefile --hidden-import=PIL -w script.py
For the second error this is a known issue with pyinstaller and pynput.
Find here some explanation.
The TLDR of it seems to be that you need to add --hidden-import=pynput.mouse._win32 --hidden-import=pynput.keyboard._win32 --hidden-import=pynput._util._win32 and any other sub-packages that give you errors.

Pyautogui problems

Recently I was working with the module pyautogui when I got an error, at first I dismissed it but then when I tried to run other programs the same error, referring to the pyautogui program popped up even though the other program had nothing to do with pyautogui. This was all in VSC, so after opening and closing VSC a few times and the error still occurred when I imported any module I decided to build my programs in terminal instead. The same error shows up. Below I have pasted the error from my terminal which will still not allow me to make any python module import whatsoever. Any help is greatly appreciated.
I have upgraded, uninstalled, and reinstalled everything and still the same
>>> import random
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Aezene\random.py", line 15, in <module>
StartLocation = pyautogui.locateOnScreen('Start.png')
File "C:\Users\Aezene\AppData\Roaming\Python\Python39\site-packages\pyautogui\__init__.py",
line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "C:\Users\Aezene\AppData\Roaming\Python\Python39\site-packages\pyautogui\__init__.py",
line 213, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
File "C:\Users\Aezene\AppData\Roaming\Python\Python39\site-packages\pyscreeze\__init__.py",
line 359, in locateOnScreen
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return
accurate coordinates, so don't pass a region here.
File "C:\Users\Aezene\AppData\Roaming\Python\Python39\site-packages\pyscreeze\__init__.py",
line 134, in wrapper
raise PyScreezeException('The Pillow package is required to use this function.')
pyscreeze.PyScreezeException: The Pillow package is required to use this function.
>>>
This happens because you haven't downloaded the "Pillow" package. You can download by using the command "pip install Pillow" or by using the following code:
from subprocess import *
from sys import *
call([executable, "-m", "pip, "install", "Pillow"])
You need to have this module because pyautogui's code won't run without it. If you install PyCharm and look at PyAutoGui's file code, you'll see that it is necessary to install the module

how use pyinstaller with opencv in raspbian?

i wrote this python script "mycv.py" in raspbian:
import cv2 as cv
and it debug and run currectly. then i use pyinstaller to make executable file.
but when run file in terminal. it has error:
Traceback (most recent call last):
File "mycv.py", line 1, in <module>
import cv2 as cv
File "/usr/local/lib/python3.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "cv2/__init__.py", line 89, in <module>
File "cv2/__init__.py", line 58, in bootstrap
File "cv2/__init__.py", line 56, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
[28400] Failed to execute script mycv
Any help is appreciated
After lots of debugging, I found the following solution:
Get the path of OpenCV
import cv2
print(cv2.file) # /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.so
Add this path while compiling through pyinstaller
pyinstaller main.py -n myApp --paths="/usr/local/lib/python3.6/dist-packages/cv2/python-3.6"
I hope this helps others also

Struggling with imports in Python

I'm just sruggling with importing modules from nested packages in Python.
After execute command in project root directory:
$ nosetests
Unfortunatelly, i'm still getting logs like this:
======================================================================
ERROR: Failure: ImportError (No module named io_file)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/user/dev/ease-ci/easeci-core/tests/lib/io/test_io_facade.py", line 4, in <module>
from lib.io.io_file import file_load, File, file_exist, file_save, file_delete, file_change
ImportError: No module named io_file
And more and more errors like that.
Can someone tell me something help me to resolve my issue? Thanks.
Pycharm put me in error, because if I run test by green arrow, everything is ok.
I'm guessing that you are trying to install a library without pip installed because you used pycharm in a weird way.
Try adding a path to the library which you have to download and then run the import within the code. Cheers!
import tensorflow as tf
https://www.jetbrains.com/help/pycharm/absolute-path-variables.html

PYTHON DLL load failed

I usually code in Matlab but I found a nice piece of PYTHON code that I would like to use. However having downloaded the package it is proving difficult to run. I'm getting the following error:
Traceback (most recent call last):
File "C:\launch.py", line 29, in <module>
from src.smcsquare import SMCsquare
File "C:\src\smcsquare.py", line 32, in <module>
from scipy.stats import norm
File "C:\Python34\lib\site-packages\scipy\stats\__init__.py", line 338, in <module>
from .stats import *
File "C:\Python34\lib\site-packages\scipy\stats\stats.py", line 184, in <module>
import scipy.special as special
File "C:\Python34\lib\site-packages\scipy\special\__init__.py", line 586, in <module>
from ._ufuncs import *
ImportError: DLL load failed: The specified module could not be found.
The _ufuncs.pyd is there in the C:\Python34\lib\site-packages\scipy\special\ directory. I tried adding this to my PYTHONPATH but it made no difference. I have also tried so dll fixers but these have not helped.
Has anyone encountered this and did you find a solution?
As other have said, make sure your .whl file matches the version and 32/64bit of the python distribution you're using.
Next, the problem I was having was I forgot to download and install the extra "numpy+mkl" package per the instruction: http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
So for me it was numpy-1.11.0+mkl-cp35-cp35m-win_amd64.whl, which I downloaded and then:
python -m pip install numpy-1.11.0+mkl-cp35-cp35m-win_amd64.whl
I had already installed the regular numpy package via pip, but I just installed this one over it and everything started working and has been fine so far.

Categories

Resources