Build a .exe file from .py with SQLite database - python

Program makes appointments and places them into schedule which are written in QsQLite database. The program runs from .py but I need it to be in .exe.
I have used cx_Freeze to create an .exe file, but the program does not generate the SQLite database.
So here is my setup file:
from cx_Freeze import setup, Executable
import os
import sys
os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35\tcl\tk8.6'
build_exe_options = {"packages": [
'os','sys','sqlite3'], 'include_files': [os.path.join(sys.base_prefix, 'DLLs', 'sqlite3.dll'), 'main.py','util.py','data.db']}
setup(
name = "Eclients",
version = "0.1",
options = {"build_exe": build_exe_options},
executables = [Executable("main.py")]
)
But database can't be opened
So,how it can be solved?

You are not including your SQLite database file in the include_files statement. See the documentation: http://cx-freeze.readthedocs.io/en/latest/faq.html#using-data-files
A better solution, however, would be to provide an option to create the missing database file when needed. This would allow the database's SCHEMA be defined within your script, and be kept consistent with your programme logic. If it needs populating with data, this might however be a less optimal solution.

Solved this problem by copying the whole 'sqldrivers' folder from C:\Program Files\Python35\Lib\site-packages\PyQt5\pluginsto main.exe directory.

Related

cx-Freeze Executable Won't Run on Another Computer

I wrote a program in Python and created an executable with cx-Freeze. I had to include tk and tcl libraries, as well as some images, in the setup.py for the executable to run correctly.
I linked these files to absolute paths on my computer, thinking that cx-freeze would copy these files over to the final executable folder so that it would become a part of its own package.
The program runs perfectly on my PC, but does not run on my colleague's PC.
Including the tcl and tk libraries as well as the images was a part of my troubleshooting process when the .exe wouldn't run. I have no idea of what to do next.
'''This is my setup.py file.'''
from cx_Freeze import setup, Executable
import sys
import os
includes = []
include_files =
[r"C:\Users\jchoujaa\AppData\Local\Programs\Python\Python37\DLLs\tcl86t.dll",
r"C:\Users\jchoujaa\AppData\Local\Programs\Python\Python37\DLLs\tk86t.dll",
r"C:\Users\jchoujaa\Desktop\Code\STARx App\Savvy Logger\Developer\Imaging\savron.png",
r"C:\Users\jchoujaa\Desktop\Code\STARx App\Savvy Logger\Developer\Imaging\s-icon.ico",
r"C:\Users\jchoujaa\Desktop\Code\STARx App\Savvy Logger\Developer\Imaging\STAR.png"]
os.environ['TCL_LIBRARY'] = r'C:\Users\jchoujaa\AppData\Local\Programs\Python\Python37\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\jchoujaa\AppData\Local\Programs\Python\Python37\tcl\tk8.6'
base = 'Win32GUI' if sys.platform == 'win32' else None
setup(name = "SavvyLogger",
version = "1.0",
description = "Logger Interpreter",
options={"build_exe": {"includes": includes, "include_files": include_files, 'packages': ['pandas', 'numpy']}},
executables = [Executable("SavvyLogger.py", base=base)])
This is the error my colleague receives when attempting to open my executable:
enter image description here
Henry Yik's suggestion in the comments worked.
The problem was that I had set the path of the images used by tkinter to a folder located on my desktop.
Henry suggested I place the image files in the same folder as my .py script and remove the path names from the image variables.
This worked!

create executable from python program with dependencies

I am trying to use cx_Freeze to make an executable for my program.
Even though the python program works perfectly, the executable says it cant file file_cleaner.
This is what the setup.py looks like
import sys
from cx_Freeze import setup, Executable
import os.path
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
build_exe_options = {"packages": ["os", "glob", "itertools", "numpy",
"matplotlib", "lmfit", "pandas", "scipy", "pathlib"], "includes": [
"src\\BCS_fit.py", "src\\file_cleaner.py", "src\\__init__.py",
"src\\dependencies\\"], "include_files": ["test\\"]}
base = None
setup(name="BCS processor",
version="0.1",
description="Console application for processing VTS data and fitting it
according to BCS theory",
author="Anil Radhakrishnan",
options={"build_exe": build_exe_options},
executables=[Executable("src\\Master.py", base=base)])
BCS_fit.py and file_cleaner.py are 2 other python files that I call from master.py.
The Dependencies folder has .py and .pyd file from a c module converted to python.
This is the first time I am trying to create an executable for a python script, please excuse any beginner errors.
Thanks a lot for your assistance!
In the past I've used py2exe, not sure if it works with Python 3.6 though,so depends what version you are currently using.
This SO has a quick guide to using cx_Freeze, maybe it covers something that you may have missed out?
Also Pynsist seems to be another very good way of creating an executable from a Python script. It can also create installation files for your program.

How to use cx_Freeze to make an executable python program

I downloaded cx_Freeze because I'm trying to make a .exe file to share with my platoon and I've been reading through the docs as well as scrolling through cx_Freeze tutorial. After following both of those I still don't know why this isn't working for me. I'm on Python 3.6.2 and I have the path directly setup to the command line.
I tried to launch with setup.py and Julian date 2.py on the desktop and I tried adding them to same folder, but no matter what I try I get back this error when I type python setup.py build, python: can't open file 'setup.py': [Error2] no such file or directory or file exsists. Below is my setup.py code.
from cx_Freeze import setup, Executable
setup(name = "Julian date 2" ,
version = "0.1" ,
description = "" ,
executables = [Executable("Julian date 2.py")])
Another issue I ran into was trying to type cxfreeze Julian date 2.py --target-dir dist I get the error 'cxfreeze' is not recognized as an internal or external command, operable program or batch file.
When you type python setup.py build, you are supposed to be in the directory with setup.py and not anywhere else. So use the command cd to get there.
cx_freeze is not in your path variable so cxfreeze Julian date 2.py --target-dir dist will not work and you have to instead add it to your path (somehow) [not recommended]
Hope this helped.
P.S.
executables = [Executable("Julian date 2.py")]) takes base too. If you want a console application:
executables = [Executable("Julian date 2.py",base='None')])
Gui for windows:
executables = [Executable("Julian date 2.py",base='Win32GUI')])
And you forgot your exe options in setup(). I recommend adapting the setup.py script on cx_freeze doxs:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = "None"
setup( name = "name",
version = "0.1",
description = " ",
options = {"build_exe": build_exe_options},
executables = [Executable("file.py", base=base)])
I solved the first issue, my file was named 'setup.py' and not just 'setup' as it's supposed to be...The name must be setup, the extension .py
Know it's DUMB, after hours, that was the problem...

cx_Freeze : DLL load error with tkinter

I am currently trying to use cx_Freeze to create a .exe file for my python scripts.
First, do cx_freeze get all data on the folder into the build folder?
Secondly, I'm having an issue when launching the .exe file. The fact that the first file opens the second one could be the issue ?
The console opened and closed, according to another post on Stackoverflow, I created a .bat file containing :
myfilename.exe%1
pause
to check out what the issue is and I got this issue :
Issue
I've really no idea what to do next since I tried many things on the setup.py to make things working.
Here is the setup.py :
"""setup.py"""
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\\Roukira\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\\Roukira\\AppData\\Local\\Programs\\Python\\Python36\\tcl\\tk8.6"
build_exe_options = {"includes": ["tkinter"]}
setup(name="todolist",
version="0.1",
description = "A simple to do list with differnt tabs per account.",
options = {"build_exe": build_exe_options},
executables = [Executable("login_system.py",base=None)])
I'm only using pillow as an external module but it doesn't seem to be the issue.
Thanks for your help by advance !
EDIT : I managed to fix it by adding the ddl missing files path inside the "include_files" option:
build_exe_options = {"packages": ["os", "tkinter"], "include_files": ["to_do_list.py","336sur525.gif","384sur540.gif",
"accounts.txt","button_hide_2.gif","button_quit_2.gif","choose.gif","icone.ico","user.gif",
r"C:\Users\Roukira\AppData\Local\Programs\Python\Python36\DLLs\tcl86t.dll",
r"C:\Users\Roukira\AppData\Local\Programs\Python\Python36\DLLs\tk86t.dll"]}
My script was using a subprocess.call function to call another script, so it didn't work after becoming .exe, I added the .py file inside the "include_files" and it worked as intended.

running a python script as administrator

i'm writing an installer using py2exe which needs to run in admin to have permission to perform various file operations. i've modified some sample code from the user_access_controls directory that comes with py2exe to create the setup file. creating/running the generated exe works fine when i run it on my own computer. however, when i try to run the exe on a computer that doesn't have python installed, i get an error saying that the import modules (shutil and os in this case) do not exist. it was my impression that py2exe automatically wraps all the file dependencies into the exe but i guess that this is not the case. py2exe does generate a zip file called library that contains all the python modules but apparently they are not used by the generated exe. basically my question is how do i get the imports to be included in the exe generated by py2exe. perhaps modification need to be made to my setup.py file - the code for this is as follows:
from distutils.core import setup
import py2exe
# The targets to build
# create a target that says nothing about UAC - On Python 2.6+, this
# should be identical to "asInvoker" below. However, for 2.5 and
# earlier it will force the app into compatibility mode (as no
# manifest will exist at all in the target.)
t1 = dict(script="findpath.py",
dest_base="findpath",
uac_info="requireAdministrator")
console = [t1]
# hack to make windows copies of them all too, but
# with '_w' on the tail of the executable.
windows = [{'script': "findpath.py",
'uac_info': "requireAdministrator",
},]
setup(
version = "0.5.0",
description = "py2exe user-access-control",
name = "py2exe samples",
# targets to build
windows = windows,
console = console,
)
Try to set options={'py2exe': {'bundle_files': 1}}, and zipfile = None in setup section. Python will make single .exe file without dependencies. Example:
from distutils.core import setup
import py2exe
setup(
console=['watt.py'],
options={'py2exe': {'bundle_files': 1}},
zipfile = None
)
I rewrite your setup script for you. This will work
from distutils.core import setup
import py2exe
# The targets to build
# create a target that says nothing about UAC - On Python 2.6+, this
# should be identical to "asInvoker" below. However, for 2.5 and
# earlier it will force the app into compatibility mode (as no
# manifest will exist at all in the target.)
t1 = dict(script="findpath.py",
dest_base="findpath",
uac_info="requireAdministrator")
console = [t1]
# hack to make windows copies of them all too, but
# with '_w' on the tail of the executable.
windows = [{'script': "findpath.py",
'uac_info': "requireAdministrator",
},]
setup(
version = "0.5.0",
description = "py2exe user-access-control",
name = "py2exe samples",
# targets to build
windows = windows,
console = console,
#the options is what you fail to include it will instruct py2exe to include these modules explicitly
options={"py2exe":
{"includes": ["sip","os","shutil"]}
}
)

Categories