繁体   English   中英

如何在 python 中导入 .so 文件而不导致 %1 is not a valid win32 application 错误?

[英]How do I import a .so file in python without causing a %1 is not a valid win32 application error?

我一直在尝试将一些已编译的 c 代码包含到我的 python 代码中,如下所示:

from ctypes import CDLL
import os

absolute_path = os.path.dirname(os.path.abspath(__file__))
# imports the c library
test_lib_path = absolute_path + '/theories/test.so'
test = CDLL(test_lib_path)

.so 文件是通过编译文件 test.c(不包含代码)并执行以下命令创建的:

gcc -o theories/test.so -shared -fPIC -O2 test.c

我希望这会将编译后的代码加载到 python 程序中以便我可以使用它,但是我收到以下错误:

OSError: [WinError 193] %1 is not a valid Win32 application

据我所知,这意味着我编译的代码与我的操作系统不兼容,但我不知道哪里会出错(如果有帮助,请使用 VS 2022)。

应该是你的python版本是64位,.so文件是32位。

首先检查python版本。

  1. 将 python 版本更改为与 .so 文件相同。
  2. 重新编译生成与python版本一致的.so文件。

你可以试试

# 64bit
gcc -m64 
# 32bit 
gcc -m32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM