cx_freeze does not find python 3 path mac - macos

I have this problem when I try to generate an app executable. I follow this example so the code for the setup would be
application_title = "app_name" #what you want to application to be called
main_python_file = "main.py" #the name of the python file you use to run the program
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
includes = ["atexit","re"]
setup(
name = application_title,
version = "0.1",
description = "Sample cx_Freeze PyQt4 script",
options = {"build_exe" : {"includes" : includes }},
executables = [Executable(main_python_file, base = base)])
then I go to my terminal and press
python setup.py bdist_mac
for some reason that I do not know how to solve I get this output, and the file it is not generated
running bdist_mac
running build
running build_exe
copying /Applications/anaconda/lib/python3.4/site-packages/cx_Freeze/bases/Console -> build/exe.macosx-10.5-x86_64-3.4/main
copying /opt/local/Library/Frameworks/Python.framework/Versions/3.4/Python -> build/exe.macosx-10.5-x86_64-3.4/Python
error: [Errno 2] No such file or directory: '/opt/local/Library/Frameworks/Python.framework/Versions/3.4/Python'
I have installed anaconda with Python 3.4 and cx_freeze. Any suggestions?

I met the same problem and it is fixed by installing a separated python besides anaconda. It seems to be similar with the problem here
py2app is not copying the Python.framework to the new app while using virutalenv

Related

cx_Freeze throws an error when building if packages are involved

cx_Freeze successfully converts a .py file to an .exe if no other packages are involved.
But when pandas is included, the setup fails with this message:
File "C:\Anaconda\lib\site-packages\cx_Freeze\hooks.py", line 1324, in load_zmq
libzmq = __import__("zmq", fromlist=["libzmq"]).libzmq
AttributeError: module 'zmq' has no attribute 'libzmq'
This is the contents of the setup.py:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["pandas"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "test",
version = "0.1",
options = {"build_exe": build_exe_options},
executables = [Executable("test.py", base=base)])
So far I've tried creating a different environment in anaconda to run cx_freeze to see if the problem resolves itself that way.
Appreciate any help.
I've spent a long time trying to find an answer to this question, but luckily it can be found here. It seems it was a bug inherent to cx_freeze.
Although the link provided to download the wheels no longer works, the patch has been added to the latest development version (6.6.0 if I recall correctly). You can install it directly from github, as such:
python -m pip install git+https://github.com/marcelotduarte/cx_Freeze.git
Doing this fixed the error for me (and enabled me to discover other errors that I had to deal with!).

PyCharm: Using SIFT with opencv-contrib (Mac)

I have a Python/OpenCV project and I am trying to use the xfeatures2d module from opencv-contrib. I am using a Mac and my IDE is PyCharm. I have installed the packages opencv-contrib-python and opencv-python through Preferences > Project Intepreter.
However, when I try to run the code below, I get a the following error:
import cv2
import numpy as np
img = cv2.imread("NotreDame.jpg", 0)
sift = cv2.xfeatures2d.SIFT_create()
line 6, in <module>
sift = cv2.xfeatures2d.SIFT_create()
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'
I have installed opencv and opencv-contrib on my computer using:
$ pip install opencv-python==3.4.2.17
$ pip install opencv-contrib-python==3.4.2.17
I am not sure how I can resolve this error. Any insights are appreciated.

Cython: prange is repeating not parallelizing

I have the following simple Cython function for a parallel reduction:
# cython: boundscheck = False
# cython: initializedcheck = False
# cython: wraparound = False
# cython: cdivision = True
# cython: language_level = 3
from cython.parallel import parallel, prange
cpdef double simple_reduction(int n, int num_threads):
cdef int i
cdef int sum = 0
for i in prange(n, nogil=True, num_threads=num_threads):
sum += 1
return sum
Which horrifyingly returns the following:
In [3]: simple_reduction(n=10, num_threads=1)
Out[3]: 10.0
In [4]: simple_reduction(n=10, num_threads=2)
Out[4]: 20.0
In [5]: simple_reduction(n=10, num_threads=3)
Out[5]: 30.0
In other words, it appears to be repeating all n iterates of the loop for each thread instead of parallelizing the iterates over each thread. Any idea what's going?
I am using Python 3.7.1 and Cython 0.29.2 on macOS Mojave 10.14.3.
UPDATE: Here's my setup.py file:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
import os
import sys
if sys.platform == 'darwin':
os.environ['CC'] = 'gcc-8'
os.environ['CXX'] = 'g++-8'
EXT_MODULES = [Extension('foo', ['foo.pyx'],
extra_compile_args=['-fopenmp'],
extra_link_args=['-fopenmp'])]
setup(name='foo',
ext_modules=cythonize(EXT_MODULES))
I have installed GCC separately and have to set the environment variables 'CC' and 'CXX' when using OSX to avoid the problem of OSX aliasing those clang.
I fixed this bug by first installing gcc using Anaconda:
conda install gcc
Then changing the lines in setup.py to use that new compiler:
if sys.platform == 'darwin':
os.environ['CC'] = '/anaconda3/bin/gcc'
os.environ['CXX'] = '/anaconda3/bin/g++'
Using Anaconda gcc (instead of the brew-installed one I was using originally) didn't fix the problem right away. It wouldn't compile due to the following bug:
/anaconda3/envs/python36/lib/gcc/x86_64-apple-darwin11.4.2/4.8.5/include-fixed/limits.h:168:61:
fatal error: limits.h: No such file or directory #include_next
/* recurse down to the real one */
The problem here has to due with macOS 10.14 and XCode 10.0. However the solution given by #Maxxx in this related question worked for me. After installing the .pkg hidden in the command line tool directory
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
the code compiled and the parallelism worked as it was supposed to.
UPDATE: After updating to OSX Catalina, this fix no longer works because the .pkg file above no longer exists. I found a new solution from reading this related question. In my case, exporting the following path to CPATH fixed the problem.
export CPATH=~/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

exe built with cx_Freeze, PyQt5, Python3 can't import ExtensionLoader_PyQt5_QtWidgets.py and run

I'm trying to pack my app Python3, PyQt5 for Windows using cx_Freeze.
I've installed Python 3.4.3, Python-win32, PyQT5, cxfreeze.
Application itself, run in console, works fine.
I try to pack it with cx_freeze:
python setup.py build_exe.
It works on the same host. But when I move it to another clean installation WinXP it gives an error:
Traceback:
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27 in <module>
File "pyftp1.py" in 7, in <module>
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 2237, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 2226, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", in 1161, in _load_backward_compatible
File "ExtensionLoader_PyQt5_QtWidgets.py", line 22, in <module>
File "ExtensionLoader_PyQt5_QtWidgets.py", line 14, in __bootstrap__
ImportError: DLL load failed: Не найден указанный модуль.
English: "specified module was not found".
Here's my setup.py:
import sys
from cx_Freeze import setup, Executable
includes = ['sys', 'PyQt5', 'PyQt5.Core', 'PyQt5.QtGui', 'PyQt5.QtWidgets', 'os', 'os.path', 'ftplib',
'traceback', 'time',]
excludes = []
packages = ['os', 'PyQt5']
path = []
build_exe_options = {
'includes': includes,
'excludes': excludes,
'packages': packages,
'path' : path,
#'dll_includes': ['msvcr100.dll'],
'include_msvcr' : True,
'include_files': [
(r'C:\Windows\System32\msvcr100.dll', 'msvcr100.dll'),
],
}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
setup(
name = "pyftp1",
version = "0.1",
description = "Foto uploader",
options = {'build_exe_options': build_exe_options},
executables = [Executable("pyftp1.py", base=base, appendScriptToLibrary=False, copyDependentFiles=True)]
)
Also I found it doesn't copy msvcr100.dll file, however I've stated it twice! I've copied it manually to target host.
Here's pyftp1.py:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, time, os, hashlib
import ftplib
import traceback
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog # <-- Line 7
from PyQt5.QtWidgets import QPushButton, QHBoxLayout, QVBoxLayout, QScrollArea, QLineEdit, QCheckBox
from PyQt5 import QtGui
from PyQt5.QtGui import QIcon, QPalette, QLinearGradient, QColor, QBrush
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
Solution.
After struggling during a lot of time with a problem I've found the solution. Many how-to do not take an account specific versions of software, or some specific modules used, or even they do not check .exe on fresh system, which has no your libraries dlls and so on. Here's working and tested solution.
First, I removed cx_freeze and used py2exe.
I've built all the stuff on WinXP 32-bit - for the goal it could be run on nearly every system.
You need:
Win XP 32-bit (or another system which you consider as minimal requirements)
Python 3.4 -- install from www.python.ord/download Windows x86 msi installer
PyWin32 -- install from sf.net pywin32-219.win32.py3.4.exe
Qt5.5 -- install it from qt.io, also you get mingw, next add folder C:\Qt\Tools\mingw492_32\bin to your system env %PATH%.
Git for Windows -- install from git-scm.com
PyQt5 -- download from riverbankcomputing.co.uk win32 x86 installer and run it
SIP -- the worst intallation!
Download it from riverbankcomputing.co.uk,
unzip,
run Git Bash,
python configure.py -p win32-g++ ,
mingw32-make.exe install.
It fails (. You go manually to sipgen/ folder and copy sip.exe to c:\Python34. Next go to ../siplib and copy sip.pyd to c:\python34\Lib\site-packages, then
strip /c/Python34/Lib/site-packages/sip.pyd
cp sip.h /c/Python34/include/
py2exe -- python -m pip install py2exe
7-Zip -- install from 7-zip.org as x86 exe
7-zip extra, unpack into installed 7-zip folder, conflicts with .txt files could be ignored
Resource Hacker -- from angusj.com -- however it's not necessary if you don't want to set to patch your .exe finally and give it a nice icon
Next I added setup.py to the project:
from distutils.core import setup
import os, sys
import py2exe
from glob import glob
import PyQt5
NAME="ProgName"
qt_platform_plugins = [("platforms", glob(PyQt5.__path__[0] + r'\plugins\platforms\*.*'))]
data_files.extend(qt_platform_plugins)
msvc_dlls = [('.', glob(r'C:\Windows\System32\msvc?100.dll'))]
data_files.extend(msvc_dlls)
# print(data_files)
sys.argv.append('py2exe')
setup(
data_files=data_files,
# windows=["pyftp1.py",],
windows=[
{
"script": "pyftp1.py",
"icon_resources": [(0, "resources/favicon.ico")]
}
],
# zipfile=None,
options={
"py2exe": {
"includes":["sip", "atexit",],
# "packages": ['PyQt5'],
"compressed": True,
"dist_dir": "dist/" + NAME,
# "bundle_files": 0,
# "zipfile": None,
"optimize": 2,
}
}
)
Just not to run all the actions manually, you may add Makefile
# start settings
DIST=dist
# change NAME also in setup.py and resource/config.txt
NAME=ProgName
EXT=exe
# final name and location of the built program
FINAL=$(DIST)/$(NAME).$(EXT)
# external programs
7ZIPDIR="C:\Program Files\7-Zip"
RESHACKER="/c/Program\ Files/Resource\ Hacker/ResourceHacker.exe"
# intermediate steps
# no icon version of program
NOICON=$(DIST)/$(NAME)_no_icon.$(EXT)
# name of .7z archive
7Z_BASENAME=$(NAME).7z
7Z=$(DIST)/$(7Z_BASENAME)
# folder with ready .exe
PROGDIR=$(DIST)/$(NAME)
all: $(FINAL)
$(FINAL): $(NOICON)
# change icon
"$(RESHACKER)" -addoverwrite $(NOICON), $(FINAL), resources/favicon.ico, ICONGROUP, MAINICON, 0
$(NOICON): $(7Z)
#build autorunning sfx with default icon
cat $(7ZIPDIR)/7zS.sfx resources/config.txt $(7Z) > $(NOICON)
$(7Z): exe
# compress program folder to .7z
cd $(DIST); $(7ZIPDIR)\\\7z.exe a $(7Z_BASENAME) $(NAME)
exe: $(DIST)
# build program itself
python setup.py py2exe
$(DIST):
# create dist directory
# echo $(DIST)/
clean:
rm -rf $(DIST)/*
Now you can easily build your program itself - as a folder of files:
mingw32-make.exe exe
Or make a single file with correct icon:
mingw32-make.exe all
Or clean that all stuff:
mingw32-make.exe clean
you could try this:
add 'libEGL.dll' to your working folder. This is from Python3x\Lib\site-packages\PyQt5
In cx-freeze setup.py add:
if sys.platform == "win32":
includefiles = ['libEGL.dll'] #here you should add other files if you need to
In cx-freeze setup.py link "includefiles" to build_exe_options like this:
build_exe_options= {
...
...
'include_files': includefiles,
}

making a python program executable

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
windows = [{'script': "single.py"}],
zipfile = None,
)
in this setup file for py2exe where it says single.py is that where I place the name of my program?
I don't know your py2exe tool, but we usually use this way to convert py to exe:
Download and install Standard Python Software:
http://www.python.org/download/
Download PyInstaller via link below:
http://pyinstaller.python-hosting.com/
Unpack the archive, that you have downloaded!
In this examople, the directory of the unpacked files:
In the <UNPACKED_FILES_DIR> directory, run Configure.py.
It must be run before trying to build anything.
Create a spec file for your project:
python Makespec.py -F -p <PYTHON_LIB_PATH> <PYTHON_SCRIPT>
-F: Produce a single file deployment.
-p <PYTHON_LIB_PATH>: Set base path for import (like using PYTHONPATH).
( e.g.: C:\Program Files\Python24\Lib\ )
<PYTHON_SCRIPT>: Path to python script.
6 Build your project!
python Build.py <SPECFILE>
<SPECFILE>: Path to the specfile, that have been created in step 4!
The full path to <SPECFILE>:
<UNPACKED_FILES_DIR>/<PYTHON_SCRIPT>/<PYTHON_SCRIPT>.spec
The binary file will be placed in the directory of <SPECFILE>.
If you can restrict your code, then Shed Skin, PyPy, or Cython make true, fast executables.
Py2exe, PyInstaller, or bbfreeze can package Python up to 2.7 into single executables.
Cx_Freeze packages Python up to 3.x into an executable plus many other files.
Yes. Are you making a windowing application or a console application? See the example setup.py files that came with py2exe.

Resources