0

Ctypes is able to load .so files until I use one that has pthread_create() in it's source code. Then it gives the error: """

FileNotFoundError: Could not find module '\the_path_to_module\module_name' (or one of it's dependencies). Try using the full path with constructor syntax.

"""

I'm using the full path and it works fine until I put pthread functions in the source code. I am able to still load it if pthread_t variables are declared, but soon as pthread_create() is coded in, ctypes refuses to load it.

I'm able to run standalone exe files using pthread just fine. I'm using Windows 10 and Python 3.10

Here's basically what I have coded that throws the error:

C code in the .so file:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// I can include pthread.h and declare pthread_t variables and ctypes will load the file

void some_func(args){

    pthread_t th_1, th_2, th_3, th_4;

    void* func_to_use_in_threads(void *arg)
        {stuff in function}

// But as soon as I include pthread_create() in the code, Python ctypes won't load it

    pthread_create(&th_1,NULL,func_to_use_in_threads,&arg);
    pthread_create etc
    pthread join() etc

.........}

So anyway the error says some dependency is missing, but like I said when I write pthread standalone .exe's there is no problem.

I have tried many things when compiling at this point and always the same error when trying to load with ctypes. I don't know much about compiling and linking and am not even sure if this is the problem so I need help.

The basic compile code I've used is: gcc -fPIC -shared -o the_name.so the_name.c -lpthread

But I've gone so far as to copy all the files that pthread.h has listed as #include into a directory and type them in like this: gcc -fPIC -shared -o -the_name.so -the_name.c -B (directory) errno.h limits.h process.h pthread.h signal.h timeb.h types.h libwinpthread-1.dll

I don't really know what I'm doing with the compiling and linking like this, but it did compile, and ctypes still threw the same error.

The Python code is basically:

import ctypes

some_lib=ctypes.CDLL('file_path\\file_name.so') #won't load the file

I also did: ` import ctypes import os

some_lib=ctypes.CDLL(os.path.join('filepath/filename.so')) #same error`

Can anyone help?

  • 1
    You're on *Win* and using a *Nix* specific build toolchain. Most likely there are other *.dll*s (belonging to that toolchain - one that I can think of is *libpthread-win.so*) that your *.so* depends on. All of those must be loadable. Most likely, it's a duplicate of [\[SO\]: Why does adding multiprocessing prevent python from finding my compiled c program? (@CristiFati's answer)](https://stackoverflow.com/a/75035737/4788546). – CristiFati Mar 21 '23 at 19:09
  • Thanks! I used os.add_dll_directory like in the link you just mentioned, and set it to the files in mingw and it loaded. I had never heard of the omp library before. I think I may have come across that page when I had been looking for solutions but overlooked it because I was looking for pthreads and ctypes issues, but yeah it looks like it was the same thing going on. – undeadpresident Mar 23 '23 at 05:05
  • *OMP* is irrelevant. It's just the library that happened to be used in the other example. It's because of the *.dll* search mechanism that I indicated it. – CristiFati Mar 23 '23 at 06:46

0 Answers0