Importing matplotlib.pyplot and BeautifulSoup with cxFreeze - python

I am attempting to compile an executable for my python script using cxFreeze. Out of the many libraries which I need to import for my script, two seem to fail with cxFreeze. In particular, consider the following test.py script:
print('matplotlib.pyplot')
import matplotlib.pyplot
compiling this with cxFreeze and running gives the following output:
separately, the following test.py script:
print('BeautifulSoup from bs4')
from bs4 import BeautifulSoup
after being compiled with cxFreeze, produces the following output:
My setup.py file for cxFreeze looks as follows:
import sys
from cx_Freeze import setup, Executable
setup(
name = "myname",
version = "1.0",
description = "some description",
executables = [Executable("test.py", base = None)]
)
I am running Python 3.3 x86, and am using a 32 bit version of cxFreeze (most recent) on Windows 7.
I am having trouble chasing down this issue. For one, the directory "C:\Python\32-bit..." doesn't exist on my computer, so I am unclear as to why cxFreeze is trying to look there. Does anyone have any idea how to approach this, or perhaps has already dealt with this issue?

After some digging around, I was able to resolve the issue. For those who may be encountering the same issue, this is what solved it for me:
For the issue with matplotlib: I simply needed to explicitly specify to cxFreeze to include matplotlib.backends.backend_tkagg. My setup file ended up looking like this:
import sys
from cx_Freeze import setup, Executable
packages = ['matplotlib.backends.backend_tkagg']
setup(
name = "myname",
version = "1.0",
description = "some description",
options = {'build_exe': {'packages':packages}},
executables = [Executable("test.py", base = None)]
)
As for the BeautifulSoup issue: There are a couple of posts around the web that have dealt with this issue: cx_freeze sre_constants.error nothing to repeat, https://bitbucket.org/anthony_tuininga/cx_freeze/issue/59/sre_constantserror-nothing-to-repeat.
The relevant conclusion: something is wrong with the 4.3.2 build of cxFreeze that causes this issue. I simply used cxFreeze 4.3.1 to build my app and the problem was solved. It may also be possible to rebuild 4.3.2 locally and have the issue be resolved, but I did not attempt this solution.

Related

Py2App error: ModuleNotFoundError: No module named 'cmath' when using Pandas

I am trying to build a standalone app that utilises Pandas. This is my setup.py file:
from setuptools import setup
APP = ['MyApp.py']
DATA_FILES = ['full path to/chromedriver']
PKGS = ['pandas','matplotlib','selenium','xlrd']
OPTIONS = {'packages': PKGS, 'iconfile': 'MyApp_icon.icns'}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app','pandas','matplotlib','selenium','xlrd'],
)
The making of the *.app file goes smoothly, but when I try to run it, it gives me the following error:
...
import pandas._libs.testing as _testing
File "pandas/_libs/testing.pyx", line 1, in init pandas._libs.testing
ModuleNotFoundError: No module named 'cmath'
I tried to include ‘cmath’ in my list of PKGS and in setup_requires in the setup.py file, but when I tried to build the app using py2app it gave me the error:
distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('cmath')
I am stuck. I couldn't find anything useful online. cmath should be automatically included from what I have been reading. Any ideas on where is the problem and how can I fix it?
I think I have found a solution: downgrade to Python version 3.6.6.
See: python 3.6 module 'cmath' is not found
To uninstall Python I followed this process: https://www.macupdate.com/app/mac/5880/python/uninstall
Then I installed Python 3.6.6: https://www.python.org/downloads/release/python-366/
With Python 3.6.6, Py2App seem to work no problem and includes Pandas smoothly.
It seems that for some reasons cmath is not included in the latest versions of Python? I might be wrong. Please let me know what you think and if you have any questions.
P.S.: I am using MacOS (Mojave 10.14.6) and PyCharm.
I had a similar issue with py2app and cmath. I solved this by adding import cmath into the main script. (MyApp.py in your case) I think doing so may have the modulegraph to add the cmath library files.

cx_Freeze not finding some TensorFlow imports

I recently wrote a library (in Python 3.6) and built a GUI for it using tkinter on Windows 10. The GUI is now finished, and I'm trying to freeze it using cx_Freeze.
The setup script runs perfectly fine (or at least I couldn't spot any error message or warning) and I can get my executable out of it. The problem is, when I run it, I get the following error message:
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\profiler\profiler.py", line 22 in <module>
from tensorflow.core.profiler.tfprof_log_pb2 import OpLogProto
ModuleNotFoundError: No module named 'tensorflow.core.profiler.tfprof_log_pb2'
The reason why TensorFlow is mentioned here is that my library uses TensorFlow, and of course, so does my GUI. What the entire error message says is that when I'm importing tensorflow (import tensorflow as tf), the program tries to do from tensorflow.python import * and the profiler.py in tensorflow.python.profiler then tried to do the import that causes the error.
I found the file that causes the error, and when I do on IDLE from tensorflow.core.profiler.tfprof_log_pb2 import OpLogProto, it works perfectly fine.
Before reaching to that point, I encountered several similar problems (the cx_Freeze building without displaying any warning or error, but the .exe having some import errors), but I could so far fix them all by myself, mostly by adding them to the list of include_files in the setup script. I tried to do the same for this TensorFlow file, but it didn't work. I also tried including TensorFlow as a package in the setup script, or directly importing it all in my main.py, without success.
My setup.pyis the following (there might be some unnecessary includes with it, since I tried lots of things):
from cx_Freeze import setup, Executable
import os
import sys
os.environ['TCL_LIBRARY'] = "C:\\Program Files\\Python36\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files\\Python36\\tcl\\tk8.6"
base = None
if sys.platform == "win32":
base = "Win32GUI"
includes = ["tkinter", "_tkinter", "numpy.core._methods", "numpy.lib.format", "tensorflow"]
include_files = ["C:\\Program Files\\Python36\\DLLs\\tcl86t.dll",
"C:\\Program Files\\Python36\\DLLs\\tk86t.dll",
"C:\\Program Files\\Python36\\DLLs\\_tkinter.pyd",
"C:\\Program Files\\Python36\\Lib\\site-packages\\tensorflow\\core\\profiler\\tfprof_log_pb2.py",
"C:\\Program Files\\Python36\\Lib\\site-packages\\tensorflow\\python\\profiler\\profiler.py",
"C:\\Program Files\\Python36\\Lib\\site-packages\\tensorflow\\include\\tensorflow\\core\\profiler\\tfprof_log.pb.h"]
packages = []
setup(name = "Ap'Pear",
version = "0.1",
description = "Test executable",
options = {"build_exe": { "includes": includes, "include_files": include_files, "packages": packages}},
executables = [Executable(script = "main.py", targetName = "Ap'Pear.exe", base = base, icon = "images/icon.ico")],
)
I tried rebuilding TensorFlow and its dependencies from scratch, but it didn't solve anything either.
Thanks in advance!
I was able to resolve this problem by creating a blank __init__.py file in \path\to\python\Lib\site-packages\tensorflow\core\profiler. I am running python 3.5.2 and TensorFlow 1.5.0 so this solution may be specific to my installations.

.exe created with cxfreeze-quickstart does nothing - cx_freeze 5.0

I am using python 3.5.1 and the unofficial cx_freeze 5.0 build available from here. I am trying to create an executable version of a python project using tkinter and sympy that I've been working on. I used cxfreeze-quickstart to create a setup.py file for the program, and in terms of building what at least seems to be a valid executable, it works without throwing any errors. However, when I try to run the executable, nothing happens. I know similar questions have been asked on here and I've looked at and tried to understand every one I've found, but none of the solutions have worked for me. I don't understand what's going on, and any help would be appreciated. Code below:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('c:\\users\\joe\\pycharmprojects\\physics2-0\\physics2-0.py', base=base,
targetName = 'c:\\users\\joe\\pycharmprojects\\physics2-0\\physics.exe')
]
setup(name='physics solver',
version = '0.1',
description = 'alpha physics solver',
options = dict(build_exe = buildOptions),
executables = executables)
Thanks in advance.
UPDATE: I am now attempting to write the setup.py script myself according to the template provided in the docs, although any help would still be greatly appreciated.
UPDATE 2: I wrote my own setup.py according to the template provided in the docs, and put it in the same folder as the script I want to freeze, something I hadn't realized I needed to do. I ran python setup.py build in command line, and it created the build subdirectory with the exe and DLLs. However, now when I try and run the exe, an error message pops up that says ImportError: DLL load failed. The specified module could not be found. in reference to tkinter. The code for the 2nd setup.py is below.
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["tkinter", "sympy", "_tkinter"], "excludes": []}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == 'win32':
base = "Win32GUI"
setup( name = "physics solver",
version = "0.1",
description = "a basic physics solver",
options = {"build_exe": build_exe_options},
executables = [Executable("Physics2-0.py", base=base)])
Below are the first 4 lines of physics2-0.py. The line brought into question by the error message is line 1.
from tkinter import *
from tkinter import ttk
from sympy import Symbol
from sympy.solvers import solve
UPDATE 3: Someone please help me out here. I can't figure this out. I've even done a clean re-install of python at this point, just to be sure I didn't accidentally mess something up at some point, and it's still giving me the same error message.

No module named builtins

I'm trying to convert my .py script into an executable using py2exe. I've had a number of issues so far that have been largely addressed by the "options" in the setup file below. But now I have a problem that I have not been able to find a solution for, and wondering if others have had this same issue and fixed it.
When I execute the setup file below using "python setup.py py2exe" it gives me an executable but when I run it, it complains "No module named builtins".
The only other post I could find on this subject indicated that builtins is a python3 thing, but I'm running 2.7.
Appreciate any advice or tips on this.
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
setup(
console=['DET14.py'],
options={
'py2exe': {
'packages' : ['matplotlib', 'pytz'],
'dll_excludes':['MSVCP90.DLL',
'libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll'],
'includes':['scipy.sparse.csgraph._validation',
'scipy.special._ufuncs_cxx']
}
},
# data_files=matplotlibdata_files
data_files=matplotlib.get_py2exe_datafiles()
)
Here is the full listing of what the error message looks like:
I also found using 'pip install future'
resolved this issue
I got the information from here: https://askubuntu.com/questions/697226/importerror-no-module-named-builtins
I hope this clarifies this for other users, like me who stumbled upon your question
Running pip install future fixed this error for me.
For compatibility with Python2.7, the package future should be added to the install_requires in setup.py.
Note that nosetests also fails without matplotlib, but I'm not sure adding matplotlib as a dependency makes much sense.
Source
I finally got this working. It turned out that I had some errors in the original setup file, some of which were outright dumb, and some simply reflected my lack of understanding of how the parameters of the setup command works. I will add that this latter class of errors was only resolved with some Sherlock Holmes-style sleuthing and plain old trial and error. By that I mean that I have still not found any documentation that calls out the meaning and usage of the parameters of the setup command. If anyone has that info and could pass it along that would be much appreciated.
With that as background, here is the answer:
There were 2 basic problems:
The list of packages in the above setup file was woefully incomplete. I am still not certain that the rule is that you have to list every single package that your program relies upon, and some which it may rely upon that you didn't know about (e.g. pytz). But when I did that, I had something at that point that I could eventually get to work.
The error message in the above original question sort of looks like my program had a dependency on a thing called "patsy". This confused me because I had no idea what that was. It turns out that statsmodels (which is core to my project) has a dependency on patsy, so it needed to be included in the "packages" list.
Below is the setup file that ended up working. I hope this description of the logic behind the fix turns out to be helpful to others facing the same kind of problem.
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
setup(
console=['DET14.py'],
options={
'py2exe': {
'packages' : ['matplotlib', 'pytz','easygui',\
'statsmodels','pandas','patsy'],
'dll_excludes':['MSVCP90.DLL',
'libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll'],
'includes':['scipy.sparse.csgraph._validation',
'scipy.special._ufuncs_cxx']
}
},
data_files=matplotlib.get_py2exe_datafiles()
)
In case pip install future does not work for you, it's possible that you have a bad copy of the future module hiding somewhere. For me, PyCharm had installed future==0.18 while I wanted future=0.16. sudo pip uninstall future did not work, you could still import future and it would be 0.18. Solution was to find and delete it.
>>> import future
>>> future.__version__
'0.18.0'
>>> future.__file__
'/home/<USERNAME>/.local/lib/python2.7/site-packages/future/__init__.pyc'
rm -rf /home/<USERNAME>/.local/lib/python2.7/site-packages/future

cx_Freeze help. ImportError

I'm a newbie to cx_Freeze and I need some help. I'm writing an application with python 3.3, pyqt4 and some more libraries (scipy, numpy, matplotlib, dxfwrite). Now I'm trying to freeze the application with cx_Freeze under windows7. I'm using cx_Freeze-4.3.2.win-amd64-py3.3 and the following setup.py for cx_Freeze:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"] }
setup(
name = "Barrel Cam Editor",
version = "0.2.0",
description = "An editor for Barrel Cams",
options = {"build_exe": build_exe_options},
executables = [Executable("barrelcameditor.py", base = "Win32GUI")])
I run the command python setup.py build and it seems to work but when I launch the obtained exe I get an Importerror: DLL load failed.
I really do not know how to solve this problem. any help?
Tnx
PS:
Thank you for your help, it was a problem with scipy.linalg. I switched to numpy.linalg and it seems to work. Now I've another little problem: I was importing a custom package:
from barrelcam import camdata, camdlg, camwidget
but in this way it is not working.
I found a workaround: I moved the files to the barrelcameditor folder and it seems to work, changing the import to
import camdata, camdlg, camwidget
There is a way to keep the original position of files?
Thank you

Categories

Resources