Using Python is wonderful. In fact, it so wonderful that you sometimes want other, non-python users to try out your scripts. You can tell them all day long to go to python.org and download the latest version. However, most people will ask you why you cannot just make an installer or a .exe for them instead.
I have made a few executable files myself and will share some ways in how I have done it. Note that I am using Python 3.6, results can differ if you are using Python 2 or Python 3.7. Occasionally, the packaging does not work without some modification when you are using the latest Python version. Try using an older Python version if the packaging does not work out of the box.
This post will cover the following tools for creating executables:
There are a couple of different projects made for creating the executable. Some of the freezers make a directory with the dependencies, and some can make a single executable or both. Creating an executable with Tkinter can be trickier, as you may need to include additional files as dependencies. Remember to also include your images, either in the same directory or included as code in your project.
PyInstaller works with Python 2.7 and Python 3.3-3.6. The usage is described in PyInstallers readthedocs. You can specify if you want to generate a single file, or a directory with the --onedir and --onefile. You can also choose to interact by console or window by default, with the --console and --windowed options.
To install Pyinstaller:
pip install pyinstaller
When you want to run PyInstaller, go to your scripts' directory and write the following in cmd:
[su_note]From PyInstallers documentation: "For certain uses, you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script: pyinstaller myscript.spec The myscript.spec file contains most of the information provided by the options that were specified when pyinstaller (or pyi-makespec) was run with the script file as the argument. You typically do not need to specify any options when running pyinstaller with the spec file. Only a few command-line options have an effect when building from a spec file."[/su_note]
You can also use a .bat file to make your packaging easier. See the example below.
pyinstaller --noconfirm --log-level=WARN ^ --onefile --nowindow ^ --add-data="README;." ^ --add-data="image1.png;img" ^ --add-binary="libfoo.so;lib" ^ --hidden-import=secret1 ^ --hidden-import=secret2 ^ --icon=..\MLNMFLCN.ICO ^ myscript.spec
I had been looking for an exe creator like this one. Nitratine has created Auto Py to Exe, a browser-based GUI application that you can use to create the .exe. The interface wraps around PyInstaller. The Auto Py to Exe script hosts a webpage and uses Google Chrome in app mode to display the page to the user. The interface is built with Eel.
I tried to freeze a basic Tkinter application and it worked flawlessly, converting my script to a single .exe file. There is a tutorial on how to install and use the script available on Youtube.
To install Auto Py to Exe, write the following in the cmd:
python -m pip install auto-py-to-exe
To start the Auto Py to Exe just write auto-py-to-exe in the cmd:
cx_Freeze packages your code with a script. There is a really helpful video tutorial on Youtube that Aayush Mahajan has made. I am referring to Aayush code below. You can find it in the Youtube video's description. First, you create the setup script. Put the script in your Python folder. Start the cmd and navigate to your Python folder, or go to the folder and press Shift + Rightclick to enable the cmd/PowerShell start here option.
# The cx_Freeze setup.py script from cx_Freeze import setup, Executable setup( name = "" , version = "0.1" , description = "" , executables = [Executable("")] , )
Start packaging by writing
c:\python36\python.exe setup.py build(Replace python36 with your version).
py2exe works similarly to cx_Freeze. It uses distutils to package the script. After installing (
pip install py2exe), use Py2Exe in the cmd by writing
python setup.py py2exe After the packaging or freezing is done, you can find your executable in the dist folder. The build folder is a temporary folder that you can safely delete.
# The Py2Exe setup script, setup.py for freezing the script hello.py from distutils.core import setup import py2exe setup(console=['hello.py'])
Nuitka is a bit different since it compiles your script into a C program which is linked to libpython. I haven't personally used Nuitka, but I will have it in mind if I need to make programs that need to run fast. You can find Nuitkas manual here. Kay Hayen, Nuitkas creator is on Twitter also.
I personally use PyInstaller and will start using Auto Py to Exe because of its simplicity. The alternatives are also very good to use, and there may be some cases where you want to use one over the other. Sometimes there are complications in packaging if you are using newer Python versions. I find that PyInstaller works with most of them.
Which one do you use, or are you using something completely different? Let me know, and happy packaging and freezing (or compiling with Nuitka :))!
Leave a Reply