简体   繁体   中英

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

I'm trying to use CTypes to call a C function from Python, but keep getting this error: OSError: [WinError 193] %1 is not a valid Win32 application . I'm using the WSL to compile the C file into a.so file using this command: gcc -fPIC -shared -o libtest.so test.c and have this Python code:

from ctypes import *


lib = CDLL('./libtest.so')
print(lib.square(5))

I can run the Python file using the WSL, but I can't run it using an Anaconda environment from PyCharm. I'm guessing the error is due to the C object being created in the WSL. Is there a way to fix the error and make it so I can compile the C files on the WSL and use the compiled object files in an Anaconda environment?

I had a similar problem with ctypes. In my case, the problem was with the '.so' files, so I followed this tutorial ( https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170 ) to create '.dll' files. Then the rest of the python code remains the same, except './libtest.so' -> './libtest.dll'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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