由于系统内部分电脑需要使用32位windows,导致平时64位系统打包都失效。
这个时候在64位环境换成32位,遇到一些问题(由于windows系统安装不了vs相关编译模块,不得已采用wingw替代),导致必须使用WinGw32进行打包。
准备清单:python 32位版本、winGw32,由于我采用nuitka进行打包,可以使用nuitka第一次打包提示下载的winGw32版本进行安装。具体配置看:
一文讲清nuitka打包
6.Cython使用WinGw编译pyd
需求:使用Cython把python单个文件编译位pyd文件
下载python32位版本、根据项目需求安装对应包;
python setup.py build_ext --inplace -DMS_WIN64
----1----
----2----
running build_ext
dllwrap: WARNING: dllwrap is deprecated, use gcc -shared or ld -shared instead
building 'ftp' extension
C:\mingw64\bin\gcc.exe -mdll -O -Wall -DMS_WIN64=1 -IC:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\include -IC:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\include -c ftp.c -o build\temp.win32-3.8\Release\ftp.o
ftp.c:217:41: warning: division by zero [-Wdiv-by-zero]
217 | enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
| ^
ftp.c:217:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant
217 | enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
| ^~~~~~~~~~~~~~~~~~~~~~~~
Traceback (most recent call last):
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\cygwinccompiler.py", line 173, in _compile
self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\ccompiler.py", line 910, in spawn
spawn(cmd, dry_run=self.dry_run)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\spawn.py", line 38, in spawn
_spawn_nt(cmd, search_path, dry_run=dry_run)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\spawn.py", line 80, in _spawn_nt
raise DistutilsExecError(
distutils.errors.DistutilsExecError: command 'C:\\mingw64\\bin\\gcc.exe' failed with exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 340, in run
self.build_extensions()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 449, in build_extensions
self._build_extensions_serial()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 474, in _build_extensions_serial
self.build_extension(ext)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 528, in build_extension
objects = self.compiler.compile(sources,
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\ccompiler.py", line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\cygwinccompiler.py", line 176, in _compile
raise CompileError(msg)
distutils.errors.CompileError: command 'C:\\mingw64\\bin\\gcc.exe' failed with exit status 1
Traceback (most recent call last):
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\cygwinccompiler.py", line 173, in _compile
self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\ccompiler.py", line 910, in spawn
spawn(cmd, dry_run=self.dry_run)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\spawn.py", line 38, in spawn
_spawn_nt(cmd, search_path, dry_run=dry_run)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\spawn.py", line 80, in _spawn_nt
raise DistutilsExecError(
distutils.errors.DistutilsExecError: command 'C:\\mingw64\\bin\\gcc.exe' failed with exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\dist.py", line 966, in run_commands
self.run_command(cmd)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\dist.py", line 985, in run_command
cmd_obj.run()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 340, in run
self.build_extensions()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 449, in build_extensions
self._build_extensions_serial()
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 474, in _build_extensions_serial
self.build_extension(ext)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\command\build_ext.py", line 528, in build_extension
objects = self.compiler.compile(sources,
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\ccompiler.py", line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\cygwinccompiler.py", line 176, in _compile
raise CompileError(msg)
distutils.errors.CompileError: command 'C:\\mingw64\\bin\\gcc.exe' failed with exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\proAll\desPyExe\src\core\buildPyd.py", line 25, in buildPyd
setup(name=name, ext_modules=cythonize(file))
File "C:\Users\HYNSYH\AppData\Local\Programs\Python\Python38-32\lib\distutils\core.py", line 165, in setup
raise SystemExit("error: " + str(msg))
SystemExit: error: command 'C:\\mingw64\\bin\\gcc.exe' failed with exit status 1
看到“C:\mingw64\bin\gcc.exe”这个一脸懵逼,实际我用到是32位,而用到了64位去;网上搜索很多解决办法。
其中比较关键的一句话,引起了我注意,与64环境的编译语句不一样
32位:
python setup.py build --compiler=mingw32
64位:
python setup.py build_ext --inplace -DMS_WIN64
我把调度语句改成32位的,还是一样的提示。为什么?
这个可能可行,我看了一下代码,有点复杂。暂时放弃
把wingw32改成wingw64,path配置也改掉,是不是就可以了?
测试执行“python setup.py build_ext --inplace -DMS_WIN64”提示不对;
测试执行"python setup.py build --compiler=mingw32"编译通过
需要验证:nuitka打包是否存在问题呢?如果存在,以后打包就比较麻烦,需要来回改路径名称 wingw32<–>wingw64
编译pyd文件之后,调用提示:
ImportError: DLL load failed while importing py_dynamicEncryption: 找不到指定的模块。
看了下程序运行imort部分代码,再调用这个模块之前已经import过其他模块,由此可见,以上步骤是可以编译出可以运行的pyd文件的;
针对次文件进行问题点位,发现一行代码操作则不能够import进来:
salt = math.round(math.random() * 1000000000) % 100000000
解决办法:使用下面函数替代这个 % 符号即可import成功。
# 取模,Python中可直接用%,计算模,r = a % b
def mod(a, b):
c = a // b
r = a - c * b
return r
# 取余
def rem(a, b):
c = int(a / b)
r = a - c * b
return r
def test():
#s = math.round(math.random() * 10000) % 10000
s = mod(math.round(math.random() * 10000), 10000) # 具体使用mod还是rem自己验证一下