PyInstaller Hidden Imports - python

I have a fully working Python program that uses the following imports:
import json
import requests
from natsort import natsorted
However, when I try to compile it to an executable using PyInstaller, I get the following errors:
Note: May not be perfect, as it was captured by my reaction times and a screenshot:
File "site-packages\requests\packages\urllib3\packages\six.py", line 82, in _import_module
ImportError: No module named 'queue'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "huepy3.py", line 2, in <module>
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimodo3_importer
py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\__init__.py", line 63, in <module>
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimodo3_importer
py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\utils.py", line 24, in <module>
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimodo3_importer
py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\_internal_utils.py", line 11, in <module>
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimodo3_importer
py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\compat.py", line 11, in <module>
File "c:\users\andrew\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimodo3_importer
py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\packages\__init__.py", line 29, in <module>
ImportError: No module named 'urllib3'
Failed to execute script huepy3
I read the error, and tried to import urllib3 in my code as well. That returned the same errors as well. I'm thinking it's down to the location of urllib3 in relation to requests, but how can I solve this? I read online about FileDialog, and again it returned the same error.
Any pointers on this would be much appreciated.
Edit: It's not just the queue import problem, it's more of how to change the paths the library requests is trying to import from and how pyinstaller is going to get those paths in the first instance.
Edit 2: Ok, maybe I should ask how PyInstaller compiles the dependencies it finds and where it stores them? I could then give it a shot myself.
Edit 3: Through hours of crawling the web I believe I might be after hidden imports. How do those works for getting urllib3 in?

You can either:
update the spec file manually to include urllib3
or
run pyinstaller with the argument --hidden-import=urllib3

Related

Can't transform a python program into exe

I want to transform a simple .py code into .exe using PyInstaller, the program works fine with python, but when I transform it into .exe using PyInstaller, it returns the error:
ModuleNotFoundError: No module named numpy.core._dtype.ctypes
I tried installing the ctypes module, but the same error happenened.
How can I fix this error?
.py code:
import numpy
input("Worked")
cmd output:
C:\Users\enzo\Desktop\Compilando\dist\iaf>iaf.exe
Traceback (most recent call last):
File "iaf.py", line 1, in <module>
File "c:\users\enzo\appdata\local\programs\python\python37\lib\site-packages\P
yInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\numpy\__init__.py", line 151, in <module>
File "c:\users\enzo\appdata\local\programs\python\python37\lib\site-packages\P
yInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\numpy\ctypeslib.py", line 369, in <module>
File "site-packages\numpy\ctypeslib.py", line 358, in _get_typecodes
File "site-packages\numpy\ctypeslib.py", line 358, in <dictcomp>
ModuleNotFoundError: No module named 'numpy.core._dtype_ctypes'
[2872] Failed to execute script iaf
after I added to the beginning of the code:
import numpy.core._dtype_ctypes
my code works
spied on KmolYuan
As an alternative solution, you can downgrade your numpy package via
python -m pip install numpy==1.15.0
as there seems to be a problem for pyinstaller using the current numpy release (1.16.0).

Using pyinstaller to package python program and there is a "NoModuleFoundError"

I use pyinstaller to package my python program into an executable program(exe).
my programs versions:
Pyinstaller :3.3.1
Python :3.6.1
Windows:10-10.0.16299-SP0
I can run the data_processing_gui.py file with the command "python data_processing_gui.py" in my command-line window without any errors.
I used this command to package my python program (which its name is data_processing_gui.py):
pyinstaller -F -p C:\Users\fanyu\Anaconda3\Lib;C:\Users\fanyu\Anaconda3\Lib\site-packages;C:\Users\fanyu\Anaconda3\Lib\site-packages\PyQt5\Qt\bin;C:\Users\fanyu\Anaconda3\Lib\site-packages\pydicom;C:\Users\fanyu\Anaconda3\Lib\site-packages\radiomics;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt\_extensions;C:\Users\fanyu\Anaconda3\Lib\site-packages\tensorflow;C:\Users\fanyu\Anaconda3\Lib\site-packages\SimpleITK;C:\Users\fanyu\Anaconda3\Lib\site-packages\PIL;C:\Users\fanyu\Anaconda3\Lib\site-packages\pandas;C:\Users\fanyu\Anaconda3\Lib\site-packages\nrrd;C:\Users\fanyu\Anaconda3\Lib\site-packages\numpy -i C:\Users\fanyu\Desktop\work\package\image\doctor.ico data_processing_gui.py
There is no error when I run code above in windows' command-line window, but when I run the target program(data_progressing_gui.exe), there is a ModuleNotFoundError, and the error information is:
Traceback (most recent call last): File "data_processing_gui.py",
line 12, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\radiomics_init_.py", line 15, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\radiomics\imageoperations.py", line 6, in
File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\pywt_init_.py", line 17, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\pywt_functions.py", line 17, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 714, in load_module
module = loader.load_module(fullname) File "pywt/_extensions/_pywt.pyx", line 1, in init pywt._extensions._pywt
(pywt_extensions_pywt.c:31281) ModuleNotFoundError: No module named
'pywt._extensions._cwt' [7564] Failed to execute script
data_processing_gui
It seems like the program can't find the specified package path (pywt._extensions._cwt), however, I've added the path in the pyinstaller's parameter list.
At first, I thought maybe there was something wrong with my pyinstaller, so I tried to reinstall it. Then I write a simple python program to test it, it runs correctly.
I've tried a lot but just can't solve it, so I am here to ask for help,I will appreciate it if anyone can give me any advice.
I find a solution for my problem in the answers to the following question ModuleNotFoundError: No module named 'pandas._libs.tslibs.timedeltas'
I can't explain why but it does solve my problem.
The solution is:
After generating the ".spec" file using pyinstaller,add the missing module into the ".spec" file at "hiddenimport=[]",like this:
hiddenimport=["pywt","pywt._estentions._cwt"]
Then use this file to generate your exe file:
pyinstaller my.spec

How to fix the no dbm clone found error in pyinstaller

I have made a GUI app which uses subliminal in it . Then i tried compiling the program using PyInstaller . When i run the exe, it gives me the following error :-
Traceback (most recent call last):
File "main.py", line 375, in <module>
File "main.py", line 144, in __init__
File "site-packages\dogpile\cache\region.py", line 237, in configure
File "site-packages\dogpile\cache\backends\file.py", line 158, in __init__
File "site-packages\dogpile\cache\backends\file.py", line 182, in _init_dbm_file
File "dbm\__init__.py", line 75, in open
ImportError: no dbm clone found; tried ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb']
Failed to execute script main
After this i tried explicitly importing dbm in my application as like :-
import dbm . But this also gives the same error.
Then I tried import dbm.gnu , dbm.ndbm , dbm.dumb .
This gives another error as shown below :-
Traceback (most recent call last):
File "main.py", line 3, in <module>
File "c:\program files\python35\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "userinterface.py", line 312, in <module>
File "c:\program files\python35\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "main.py", line 9, in <module>
File "c:\program files\python35\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "dbm\ndbm.py", line 3, in <module>
ImportError: No module named '_dbm'
Failed to execute script main
Some googling and i found that it uses _dbm package from dbm.gdbm . so instead of import dbm.gnu , dbm.ndbm , dbm.dumb , I used import dbm.dumb .
Surprisingly, doing this started my application . But the subliminal doesn't find any subtitle now. So it's total useless now .
What i am doing wrong . whats the right way to fix this problem. while compiling , i have also tried giving --hidden-import dbm with no luck.
Here is the command that i use to compile using pyinstaller :-
pyinstaller main.py --hidden-import dbm --hidden-import dogpile.cache.backends.file --additional-hooks-dir=. --add-data guessit;guessit --add-data babelfish;babelfish -i vid2.ico -n Subtitler
I would be grateful for any clue or help :)
I found that , in _dbm package only works in UNIX like systems .
After some fiddling with it and finally fixed it .
In your main python file just use this import :-
import dbm.dump

Freezing (.exe) a traitsUI program, realistically feasible?

I'm trying to freeze with either cx_freeze or pyInstaller a TraitsUI program that makes use of Chaco, Traits, TraitsUI and to a lesser extent mayavi (could actually be taken out). I need this to run on mac, linux, ubuntu so am avoiding py2exe. I've intentionally uninstalled pyqt and pyside so that only the wx backend is available.
Using cx_freeze, I encountered and reported a bug, so pyInstaller seems to be getting me the furthest. It generated an .exe file, but when I run the file I get warnings and an error. To generate the .exe file I did:
pyinstaller --onefile --name=pameapp pamemain.py
pyinstaller pameapp.exe
Upon running, the warnings are:
Warning: Unable to import the wx backend for pyface due to traceback: Traceback (most recent call last):
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
ImportError: No module named wx.init
Warning: Unable to import the qt4 backend for pyface due to traceback: Traceback (most recent call last):
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
ImportError: No module named init
Warning: Unable to import the null backend for pyface due to traceback: Traceback (most recent call last):
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 45, in _init_toolkit
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/pyface.toolkit", line 31, in import_toolkit
ImportError: No module named null.init
In addition, I get the following error:
Traceback (most recent call last):
File "<string>", line 8, in <module>
File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.api", line 35, in <module>
File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors", line 22, in <module>
File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.api", line 10, in <module>
File "/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.code_editor", line 36, in <module>
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.editors.code_editor", line 48, in ToolkitEditorFactory
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traits.traits", line 487, in __call__
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traits.traits", line 1191, in Color
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.toolkit_traits", line 7, in ColorTrait
File "/home/glue/Desktop/fibersim/pame/build/pameapp/out00-PYZ.pyz/traitsui.toolkit", line 137, in toolkit
traits.trait_errors.TraitError: Could not find any UI toolkit called 'null'
pyface probably has a lot of dynamic imports, so this must be a problem encountered by anyone who tries to freeze ETS. Is there a single fix to get this working?
Considering the older stuff floating around the web on success rates to freezing an ETS program, is it even worth further investment? To us, having people avoid configuring python for this application would be a huge plus.
Thanks.
Pyface.toolkit you are using is using dynamic import:
def import_toolkit(tk):
try:
# Try and import the toolkit's pyface backend init module.
be = 'pyface.ui.%s.' % tk
__import__(be + 'init')
except:
raise
return be
So pyinstaller is not able to detect this. Check with python -v b.py 2>&1 | grep imp what is the specific import and add hook file (hook-.py) with proper hiddenimports list.
However I've did some tests in local and I am not able to do working test schema with __import__. It's strange as I did some debugging and I found that hiddenimport is added but still not usable.

PyInstaller 2.1 IOError

I'm trying to turn my GUI into an executable file using Pyinstaller. The GUI uses vpython, matplotlib and some other standard python libraries, I have checked that I can import all the modules in the command console using python. I'm building using the command,
python pyinstaller.py -p C:\Python27\Lib\site-packages GUI.py
The program builds, but when I try to run it I get errors that I don't really understand.
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "C:\Users\James\Desktop\PyInstaller-2.1\PyInstaller\loader\pyi_importers.
py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\James\Desktop\PyInstaller-2.1\GUI\build\GUI\out00-PYZ.pyz\visua
l", line 4, in <module>
File "C:\Users\James\Desktop\PyInstaller-2.1\PyInstaller\loader\pyi_importers.
py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\James\Desktop\PyInstaller-2.1\GUI\build\GUI\out00-PYZ.pyz\visua
l_common.create_display", line 35, in <module>
File "C:\Users\James\Desktop\PyInstaller-2.1\PyInstaller\loader\pyi_importers.
py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\James\Desktop\PyInstaller-2.1\GUI\build\GUI\out00-PYZ.pyz\visua
l_common.materials", line 159, in <module>
File "C:\Users\James\Desktop\PyInstaller-2.1\GUI\build\GUI\out00-PYZ.pyz\visua
l_common.materials", line 129, in loadTGA
IOError: [Errno 2] No such file or directory: 'C:\\Users\\James\\Desktop\\PYINST
~1.1\\GUI\\dist\\GUI\\visual_common/turbulence3.tga'
swig/python detected a memory leak of type 'wxPlatformInfo *', no destructor fou
nd.
C:\Users\James\Desktop\PyInstaller-2.1\GUI\dist\GUI>
Any help appreciated.
I was able to fix this after reading,
http://osdir.com/ml/python-visualpython-user/2009-07/msg00039.html
The tl;dr is that there is some code in materials.py in Vpython which sets the texture path,
if hasattr(sys,'frozen') and (sys.frozen=="windows_exe" or sys.frozen=="console_exe"):
texturePath="visual\\"
else:
texturePath = os.path.split( __file__ )[0] + "/"
del sys
I commented this out and replaced it with a static path. Then I copied the tga files from Vpython into this path. There is probably a more elegant solution but this workaround is fine for me.
i fixed like that :
#texturePath="visual\\" --> texturePath="visual_common\\"
#texturePath = os.path.split( __file__ )[0] + "/" -->
texturePath="C:\Python27\Lib\site-packages\\visual_common\\"
Regards

Categories

Resources