PyInstaller and OpenEXR - exe crashes - python

I am using PyInstaller 3.3.1 and OpenEXR 1.3.0 in my project.
I create single file executable with --onefile option.
This works as intended:
import sys
if getattr(sys, 'frozen', False):
print("hello world")
"hello world" is simply output into console and program closes.
This on the other hand makes exe crash (no errors are displayed):
import sys
import OpenEXR
if getattr(sys, 'frozen', False):
print("hello world")
I tried debugging the program, to see if OpenEXR module is being properly included in exe. It is.
What can be the other reason of the crash?
Is there any way to make the exe tell me why it crashes?

I found the workaround to the problem. Instead of working with OpenEXR directly, I used pyexr - the wrapper library over OpenEXR. Exe doesn't crash anymore.

Related

Filename reading problem when converting to exe with cx_Freeze module

I created a file called dork.py in Python and wrote the following inside the file:
import os,time
print(os.path.basename(__file__))
time.sleep(3)
as a result it prints dork.py on the screen
In the setup.py I created for cx_Freeze, I wrote the following:
from cx_Freeze import setup, Executable
build_exe_options = {
'packages': ['os','time'],
'include_files':['/']
}
setup(
name='YourAppName',
version='1.0',
description='Your App Description',
options={'build_exe': build_exe_options},
executables=[Executable('dork.py')]
)
When I run the exe file I want it to print dork.exe but it says dork.py. I tried this with pyinstaller and pyinstaller was giving an error
How can i solve this
I expected it to print dork.exe but it says dork.py
the __file__ will point to the original python file after freezing, this is the way cx_freeze or pyinstaller "patches" these variables when it freezes the modules.
a simple way to make it return .exe when the app is frozen is outlined in the documentation
import sys
if getattr(sys, "frozen", False):
# The application is frozen
script_path = sys.executable
else:
# The application is not frozen
script_path = __file__
print(script_path)

Cython embed on Windows

I have read How to enable `--embed` with cythonize? and Cython --embed flag in setup.py but this method does not work on Windows. Let's say we have:
# app.py
print("hello")
# build.py
import setuptools # allows us to avoid calling vcvarsall.bat, see https://stackoverflow.com/a/53172602/
from distutils.core import setup
from Cython.Build import cythonize
from Cython.Compiler import Options
Options.embed = "main"
setup(ext_modules=cythonize(r'app.py', build_dir="release"), script_args=['build'], options={'build': {'build_lib': 'release'}})
Running this build.py script on Python 3.8 for Windows does not produce an app.exe file like it would with the command line command:
cython app.py --embed
Instead, it produces a .pyd file.
How to use cythonize + embed from a Python script, producing a .exe, on Windows?
Solved: in fact the problem did not come from cythonize itself, but from the fact distutils.core.setup(...) is configured to compile+link into a .pyd instead of a .exe.
Here is the solution:
from distutils._msvccompiler import MSVCCompiler # "from distutils.msvccompiler" did not work for me!
from Cython.Compiler import Options
Options.embed = "main"
cythonize(r'src\app.py', build_dir="build")
compiler = MSVCCompiler()
compiler.compile([r"build\src\app.c"], include_dirs=["C:/Python38/include"])
compiler.link_executable([r"build\src\app.obj"], "app", libraries=["python38"], library_dirs=["C:/Python38/libs"], output_dir="release", extra_preargs=["/NOIMPLIB", "/NOEXP"])
The .exe will be in the release folder.
(Note: I also upgraded Cython to the latest version 0.29.30, it might have helped as well)

Failing to create a .exe file with Python and PyQt5

I`m trying to make an executable from my python script called ProyectoNew.py. It works with a folder called "Imagenes" and another called "ModulosExternos", and a PyQT5's .ui file like this:
Here`s the Code posted in GitHub: https://github.com/TheFlosh/ProyectoSoftware.git
I`ve tried to use Pyinstaller, Py2Exe and CXFreeze but it didn't worked. Using each one of these modules to create a .exe file I've got the same result when I tried to execute it in another PC, as shown here:
On the LEFT of the picture you can see what I get after using pyinstaller (or Py2Exe), on the right you can see what I need to show.
Here are the modules that I use in my code ("ModulosExternos" is the folder where I put some particular modules that my code needs):
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pyttsx3
from pyttsx3.drivers import sapi5
import PyQt5
import sip
import os
from ModulosExternos import Boton_1,Boton_2,Boton_3,Boton_4,Boton_Final,Listas_Pictogramas, Frases_Completas
And here is the last part of it, to instantiate the GUI:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
GUI = ProyectoNew()
GUI.show()
sys.exit(app.exec())
I`ve read a lot of posts on the internet that recommended creating Setup.Py file to initiate the process of creating an executable file of my project. These are some examples of what I did with two of them:
With CX_Freeze:
import sys
import os
from cx_Freeze import setup, Executable
files = ['icono.ico','/Imagenes']
target = Executable(
script = "/ProyectoNew.py",
base = 'Win32GUI',
)
#Setup
setup(
name = "Proyect2",
version = "1.6",
description = "Software",
author = "----",
options = {'build_exe': {'include_files' : files}},
executables = [target]
)
With Py2Exe:
from cx_Freeze import setup, Executable
setup(name = "Proyect2",
version="1.0",
description = "Software",
executables = [Executable("ProyectoNew.py")],)
With Pyinstaller I typed: python --nowindowed --onefile ProyectoNew.py but I constantly got the same result as shown before.
I think that when I execute Pyinstaller, the .exe file doesn`t load the modules and the images that I use. What am I missing while creating the file? What do I need to do to execute the .exe file in another PC?.
I would prefer to use pyinstaller,but using any one of these will help me.
What's is wrong with that? You get some kind of error? Please edit your question with that.
With pyinstaller and this command, you should be able easily build an executable file:
pyinstaller [FILE].py -w -F
This command generate a /dist folder with the neccesary files and .exe file to run
*-w param is for do not provide a console window for standard i/o
*-F param is for one-file bundled executable
You can check more params here
PD: Before, you need obviously install pyinstaller:
pip install pyinstaller

How to compile Python code with PyTesseract to EXE?

I have working Python project which use pytesseract library.
I tested it in PyCharm. Python ver. 3.7.
Now I'm trying to compile this project to exe using PyInstaller.
When I run exe I got error:
File "getTextFromScreen.py", line 5, in ModuleNotFoundError:
No module named 'pytesseract' [9188] Failed to execute script main
My import in code looks like:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'Tesseract-OCR\tesseract'
I provide whole 'Tesseract-OCR' folder in python project folder and compiled project folder.
I don't know what do I do wrong.
I want to ask you for help
Are you using windows? You must include the .exe extension in your path. Instead just r'Tesseract-OCR\tesseract', use r'Tesseract-OCR\tesseract.exe'. I have a project using PyTesseract too, provide a whole tesseract folder in python project and working well after compiled using PyInstaller.
If you want to create an .exe and run it from any other pc where the Tesseract is not, you must use the auto-py-to-exe tool, in the additional files option attach the folder where all the Tesseract files were installed, then put this in your code
import sys
if getattr(sys, 'frozen', False):
_path = os.path.join(sys._MEIPASS, './tresseract/tesseract.exe')
print(_path)
pytesseract.pytesseract.tesseract_cmd =_path
# the .exe will look here
else:
pytesseract.pytesseract.tesseract_cmd = r"C:\tresseract\\tesseract.exe"
#ruta donde se encuentre su tresseract
and compile, good luck !!

Freeze a program created with Python's `click` pacage

I've got a command line program that uses Python's click package. I can install and run it locally, no problem with:
pip install --editable . # (or leave out the editable of course)
Now, I'd like to create an executable file that can be distributed and run standalone. Typically, since I'm in a Windows environment, I would use one of py2exe, pyinstaller or cx_Freeze. However, none of these packages work.
More specifically, they all generate an executable, but the executable does nothing. I suspect this problem is because my main.py script doesn't have a main function. Any suggestions would be very helpful, thanks in advance!
Can reproduce the issues with code copied from here.
hello.py
import click
#click.command()
def cli():
click.echo("I AM WORKING")
setup.py
from distutils.core import setup
import py2exe
setup(
name="hello",
version="0.1",
py_modules=['hello'],
install_requires=[
'Click'
],
entry_points="""
[console_scripts]
hello=hello:cli
""",
console=['hello.py']
)
If someone could supply a working setup.py file to create an executable and any other required files, that would be much appreciated.
From the console:
python setup.py py2exe
# A bunch of info, no errors
cd dist
hello.exe
# no output, should output "I AM WORKING"
I much prefer pyinstaller to the other alternatives, so I will cast an answer in terms of pyinstaller.
Starting click app when frozen
You can detect when your program has been frozen with pyinstaller, and then start the click app like:
if getattr(sys, 'frozen', False):
cli(sys.argv[1:])
Building an exe with pyinstaller
This simple test app can be built simply with:
pyinstaller --onefile hello.py
Test Code:
import sys
import click
#click.command()
#click.argument('arg')
def cli(arg):
click.echo("I AM WORKING (%s)" % arg)
if getattr(sys, 'frozen', False):
cli(sys.argv[1:])
Testing:
>dist\test.exe an_arg
I AM WORKING (an_arg)

Categories

Resources