笔者在安装使用
安装Python
我们最好手动安装Python,方法是直接在Python官网下载Windows的安装包。
打开链接
https://www.python.org/downloads/windows/
,然后选择正确的安装包(要选择64位的安装包, 比如
打开安装包执行安装程序,这个过程中需要注意的是要勾选选项"将Python安装路径添加到Path"
具体可以参考Python 安装教程(windows)
安装Nuitka
安装Nuitka可以直接使用pip进行安装
1 | pip install nuitka |
安装好之后我们可以测试一下Nuitka是否可以编译python文件
比如笔者在
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/env python # coding: utf-8 def job(): print("hello") if __name__ == "__main__": while True: job() sleep(60) |
我们尝试编译这个文件
1 2 | > cd C:\code\test_code > nuitka main.py |
这时你可能会看到如下报错:
1 2 3 4 5 6 7 8 9 | Error, cannot locate suitable C compiler. You have the following options: a) If a suitable Visual Studio version is installed, it will not be located automatically, unless you install pywin32 for the Python installation below "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_86". b) To make it find Visual Studio without registry execute from Start Menu the 'Visual Studio Command Prompt' or "vcvarsall.bat". That will add Visual Studio to the "PATH". And it then will be detected. c) Install MinGW64 to "C:\MinGW64" or "\MinGW", where then it is automatically detected or add it to PATH before executing Nuitka. But be sure to pick the proper variant (32/64 bits, your Python arch is 'x86'), or else cryptic errors will be shown. Normal MinGW will not work! MinGW64 does not mean 64 bits, just better Windows compatibility. Cygwin based gcc will not work. MSYS2 based gcc will not work. clang-cl will only work if MSVC already worked. |
这是由于我们并没有安装C编译器的缘故,因此我们还需要安装C编译器
安装MinGW64
要想使用Nuitka,我们就需要C编译器,而这通常使用MinGW64(Minimal GNU for Windows)会比较好, MinGW64是一套适用于Windows的开发环境,这个开发环境有一组开源的工具集,其中就有用于C/C++语言编译的gcc,而
首先要下载安装包,下载链接
https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/
最新的版本是8.1.0,找到这个版本的安装包
MinGW64安装包
下载之后,解压到你的安装目录(解压后就算安装了,不需要执行安装exe),比如
然后将安装目录下的
1 2 3 4 5 | >gcc --version gcc (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) 8.1.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
当看到如上所示的文字说明我们已经安装成功。
使用Nuitka进行编译
这时,我们再使用Nuitka进行编译试下
1 | > nuitka --mingw64 main.py |
可以看到这次成功了,在test_code目录下新生成了一个exe可执行文件。