Loading DLL from python inline code in c

The essence of my problem is this:

I am developing code in Windows XP in C with MS Visual Studio 10.0, and I need to embed Python to plot, manage files and some other things. I had problems with sys.path, which found my Pure-Python modules, but I fixed the problem by changing PYTHONPATH.

Now my problem is getting python to search for dynamic libraries that are pulled by some modules. In particular, my problem is to compress the folder into one bzip2 name with the same name.

From a regular python command line, this works fine:

import tarfile tar=tarfile.open('Code.tar.bz2','w:bz2') tar.add('Code',arcname='Code') tar.close() 

But when I call this code from my c-code, it gives me this error:

 Traceback (most recent call last): File "<string>", line 4, in <module> File "D:\My_Documents\Code\ScrollModel\trunk\PythonCode.py", line 20, in Colle ctFiles tar=tarfile.open(os.path.join(runPath,'CODE.tar.bz2'),'w:bz2') File "c:\Python26\lib\tarfile.py", line 1671, in open return func(name, filemode, fileobj, **kwargs) File "c:\Python26\lib\tarfile.py", line 1737, in bz2open raise CompressionError("bz2 module is not available") tarfile.CompressionError: bz2 module is not available 

I have a suspicion that the problem is similar to that described in section 5.6 Embedded Python , but it is a little difficult to say. What is it worth if I do

 Py_Initialize(); PyRun_SimpleString("import ssl\n"); Py_Finalize(); 

it doesn't work either, and I get ImportError.

Has anyone had a problem? Did I miss something important?

+4
source share
3 answers

Try it, it works on my car.

Create a simple Windows console application in Visual Studio 2010 (remove the precompiled headers option in the wizard). Replace the generated code with this:

 #include <Python.h> int main(int argc, char *argv[]) { Py_Initialize(); PyRun_SimpleString("import ssl \n" "for f in dir(ssl):\n" " print f \n" ); Py_Finalize(); return 0; } 

With PYTHONHOME, install something like c: \ Python ...

  • add C: \ Python \ Include to the include path
  • add C: \ Python \ Libs to the library path
  • add python26.lib to the linker input (customize using your version of Python)

Build. Run from anywhere and you should see a list of the contents of the ssl module.

I also tried with Minho. The same file created using this command line:

 gcc -Wall -o test.exe embeed.c -I%PYTHONHOME%\Include -L%PYTHONHOME%\libs -lpython26 
+2
source

Hey, I asked a similar question , my Linux operating system.

When I compile the c file, the $(python-config --cflags --ldflags) option is added $(python-config --cflags --ldflags) since

gcc test.c $(python-config --cflags --ldflags) -o test

I think on Windows you can also check the python-config option, hope this helps!

0
source

I had a similar problem with Boost C ++ DLL. Any external DLL must be in the DLL search path.

In my experience, PYTHONPATH affects the Python module (the import statement in Python completes the LoadLibrary call), and the build options have nothing to do with it.

When you load a DLL, Windows doesn't care what the process is. In other words, Python follows the same rules for loading a DLL as Notepad. You can confirm that you have encountered a Windows path problem by copying any missing DLL in the same directory as the python extension, or in the directory of your path.

To find out which DLLs are needed for any other executable file or DLL, just open the DLL or EXE file with DependencyWalker . There is also a Profile menu that allows you to launch your application and monitor its search and loading of the DLL.

0
source

Source: https://habr.com/ru/post/1347183/


All Articles