I have created a python program with gui using tkinter. In the file there are several calls to external text files. I wanted to make a standalone executable using pyinstaller but the executable formed gives an error which is something of the sort "Script cant be executed" ( I don't remember the exact wordings).
How can I take that whole folder and convert it into a single .exe file?
When you build it, you'll either use something like pyinstaller , cx_Freeze , p2exe and there are many other ...
You can also refer to this video to understand how and what goes on HERE but basically, with most of these converters, you'll have a setup.py
file that contains information on how to setup your .py and you'll include your modules in the packages.
A setup.py file would usually look like this:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "somename",
version = "0.1",
description = "this is my software",
options = {"build_exe": build_exe_options},
# our package list is included in build options line 3
executables = [Executable("ourpy_file.py", base=base)])
cx_Freeze Documentation for more details. If we were to include any other files we would use (with cx_Freeze) include_files
and our build_exe_options would
look like:
build_exe_options = {"packages": ["os", "tkinter"],"include_files" :"<fullpath>"}
# this is if we were to include os and tkinter and include some other files
At first you need to create a setup.py
file . It would be better if you pip install cx_Freezer
than an other library like py2exe
or pyinstaller
.
Copy the DLLs
file from from the directory \\Python\\Python36-32\\
to the .py
file directory.
Then you write down the following code :
from cx_Freeze import setup, Executable
import sys
import os.path
base = None
if sys.platform == "win32":
base = "Win32GUI"
#os.environ['TCL_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tcl8.6'
#os.environ['TK_LIBRARY'] = r'C:\Program Files\Python35-32\tcl\tk8.6'
import os
includes = os.listdir("") #Include your main python file with its directory
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')
setup(name = 'Python_file',
version = "0.1",
description = "Python_file",
options = {'build_exe':{'include_files':includes,'packages':['pygame','tkinter','dill','PIL']}},
executables=[Executable("python_file.py", base= base)])
Open cmd
from the setup.py
file directory then run Python setup.py
.
To solve the error
Failed to execute script ---------- exe name
after compiling with pyinstaller
make sure you copy all the files
(shouldn't be .py file) you used in script to where you have your exe
file. This mostly cause by image files
you used in your program so do well to copy that into the folder
you have your standalone file
then start the exe
file again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.