How I create the Treebard .exe
Aug 22, 2024 17:46:48 GMT -8
Post by Uncle Buddy on Aug 22, 2024 17:46:48 GMT -8
Last updated July 30, 2024
# ************************************************
# ************************************************
CXFREEZE LINKS:
https://www.youtube.com/watch?v=y4s_lG0IuRc (2023--CodersLegacy)
https://www.youtube.com/watch?v=2HxLYz7rH_M (add icons--CodersLegacy)
https://www.youtube.com/watch?v=gsSjIx_uFG4 (virtual env w/ pyinstaller--just replace the end part with cxfreeze commands--CodersLegacy)
https://www.youtube.com/watch?v=pFF47kliTlA (2023--36 minutes long) (also tells how to make exe for older windows and older machines) (HOW TO INCLUDE DATA FILES ~15:00) (explains how he determined what could be excluded if you don't want to use a venv)(some parts are more technical but should watch it anyway)
https://www.youtube.com/watch?v=DoHWJV8iVTQ (how to solve problems esp. includes & paths)
To switch versions and NOT use virtual environments, just change the path to the Python version you want: https://www.youtube.com/watch?v=cT7bH-no3MI
VIRTUALENV:
https://www.youtube.com/watch?v=ghc1L_yClm8
WHAT I DID:
use virtual environment instead of EXCLUDES to keep unwanted libs from being auto-included when making an exe.
create a directory for all your virtual environments eg: d:/py_env OR create a directory where you just want to keep THIS env (I did the latter: d:/treebard)
install the other Python version you want to use not globally -- it doesn't need to be in the path so don't check that box when installing. Use 32-bit so cx_freeze exe will work on 32-bit or 64-bit computers. (I got 3.11.9 32-bit Windows installer. This Python can't be used with Windows 7 or earlier.)
run cmd as administrator:
python --version (will show the latest installation/the one it finds in the path)
C:\Windows\System32>python --version
Python 3.12.2
pip list (will list all the python packages installed into the current (global) python env)
C:\Windows\System32>pip list
Package Version
------------------------- --------
altgraph 0.17.4
auto-py-to-exe 2.42.0
bottle 0.12.25
bottle-websocket 0.2.9
cffi 1.16.0
distlib 0.3.8
Eel 0.16.0
filelock 3.13.4
future 1.0.0
gevent 24.2.1
gevent-websocket 0.10.1
greenlet 3.0.3
Nuitka 2.1.2
ordered-set 4.1.0
packaging 23.2
pefile 2023.2.7
pillow 10.2.0
pip 24.0
platformdirs 4.2.0
pycparser 2.21
pyinstaller 6.5.0
pyinstaller-hooks-contrib 2024.3
pyparsing 3.1.1
pywin32-ctypes 0.2.2
setuptools 69.1.1
virtualenv 20.25.3
whichcraft 0.6.1
zope.event 5.0
zope.interface 6.2
zstandard 0.22.0
venv comes with Python but will only use your global python installation so:
pip install virtualenv
cd to d:/treebard
C:\Windows\System32>d:
D:\>cd treebard
D:\treebard>
to create an env that uses the GLOBAL python (the one in your PATH):
virtualenv tbard_037
BUT TO CREATE an env that uses another python:
Get the full path to the version you want to use, eg:
type python in search, right click open file location, right-click the shortcut shown for the right version > properties > shortcut > copy the target, paste into cmd
virtualenv -p C:\Users\Lutherman\AppData\Local\Programs\Python\Python311-32\python.exe tbard_037
created virtual environment CPython3.11.9.final.0-32 in 476ms
creator CPython3Windows(dest=D:\treebard\tbard_037, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Lutherman\AppData\Local\pypa\virtualenv)
added seed packages: pillow==10.3.0, pip==24.0, setuptools==69.5.1, wheel==0.43.0
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
(The first tbard_037 env was accidentally deleted when I changed the name of the path to d:/treebard instead of d:/tbard. When I activated the env and did pip list I saw that pillow didn't have to be installed this time, so it must have remembered the env name and restored it when I used the name again.)
activate the environment:
tbard_037\scripts\activate (USE BACKWARD SLASHES)
The name of the venv will now appear at the start of the cmd prompt in parentheses.
(tbard_037) D:\treebard>
(tbard_037) D:\treebard>python --version
Python 3.11.9
(tbard_037) D:\treebard>pip list
Package Version
---------- -------
pillow 10.3.0
pip 24.0
setuptools 69.5.1
wheel 0.43.0
to run the python interactive console just type
python <enter>
to quit interactive python
quit()
to install libs:
(tbard_037) D:\treebard>pip install pillow
Collecting pillow
Downloading pillow-10.3.0-cp311-cp311-win32.whl.metadata (9.4 kB)
Downloading pillow-10.3.0-cp311-cp311-win32.whl (2.2 MB)
---------------------------------------- 2.2/2.2 MB 9.4 MB/s eta 0:00:00
Installing collected packages: pillow
Successfully installed pillow-10.3.0
To run the app:
python d:/treebard/treebard_root_037.py
To close the app: use the X on the main window. You can use CTRL-C in the terminal but it takes longer. (still true?The app has been provided with 2 lines of code at the end of the main script which keep the console open separately in hopes that people who download the .exe will be able to report error messages by copying them from the terminal.)
To edit the app: just edit and rerun, there's no need to deactivate the env each time.
to deactivate the env:
deactivate
cxfreeze setup.py: (These notes are a combination of older and newer instructions. The setup.py should be made automatically and you can look at it before running the command to build the exe.)
inside the venv:
pip install cx_freeze
cxfreeze-quickstart (you could instead make your own setup.py but this does it for you)
the app's main .py script has to be
--a sibling of the virtual env and
--a sibling of setup.py
A file called requirements.txt has to be a sibling of the virtual env if you don't want to install dependencies manually one-by-one (not necessary in our case, there are only 2 to install).
In requirements.txt each line lists a dependency and version eg:
pillow==10.3.0
cx_Freeze==6.15.16
...this will get the intended version of each library into the environment instead of letting pyinstaller or cxfreeze grab whatever version is available/current at pip.
pip install -r requirements.txt
cx_freeze will get most included libs but if there is a circular dependency problem, the lib that the error complains about can be copy-pasted into build_options['packages'])
To show console change in call to setup():
inside of named parameter executables=[...]
instead of: `base="Win32GUI"`
this: `base="Console"`
To create the exe:
python setup.py build (this will be added to the console automatically and the terminal will ask whether to run setup.py. SAY NO (the default; just hit Enter) and edit setup.py for inclusions as below.)
Manually run setup.py when finished editing it:
python setup.py build
A folder called build will be created as a sibling of setup.py. In the folder will be a folder with exe in the name. In the exe folder there will be the .exe file, some python dlls, and a folder called lib which will contain the libraries that were added by cx_freeze as dependencies. Also in lib will be the non-Python directories you manually include by adding them manually to setup.py, e.g. to add data, etc, images, and default directories I added them like this:
build_options = {
'packages': ['data', 'etc', 'images', 'default'],
'excludes': [],
'include_files': [("favicon.ico", "share/favicon.ico")]}
the .ico should be manually placed in the file structure as a sibling of setup.py. Add it to the list `executables` in setup.py eg `icon=favicon`. DON'T INCLUDE THE EXTENSION In Windows .ico will be auto-added. When finished, the .exe will use the icon instead of the windows icon. (This won't work if you use the python version that is downloaded from the Windows store.) That gets your .ico to appear in Windows file explorer. More has to be done for the icon to show up on your Tkinter window borders. Here's what I did in my main .py script to define the icon and then the path to it, which is different in the .py directory structure vs. the .exe directory structure:
if extension in ("py", "pyc", "pyw"):
treebard.iconbitmap(default=f"{app_path}/favicon.ico")
elif extension == "exe":
ico_path = app_path.replace("/lib", "/share")
treebard.iconbitmap(default=f"{ico_path}/favicon.ico")
Similarly, in files.py I had to add a condition so that if the script is running as an .exe instead of a .py, a different directory structure will be created for the paths. The path to the .exe is customized here for how cx_Freeze does things. If using Pyinstaller or some other .exe maker, this would have to be done according to the folder structure created by that app when it builds the .exe.
If there's an error complaining about missing dll, go to the python installation eg c: > users >lutherman > AppData > Local > Programs > Python > Python311 > Lib > site-packages
...find the dll or missing directory there, copy-paste it into your venv ... build > *exe* > lib
...there may also be a way to include it via build_options['packages']
The setup.py file creates the .exe when it runs. The setup.py can be run, edited, and rerun so you have a lot of control over making EXEs.
If there's a problem but the console doesn't stay open, Run the application in a command shell to see whether there are errors shown on the console.
NOT LIKE THIS: python treebard_root_037.py
DO LIKE THIS: C:\Users\Lutherman\Desktop\build\exe.win32-3.11\treebard_20240420f.exe
# ************************************************
# HERE IS THE SECOND setup.py CODE THAT WORKED PERFECTLY July 30, 2024:
from cx_Freeze import setup, Executable
build_options = {
'packages': ['data', 'etc', 'images', 'default'],
'excludes': [],
'include_files': [("favicon.ico", "share/favicon.ico")]}
base = 'console'
executables = [
Executable('treebard_root_041.py', base=base, icon='favicon')]
setup(name='Treebard Genealogy Software',
# version = '0.0.0',
description = 'second cx_Freeze .exe app',
options = {'build_exe': build_options},
executables = executables)
# ************************************************