Nuitka

Nuitka

Python编译器 将Python代码转换为高性能C程序

Nuitka是一个Python编译器,可将Python 2和Python 3代码编译为C程序。它支持全部Python语言特性,与CPython高度兼容,并能与现有Python库无缝集成。Nuitka通过优化来提高性能,同时保持与标准Python的兼容性。它可用于编译单个模块、整个程序包或创建独立可执行文件,是提升Python代码执行速度和简化分发的有效工具。

NuitkaPython编译器C编译器跨平台性能优化Github开源项目

.. image:: https://img.shields.io/pypi/pyversions/Nuitka.svg :target: https://pypi.org/project/Nuitka

.. image:: https://badge.fury.io/py/Nuitka.svg :target: https://pypi.org/project/Nuitka

.. image:: https://img.shields.io/badge/Contributor%20Covenant-v1.4%20adopted-ff69b4.svg :target: CODE_OF_CONDUCT.md

#################### Nuitka User Manual ####################

This document is the recommended first read when you start using Nuitka. On this page, you will learn more about Nuitka fundamentals, such as license type, use cases, requirements, and credits.

.. contents:: Table of Contents :depth: 1 :local: :class: page-toc

Nuitka is the Python compiler. It is written in Python. It is a seamless replacement or extension to the Python interpreter and compiles every construct that Python 2 (2.6, 2.7) and Python 3 (3.4 - 3.12) have, when itself run with that Python version.

It then executes uncompiled code and compiled code together in an extremely compatible manner.

You can use all Python library modules and all extension modules freely.

Nuitka translates the Python modules into a C level program that then uses libpython and static C files of its own to execute in the same way as CPython does.

All optimization is aimed at avoiding overhead, where it's unnecessary. None is aimed at removing compatibility, although slight improvements will occasionally be done, where not every bug of standard Python is emulated, e.g. more complete error messages are given, but there is a full compatibility mode to disable even that.


Requirements


To ensure smooth operation of Nuitka, make sure to follow system requirements, that include the following components:

.. contents:: :depth: 1 :local:

C Compiler

You need a C compiler with support for C11 or alternatively a C++ compiler for C++03 [#]_.

Currently, this means, you need to use one of these compilers:

  • The MinGW64 C11 compiler, on Windows, must be based on gcc 11.2 or higher. It will be automatically downloaded if no usable C compiler is found, which is the recommended way of installing it, as Nuitka will also upgrade it for you.

  • Visual Studio 2022 or higher on Windows [#]_. English language pack for best results (Nuitka filters away garbage outputs, but only for English language). It will be used by default if installed.

  • On all other platforms, the gcc compiler of at least version 5.1, and below that the g++ compiler of at least version 4.4 as an alternative.

  • The clang compiler on macOS X and most FreeBSD architectures.

  • On Windows, the clang-cl compiler on Windows can be used if provided by the Visual Studio installer.

.. [#]

Support for this C11 is given with gcc 5.x or higher or any clang version.

The older MSVC compilers don't do it yet. But as a workaround, with Python 3.10 or older, the C++03 language standard is significantly overlapping with C11, it is then used instead.

.. [#]

Download for free from https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx (the community editions work just fine).

The latest version is recommended, but not required. On the other hand, there is no need to except to support pre-Windows 10 versions, and they might work for you, but support of these configurations is only available to commercial users.

Python

Python 2 (2.6, 2.7) and Python 3 (3.4 — 3.12) are supported. If at any moment, there is a stable Python release that is not in this list, rest assured it is being worked on and will be added.

.. important::

For Python 3.4 and only that version, we need other Python version as a compile time dependency.

Nuitka itself is fully compatible with all listed versions, but Scons as an internally used tool is not.

For these versions, you need a Python2 or Python 3.5 or higher installed as well, but only during the compile time. That is for use with Scons (which orchestrates the C compilation), which does not support the same Python versions as Nuitka.

In addition, on Windows, Python2 cannot be used because clcache does not work with it, there a Python 3.5 or higher needs to be installed.

Nuitka finds these needed Python versions (e.g. on Windows via registry) and you shouldn't notice it as long as they are installed.

Increasingly, other functionality is available when another Python has a certain package installed. For example, onefile compression will work for a Python 2.x when another Python is found that has the zstandard package installed.

.. admonition:: Moving binaries to other machines

The created binaries can be made executable independent of the Python installation, with --standalone and --onefile options.

.. admonition:: Binary filename suffix

The created binaries have an .exe suffix on Windows. On other platforms they have no suffix for standalone mode, or .bin suffix, that you are free to remove or change, or specify with the -o option.

The suffix for acceleration mode is added just to be sure that the original script name and the binary name do not ever collide, so we can safely overwrite the binary without destroying the original source file.

.. admonition:: It has to be CPython, Anaconda Python, or Homebrew

You need the standard Python implementation, called "CPython", to execute Nuitka because it is closely tied to implementation details of it.

.. admonition:: It cannot be from the Windows app store

It is known that Windows app store Python definitely does not work, it's checked against.

.. admonition:: It cannot be pyenv on macOS

It is known that macOS "pyenv" does not work. Use Homebrew instead for self compiled Python installations. But note that standalone mode will be worse on these platforms and not be as backward compatible with older macOS versions.

Operating System

Supported Operating Systems: Linux, FreeBSD, NetBSD, macOS, and Windows (32 bits/64 bits/ARM).

Others will work as well. The portability is expected to be generally good, but the e.g. Nuitka's internal Scons usage may have to be adapted or need flags passed. Make sure to match Python and C compiler architecture, or else you will get cryptic error messages.

Architecture

Supported Architectures are x86, x86_64 (amd64), and arm, likely many, many more.

Other architectures are expected to also work, out of the box, as Nuitka is generally not using any hardware specifics. These are just the ones tested and known to be good. Feedback is welcome. Generally, the architectures that Debian supports can be considered good and tested, too.


Usage


Command Line

The recommended way of executing Nuitka is <the_right_python> -m nuitka to be absolutely certain which Python interpreter you are using, so it is easier to match with what Nuitka has.

The next best way of executing Nuitka bare that is from a source checkout or archive, with no environment variable changes, most noteworthy, you do not have to mess with PYTHONPATH at all for Nuitka. You just execute the nuitka and nuitka-run scripts directly without any changes to the environment. You may want to add the bin directory to your PATH for your convenience, but that step is optional.

Moreover, if you want to execute with the right interpreter, in that case, be sure to execute <the_right_python> bin/nuitka and be good.

.. admonition:: Pick the right Interpreter

If you encounter a SyntaxError you absolutely most certainly have picked the wrong interpreter for the program you are compiling.

Nuitka has a --help option to output what it can do:

.. code:: bash

nuitka --help

The nuitka-run command is the same as nuitka, but with a different default. It tries to compile and directly execute a Python script:

.. code:: bash

nuitka-run --help

This option that is different is --run, and passing on arguments after the first non-option to the created binary, so it is somewhat more similar to what plain python will do.

Installation

For most systems, there will be packages on the download page <https://nuitka.net/doc/download.html>__ of Nuitka. But you can also install it from source code as described above, but also like any other Python program it can be installed via the normal python setup.py install routine.

Notice for integration with GitHub workflows there is this Nuitka-Action <https://github.com/Nuitka/Nuitka-Action>__ that you should use that makes it really easy to integrate. You ought to start with a local compilation though, but this will be easiest for cross platform compilation with Nuitka.

License

Nuitka is licensed under the Apache License, Version 2.0; you may not use it except in compliance with the License.

You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Tutorial Setup and build on Windows


This is basic steps if you have nothing installed, of course if you have any of the parts, just skip it.

Setup

Install Python

  • Download and install Python from https://www.python.org/downloads/windows

  • Select one of Windows x86-64 web-based installer (64 bits Python, recommended) or x86 executable (32 bits Python) installer.

  • Verify it's working using command python --version.

Install Nuitka

  • python -m pip install nuitka

  • Verify using command python -m nuitka --version

Write some code and test

Create a folder for the Python code

  • mkdir HelloWorld

  • make a python file named hello.py

.. code:: python

def talk(message): return "Talk " + message

def main(): print(talk("Hello World"))

if name == "main": main()

Test your program

Do as you normally would. Running Nuitka on code that works incorrectly is not easier to debug.

.. code:: bash

python hello.py


Build it using

.. code:: bash

python -m nuitka hello.py

.. note::

This will prompt you to download a C caching tool (to speed up repeated compilation of generated C code) and a MinGW64 based C compiler, unless you have a suitable MSVC installed. Say yes to both those questions.

Run it

Execute the hello.exe created near hello.py.

Distribute

To distribute, build with --standalone option, which will not output a single executable, but a whole folder. Copy the resulting hello.dist folder to the other machine and run it.

You may also try --onefile which does create a single file, but make sure that the mere standalone is working, before turning to it, as it will make the debugging only harder, e.g. in case of missing data files.


Use Cases


Use Case 1 — Program compilation with all modules embedded

If you want to compile a whole program recursively, and not only the single file that is the main program, do it like this:

.. code:: bash

python -m nuitka --follow-imports program.py

.. note::

There are more fine-grained controls than --follow-imports available. Consider the output of nuitka --help. Including fewer modules into the compilation, but instead using normal Python for it, will make it faster to compile.

In case you have a source directory with dynamically loaded files, i.e. one which cannot be found by recursing after normal import statements via the PYTHONPATH (which would be the recommended way), you can always require that a given directory shall also be included in the executable:

.. code:: bash

python -m nuitka --follow-imports --include-plugin-directory=plugin_dir program.py

.. note::

If you don't do any dynamic imports, simply setting your PYTHONPATH at compilation time is what you should do.

Use --include-plugin-directory only if you make __import__() calls that Nuitka cannot predict, and that come from a directory, for everything from your Python installation, use --include-module or --include-package.

.. note::

The resulting filename will be program.exe on Windows, program.bin on other platforms, but --output-filename allows changing that.

.. note::

The resulting binary still depends on CPython and used C extension modules being installed.

If you want to be able to copy it to another machine, use --standalone and copy the created program.dist directory and execute the program.exe (Windows) or program (other platforms) put inside.

Use Case 2 — Extension Module compilation

If you want to compile a single extension module, all you have to do is this:

.. code:: bash

python -m nuitka --module some_module.py

The resulting file some_module.so can then be used instead of some_module.py.

.. important::

The filename of the produced extension module must not be changed as Python insists on a module name derived function as an entry point, in this case PyInit_some_module and renaming the file will not change that. Match the filename of the source code to what the binary name should be.

.. note::

If both the extension module and the source code of it are in the same directory, the extension module is loaded. Changes to the source code only have effect once you recompile.

.. note::

The option --follow-import-to works as well, but the included modules will only become importable after you imported the some_module name. If these kinds of imports are invisible to Nuitka, e.g. dynamically created, you can use --include-module or --include-package in that case, but for static imports it should not be needed.

.. note::

An extension module can never include other extension modules. You will have to create a wheel for this to be doable.

.. note::

The resulting extension module can only be loaded into a CPython of the same version and doesn't include other extension modules.

Use Case 3 — Package compilation

If you need to compile a whole package and embed all modules, that is also feasible, use Nuitka like this:

.. code:: bash

python -m nuitka --module some_package --include-package=some_package

.. note::

The inclusion of the package contents needs to be provided manually; otherwise, the package is mostly empty. You can be more specific if you like, and only include part of it, or exclude part of it, e.g. with --nofollow-import-to='*.tests' you would not include the unused test part of your code.

.. note::

Data files located inside the package will not be embedded by this process, you need to copy them yourself with this approach. Alternatively, you can use the file embedding of Nuitka commercial <https://nuitka.net/doc/commercial/protect-data-files.html>__.

Use Case 4 — Program Distribution

For distribution to other systems, there is the standalone mode, which produces a folder for which you can specify --standalone.

.. code:: bash

python -m nuitka --standalone program.py

Following all imports is default in this mode. You can selectively exclude modules by specifically saying --nofollow-import-to, but then an ImportError will be raised when import of it is attempted at program run time. This may cause different behavior, but it may also improve your compile time if done wisely.

For data files to be included, use the option --include-data-files=<source>=<target> where the source is a file system path, but the target has to be specified relative. For the standalone mode, you can also copy them manually, but this can do extra checks, and for the onefile mode, there is no manual copying possible.

To copy some or all file in a directory, use the option --include-data-files=/etc/*.txt=etc/ where you get to specify shell patterns for the files, and a subdirectory where to put them, indicated by the trailing slash.

.. important::

Nuitka does not consider data files code, do not include DLLs, or Python files as data files, and expect them to work, they will not, unless you really know what you are doing.

In the following, non-code data files are all files, not matching on of these criterions.

+------------+----------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+ | Suffix | Rationale | Solution | +============+========================================================================================+========================================================================================================+ | .py | Nuitka trims even the stdlib modules to

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯�飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

openai-agents-python

openai-agents-python

OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。

openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。

下拉加载更多