Home > database >  How do I import a .so file in python without causing a %1 is not a valid win32 application error?
How do I import a .so file in python without causing a %1 is not a valid win32 application error?

Time:06-23

I have been attempting to include some compiled c code into my python code as follows:

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)

The .so file was created by compiling the file test.c (which contains no code) and executing the following command:

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

I would expect this to load the compiled code into the python program so that I can use it, but I get the following error:

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

As far as I can tell, this is meant to indicate that my compiled code is not compatible with my operating system, but I have no clue where that would go wrong (using VS 2022 if that helps).

CodePudding user response:

It should be that your python version is 64 bit and the .so file is 32 bit.

Check the python version first.

  1. Change the python version to be the same as the .so file.
  2. Recompile and generate a .so file consistent with the python version.

You can try

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