I have written a Python app which makes use of:
from PIL import Image
I want to distribute a packed version of this application so that whoever uses it does not need to install all the dependencies.
For that I have used pyinstaller, without much success.
I run:
pyinstaller --onefile image_download.py
and it does not complain. However, when I run the application, I get:
Traceback (most recent call last):
File "", line 26, in
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.dict)
File "/home/ic/twitter-project/build/image_download/out00-PYZ.pyz/PIL.PngImagePlugin", line 40, in
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.dict)
File "/home/ic/twitter-project/build/image_download/out00-PYZ.pyz/PIL.Image", line 63, in
ImportError: cannot import name _imaging
I am using:
Ubuntu 14.04
Python 2.7
pyinstaller 2.1
PIL.VERSION 1.1.7 (although I thought I had installed Pillow 2.8.1)
The script works fine running as python image_download.py, but pyinstaller is not being able to import _imaging, for some reason.
Any suggestions?
Finally I could not use pyinstaller for this, since I could not fix the import problem.
I managed to build a standalone executable with Nuitka and bbfreeze, though, so in case anyone runs into the same problem: it is worth trying other tools.
Related
I'm using Tkinter on MacOS and its libraries are a little broken, so there is an extension called tkmacosx that fixes many bugs.
All goes fine if I run the program from terminal with python3 file.py or from VSCode.
Then I tried to install it with pyinstaller, goes fine and when running the executable this is the output :
from tkmacosx import Button
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/__init__.py", line 28, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/__init__.py", line 15, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/colorvar.py", line 18, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/utils/__init__.py", line 17, in <module>
ModuleNotFoundError: No module named 'colour'
I checked and colour is a library inside/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.1.5) so I tried to add this library to all python versions because I thought that maybe one of them miss this package and adding it to all would fix :
ls /Library/Frameworks/Python.framework/Versions
output :
2.7 3.10 3.5 3.9 Current
So I added colour to each version running (not running on Current bcz it's not a version ?) :
python_version -m pip install colour
But I still have the same error. It's like if Python can't find the right module path when launching the executable but if I run it from the interpreter with python3 or from VSCode all works fine.
Consider I didn't set up an environment, I just created files, classes and solved imports. But I created this __init__.py file :
import os
import sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
Because it solved a previous import problem.
I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.
Though the conversion from *.py- to a standalone *.exe-file via the command "pyinstaller --onefile filename.py" can be completed successfully, the executable itself failes to run
Traceback (most recent call last): File
"venv\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py",
line 13, in File
"c:\users\user01\pycharmprojects\api4trd\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 623, in exec_module
exec(bytecode, module.dict) File "lib\site-packages\pkg_resources__init__.py", line 86, in
ModuleNotFoundError: No module named 'pkg_resources.py2_warn' [13560]
Failed to execute script pyi_rth_pkgres
following packages are used
1) math
2) datetime
3) yfinance
Python version: 3.8.1
pyinstaller version: 3.6
OS:W10
Thank you!
You are missing one hidden import in your pyinstaller command. Although earlier version of Python and Pyinstaller never used to give this error, but nevertheless here is the solution
pyinstaller --onefile --hidden-import=pkg_resources.py2_warn filename.py
I want to bundle some python code into a distributable application (.app file) but having trouble with getting opencv and PyQt5 into an application. Either, on the other hand, works fine.
I've stripped all my code and the problem persists. See the following code samples:
# Works absolutely fine
import cv2
print("Hi there opencv v{0} user!".format(cv2.__version__))
# Works absolutely fine
from PyQt5.QtCore import *
print("Qt version {0}".format(QT_VERSION_STR))
# Does not work, see below
import cv2
from PyQt5.QtCore import *
print("Qt version {0}".format(QT_VERSION_STR))
print("opencv version {0}".format(cv2.__version__))
And by "not work", I mean that it bundles without errors (python3.5 -m pyinstaller samplecode.py), but cannot be executed. Instead, the program quits and returns an error:
Traceback (most recent call last):
File "/Users/*****/build/test.py", line 1, in <module>
import cv2
File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2/__init__.py", line 9, in <module>
from .cv2 import *
File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
ImportError: dlopen(/Users/*****/build/test/dist/test/cv2.cv2.so, 2): Symbol not found: __ZN10QByteArray11shared_nullE
Referenced from: /Users/*****/build/test/dist/test/QtTest
Expected in: /Users/*****/build/test/dist/test/QtCore
in /Users/*****/build/test/dist/test/QtTest
[20049] Failed to execute script test
The stars just replace my personal and project folder names.
I tried manually copying the cv2.so file, renaming it to cv2.cv2.so and placing it in the folder. This produces the a slightly different error:
Traceback (most recent call last):
File "/Users/*****/build/test.py", line 1, in <module>
import cv2
File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2/__init__.py", line 9, in <module>
from .cv2 import *
File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
ImportError: dlopen(/Users/*****/build/test/dist/test/cv2.cv2.so, 2): Library not loaded: #loader_path/.dylibs/QtGui
Referenced from: /Users/*****/build/test/dist/test/cv2.cv2.so
Reason: image not found
I've also tried cx_freeze and py2app but couldn't get these to work either. I got the furthest with pyinstaller and its development seems the most active so I prefer using this.
I'm running MacOS 10.12, and have reproduced the error on another clean-install MacBook running the same OS. I'm using Python 3.5.4, PyQt 5.9.1, OpenCV 3.3.0. For PyInstaller I've tried 3.2.1 and the most recent dev.
Can anyone help me understand and solve this error?
Using opencv-python-headless instead solved the problem for me, as mentionned in https://github.com/pyinstaller/pyinstaller/issues/3426.
Uninstall you opencv library, and then
pip install opencv-python-headless
https://pypi.org/project/opencv-python-headless/
I encountered these same errors (using MacOS 10.11.6) when trying to execute my app, which I also built using pyinstaller. I was able to successfully resolve my issue by deprecating opencv and pyinstaller to the following versions:
pip install opencv-python == 3.1.0.4
pip install pyinstaller == 3.3.1
This is not a satisfying solution, but it may be worth trying if you are still struggling with this issue.
I encountered a lot of problems like this, but after some investigating, I solved my problems by creating the virtual environment and install all the libraries I used in the project, then install the OpenCV and pyinstaller on that virtual environment like this:
activate myEnvironment
> pip install opencv-python
> pip install pyinstaller
and then used the pyinstaller in that virtual environment to create the exe file. it bundles all the dlls needed to software.
I want to use pyOpenSSL, which I have downloaded and tried to build, but I am having problems with setuptools.
First I have downloaded just python and used it with no success, but now I have tried with WinPython and got the same result, which is:
Traceback (most recent call last):
File "setup.py", line 11, in <module>
from setuptools import setup
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\__init_
_.py", line 11, in <module>
from setuptools.extension import Extension
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\extensi
on.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\dist.py
", line 15, in <module>
from setuptools.compat import numeric_types, basestring
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\compat.
py", line 19, in <module>
from SimpleHTTPServer import SimpleHTTPRequestHandler
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\SimpleHTTPServer.py", line 27, i
n <module>
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\SimpleHTTPServer.py", line 208,
in SimpleHTTPRequestHandler
mimetypes.init() # try to read system mime.types
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\mimetypes.py", line 358, in init
db.read_windows_registry()
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\mimetypes.py", line 260, in read
_windows_registry
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: must be string without null bytes or None, not str
I have installed Python 2.7.6 with PyCharm with no success, and also tried WinPython with the same result.
I am running windows 7 x64.
Since you are facing problem in installing setuptools itself, you can download the windows binary file for setuptools from this link.
This website is a great repository of pre-compiled windows binaries for various Python modules.
If installing 64-bit Python is not a necessity, I would suggest you to install 32-bit version of Python and other modules.
This is related to a bug in Python 2.7.6, to do with Windows Registry corruption.
Some programs (wrongly) write a terminating null to registry entries and this screws up setuptools. Updating Python fixes the issue.
See This blog post