title: Ctypes: Could not find module ( Shared Library ) to use C-Code in Python
body: <p>I am currently working to get some intuition about interfacing <code>C-Code</code> to <code>Python</code>. For interfacing C-code to Python, I am using <a href="https://docs.python.org/2/library/ctypes.html" rel="nofollow noreferrer">ctypes</a> module and following these implementations such that:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/145649/10413749">Calling C/C++ from Python?</a> </li>
  <li><a href="https://www.geeksforgeeks.org/how-to-call-a-c-function-in-python/" rel="nofollow noreferrer">How to Call a C function in Python</a>.</li>
  </ul>
  
  <p>As we know that</p>
  
  <blockquote>
    <p>The ctypes module provides C compatible data types and functions to
    load DLLs so that calls can be made to C shared libraries without
    having to modify them.</p>
  </blockquote>
  
  <p>I have just implemented a simple function using <code>C</code> and generated " <em>Shared Library</em> " of the file using these commands on <a href="https://www.cygwin.com/" rel="nofollow noreferrer">Cygwin</a>:</p>
  
  <pre><code>For Linux, 
  - cc -fPIC -shared -o libfunc.so function.c
  - gcc -shared -Wl,-soname,adder -o adder.so -fPIC function.c
  
  On Windows, assuming that you have GCC installed: 
  1 ~ $ gcc -std=c11 -Wall -Wextra -pedantic -c -fPIC function.c -o libfunc.o
  2 ~ $ gcc -shared libfunc.o -o libfunc.dll
  </code></pre>
  
  <p>Then I placed all the files in the <em>same directory</em>. Following are given files:</p>
  
  <ul>
  <li>function.c </li>
  <li>libfunc.so </li>
  <li>Testing.py</li>
  </ul>
  
  <h2>function.c</h2>
  
  <pre><code>int func(int num) 
  {
      if (num == 0)
          return 0;
      else
          return 1;
  }
  </code></pre>
  
  <h2>Testing.py</h2>
  
  <pre><code>import os
  import ctypes
  from ctypes.util import find_library
  
  
  num = 16
  # To verify that Library is available in the specified path
  pathToWin32Environment = os.getcwd()
  pathToDll = pathToWin32Environment + "\\libfunc.so"
  if not os.path.exists(pathToDll):
      raise Exception('Could not locate ' + pathToDll)
  
  curr_dir_before = os.getcwd()
  os.chdir(pathToWin32Environment)
  
  # print("Available library:", find_library('libfunc.so'))
  
  
  Lib_func = ctypes.cdll.LoadLibrary("F:/PythonCodeBase/NetEQ/libfunc.so")
  # Lib_func = ctypes.CDLL(F:/PythonCodeBase/NetEQ/libfunc.so")
  # Lib_func = ctypes.windll.LoadLibrary("libfunc.so")
  
  Lib_func.func.argtypes(ctypes.c_int)
  ret_val = Lib_func.func(num)
  
  print(ret_val)
  </code></pre>
  
  <hr>
  
  <p>I have tried many times, either I'm giving the <code>full Specified path</code> or simple <code>(.so) filename</code>. Every time, I just got these types of errors.</p>
  
  <pre><code>- FileNotFoundError: Could not find module 'F:\PythonCodeBase\NetEQ\libfunc.so'. Try using the full path with constructor syntax.
  - FileNotFoundError: Could not find module 'libfunc.so'. Try using the full path with constructor syntax.
  </code></pre>
  
  <p>I have done multiple attempts using <code>PyCharm IDE</code> as well as <code>Command Prompt</code> but the same error appears. 
  Please assist me accordingly.</p>