DEV Community

il3ven
il3ven

Posted on

Nuitka vs PyInstaller | Distributing a Python application

Alternative title: Execute Python code from other languages such as JavaScript
Alternative title: Can Python applications be compiled?


In broad terms, there are two ways to distribute your application to your users. Such that, your users don't require a working development environment. In other words, a standalone application.

Bundle

We can create a bundle that includes the Python interpreter, your application and all the other code your application depends on. PyInstaller does this.

Compile

We can compile your Python application and all the other code your application depends on to machine code. Nuitka can do this.

Difference between PyInstaller and Nuitka

Nuitka will convert your Python code and all of its dependencies into C code. It then compiles the C code using a standard compiler like GCC to machine code. The machine code can then be directly executed without a dev environment.

On the other hand PyInstaller will bundle your Python code, all its dependencies and include a Python interpreter in that bundle. The bundle can then be run without a dev environment.

Since Nuitka produces machine code reverse engineering it is harder. It is more suitable if you want to protect your IP. We can protect our code using PyInstaller too but that process requires more manual work (more information) and is not plug and play like Nuitka.

In terms of output size, I have found both to produce applications of similar size.

In terms of speed, the final application produced by Nuitka should be faster because it is compiled to machine code. But in practice I haven't found any significant difference.

PyInstaller and Nuitka solve the same problem but take different routes. Since there is no compiling involved in PyInstaller the distribution process is much faster.

Technically PyInstaller does compile

I have stated above that PyInstaller bundles your Python code but technically it bundles .pyc files and not .py files. .pyc are intermediate files generated by the Python interpreter by compiling .py files. .pyc files are not machine code. A Python interpreter is required to execute those files.

Check this excellent StackOverflow answer for more information.

What to do with a standalone Python application?

  • Distribute it as a CLI application.
  • Distribute it as a standalone script.
  • Make a GUI application using PyQT.
  • Use your Python code as a backend. We can expose an HTTP server from our standalone application. This HTTP server can be called by a frontend client or another backend service written in a different language. A good use case is to include the standalone application in an Electron app and the JavaScript frontend can execute Python code by calling the backend.

Help me out

I am looking for remote opportunities, full time or part time. I'm based out of India. I specialize in web development but my expertise lie in solving technical problems irrespective of the tech stack. You can reach out at to.vc95 (at) gmail (dot) com.

Top comments (0)