hello everyone serdar yegalop here for infoworld in this episode of smartpython i'm going to take
a quick tour of nuetka a third-party project that compiles python programs to standalone executables
most experienced python users know that python doesn't have a native way to generate standalone
executables from a python program as this was never one of the language's design goals
over time we've seen third-party solutions to this problem
one of them is pi installer which i covered in a previous video an article
most of these solutions work by packaging the python interpreter along with the bytecode for
the program you want to run and any needed data files nuedka works a little bit like this but
it has a significant twist it translates all the python code in your program including its library
dependencies into c and it compiles it all into a single executable now it does have to bundle the
python runtime with it to work properly but the results can all be contained in a single folder
and can be redistributed as is it's also much harder to reverse engineer python code from the
resulting binary so it's a convenient way to obfuscate code if you need to do that
nuwitka recently reached a 1.0 release so it's now stable enough to consider using in production
you will want to test the resulting output thoroughly of course
but it is at a point now where it can work with the most common use cases quite easily
to use nuetca first you need to have a c compiler already installed in your system
in the case of windows you can use the msvc compiler the one in visual studio 2022 you
can get the command line version of that for free on linux you'll need gcc version 11.2 or higher
or on linux and on other platforms you can use the clang compiler where that's supported
now for this demonstration i've created a very simple graphical program one that uses the piglet
library this is the same graphics library i used for my previous python demo if we run this program
it doesn't do very much all it does is pop up a window and display some text but it depends on
at least one library outside the standard library namely piglet and it needs at least one data file
the typeface that we need to display this text and nutca can handle both of these scenarios
now since i have no it installed already if i want to use it all i have to do is type nuetka
from the command line followed by the name of a script compile
because my script requires some configuration to work properly i'm going to use a batch file that i
created earlier to launch the compilation process so let's take a look at that now the one option
that we need to supply obviously is the name of our script that's the most important option
and the other important option that we need to supply this is the one that invokes most
of nuedka's magic is this switch standalone what this does is it sets a lot of the other
options for newitka's most common use cases all in one fell swoop it compiles the program
and all of its dependencies into a single portable executable that resides in one directory we can
also use the output directory switch to indicate where to write the compiled files
and because my project needs data files we use the include data file switch to pass in
some wild card for file names to match against i mentioned before the one thing we need most is a
true type font file but you can include as many different kinds of files as you need and you can
also specify where in the target directory those files are supposed to go everything after this
equals sign here is a path in the target directory to place the files that are matched
and finally we can pass any flags that we would normally pass to the python interpreter
itself if we were running this as a conventional python program
in this case i'm using the o option to disable asserts as i would for something being placed
into production these few options tend to cover most of the common use cases for nutca you want
something you can freely redistribute so you use the standalone switch you want to bundle data
files for the application so you use include data files and you want to set any production flags
now i should note that notekit projects can take several minutes to compile for the sake of this
video i have already run a compilation pass on this project in the background
and you can see the results of that output here you'll be able to monitor the build
process as it runs normally but for now let's take a look at the actual compiled product
in this directory you can see we have the executable itself the python runtime and a
number of support libraries that are essential to python and finally we have that font file that we
need all this we could compress into a into a zip for instance if we wanted to redistribute that
and so when we launched the executable we have our program which runs identically to
the python version nuetca does try to make some optimizations for performance wherever possible
but your programs should run no slower than they do with regular python and they should be highly
compatible with other parts of the python ecosystem it wouldn't be python if it wasn't
outwardly compatible with other python programs i should also point out that the compiled binary can
be quite large in this case it's a 22 megabyte image this is because nutca has to traverse the
entire standard library and all of the third-party libraries that we've used in this example
and compile all of that to see and all of that code does add up however if you want to do a
quick build that isn't a redistributable but will just run in the same place as your existing
amp to test it you can do that by just typing nuetca and the name of your script so when i run
this here we have a much shorter build process because we're only building the script itself
and not any of its support libraries however that means it's also far less comprehensive
those other libraries are just interpreted like regular python libraries at runtime
but once we have a resulting exc it will run as expected and so you can use this as an interim
test technique to make sure that the compilation process for the core of your program works
correctly i should note that if your program is highly complex or if it has a lot of dependencies
or it uses a lot of python's dynamic behaviors at runtime you're going to need to do more work
to make new edc compile your application correctly for instance the locations of any dynamic imports
need to be hinted to the compiler with a command line option
include plug-in directory but with a little elbow grease you can find that most any python program
can be packed up as a redistributable with new edca and made useful to others
in future videos i may explore new income more deeply especially for packaging up
programs that use common third-party libraries like numpy but for now i encourage you to try
new edca on your own and see what results you get that's it for this video if you like it please
leave a comment below don't forget to subscribe to the tech talk channel on youtube and for more
smart python and more dev with serdar be sure to follow me on facebook youtube and inforw.com