简体   繁体   中英

Loading DLL from embedded python code in c

The crux of my problem is this:

I am developing code on Windows XP in C with MS Visual Studio 10.0, and I need to embed Python to do some plotting, file management, and some other things. I had problems with sys.path finding my Pure-Python modules, but I have fixed the problem by modifying PYTHONPATH.

Now, my problem is getting python to find dynamic libraries that are pulled in by some modules. In particular, my problem is to compress a folder into a bzip2 achive of the same name.

From a normal python command prompt, this works just 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 the problem is similar to what is described at section 5.6 of Embedded Python , but it is a bit hard to tell. For what its worth, if I do

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

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

Anyone had any problems like this? Am I missing something critical?

Try this, it works on my machine.

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

#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 set to 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 (adjust with your Python version)

Build. Run from anywhere and you should see a listing of the content of the ssl module.

I also tried with Mingw. Same file, build with this command line :

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

Hey, I have asked a similar question , my operation system is Linux .

When i compile c file, option $(python-config --cflags --ldflags) should be added, as

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

I think in Windows you may also check python-config option, Hope this helps!

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

In my experience, PYTHONPATH affects Python module (the import statement in Python will end up a LoadLibrary call), and 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 DLL loading rules as Notepad. You can confirm that you are facing a Windows path problem by copying any missing DLL in the same directory as your python extension, or to a directory in your path.

To find what DLL are required by any other executable or DLL, simply open the DLL or EXE file with DependencyWalker . There is also a "Profile" menu which will allow you to run your application and watch it search and load DLLs.

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