7.python环境打包32位:nuitka、Cython使用WinGw32进行32位编译打包

在32位Windows环境下,使用Cython编译Python文件为pyd时遇到了错误,主要是由于编译器配置问题。尝试了修改Python内部调度和更改wingw32为wingw64的路径,最终通过调整编译命令解决了问题。文章详细记录了解决过程,并分享了遇到的取余运算符导致的导入错误及其解决方法。
摘要由CSDN通过智能技术生成


前言

由于系统内部分电脑需要使用32位windows,导致平时64位系统打包都失效。
这个时候在64位环境换成32位,遇到一些问题(由于windows系统安装不了vs相关编译模块,不得已采用wingw替代),导致必须使用WinGw32进行打包。

准备清单:python 32位版本、winGw32,由于我采用nuitka进行打包,可以使用nuitka第一次打包提示下载的winGw32版本进行安装。具体配置看:
一文讲清nuitka打包
6.Cython使用WinGw编译pyd


需求:使用Cython把python单个文件编译位pyd文件

一、基础安装一笔带过

下载python32位版本、根据项目需求安装对应包;

二、Cython使用wingw32遇到问题

 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位的,还是一样的提示。为什么?

三、修改python内部调度,可行不?

这个可能可行,我看了一下代码,有点复杂。暂时放弃

四、昨夜思考改名怎么样

把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自己验证一下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿飞哥666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值