K.Mulier
K.Mulier

Reputation: 9640

Compile Python 3.6 script to standalone exe with Nuitka on Windows 10

Note:
Before marking this question as duplicate, please verify that the other question answers the topic for this setup:

 

1. How I build my exe

I'll first explain how I build my executable. Suppose I have a folder with a simple python script that I want to build:

enter image description here

The buildscript.py looks like this:

#####################################################
#               NUITKA BUILD SCRIPT                 #
#####################################################
# Author: Matic Kukovec
# Date: April 2018

import os
import platform


NUITKA = "C:/Python36/Scripts/nuitka3-script.py"  # Path where my nuitka3-script.py is
CWD = os.getcwd().replace("\\", "/")
MSVC = "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Auxiliary/Build/vcvars64.bat"
PYTHON_VERSION = "3.6"
PYTHON_EXE_PATH= "C:/Python36/python.exe"
NUMBER_OF_CORES_FOR_COMPILATION = 1 # 1 is the safest choice, but you can try more

# Generate command
command = '"{}" amd64 &'.format(MSVC)
command += "{} ".format(PYTHON_EXE_PATH)
command += "{} ".format(NUITKA)
command += "--python-version={} ".format(PYTHON_VERSION)
command += "--output-dir={}/output ".format(CWD)
command += "--verbose "
command += "--jobs={} ".format(NUMBER_OF_CORES_FOR_COMPILATION)
command += "--show-scons "
# command += "--windows-disable-console "
# command += "--icon={}/myicon.ico ".format(CWD)
command += "--standalone "
# command += "--run "
command += "{}/cubeimporter.py ".format(CWD)
os.system(command)

print("END")

 

2. Result of the build

After the build finishes, the folder looks like this (see picture below). As you can see, there are plenty of other files sitting next to the executable. I can see .dll and .pyd files.

enter image description here

 

3. Desired result

I wish I could build just a standalone executable. No dll- or other files needed. When I put the executable on a thumb drive and stick it into another computer (running Windows 10, 64-bit), it should just work. Even if there is no Python installed on that computer.

Is this possible with Nuitka?
If no, is it possible with another Python compiler?
Please provide all the steps needed, one-by-one :-)

Upvotes: 16

Views: 26173

Answers (4)

Great Owl
Great Owl

Reputation: 51

You need something like this, for windows

py -m nuitka --show-scons --show-progress --onefile --remove-output --warn-implicit-exceptions --warn-unusual-code --windows-disable-console

--remove-output takes care of removing the garbage py -m nuitka --help

--remove-output Removes the build directory after producing the module or exe file. Defaults to off.

If you do tkinter, insert --enable-plugin=tk-inter

Upvotes: 0

Hamza Zubair
Hamza Zubair

Reputation: 1420

In your buildscript.py file

use --onefile instead of --standalone

All the files will still be created but you don't need them and you can delete them.

Upvotes: 4

Fabian Heller
Fabian Heller

Reputation: 470

Easier than Nuitka for a single executable is e.g. PyInstaller: pyinstaller --onefile program.py (to disable the console window for GUI applications add the -w option).

To create a single executable with Nuitka, you can create a SFX archive from the generated files. You can run Nuitka with the --standalone option which generates a program_dist directory.

Create then a 7-Zip SFX config file config.txt: ;!@Install@!UTF-8! GUIMode="2" ExecuteFile="%%T/program_dist/program.exe" ;!@InstallEnd@!

Then get the 7-Zip SFX from https://github.com/chrislake/7zsfxmm (from releases – 7zsd_extra_171_3901.7z) and unpack the 7zsd_All_x64.sfx file.

Pack then the program_dist with 7-Zip (so the folder is included in the archive) to program.7z. Then, an SFX can be created with copy /b 7zsd_All_x64.sfx + config.txt + program.7z single_executable.exe.

On Unix, you can also create yourself an SFX if you create a tar archive and append it to a shell script which extract it and unpack it, for details see https://www.linuxjournal.com/node/1005818.

Upvotes: 12

Zahu
Zahu

Reputation: 33

Open console and type:

nuitka --recurse-all program.py

An exe file will be created. Run it.

Upvotes: 0

Related Questions