embedding wxPython in VisualStudio 2005 C/C++ fails - python

When I try to embed python wx in C like this:
#include "stdafx.h"
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
int _tmain(int argc, _TCHAR* argv[])
{
PyObject* pyModule;
Py_Initialize();
//_pyModule = PyImport_ImportModule("__main__");
//_pyModule = PyImport_ImportModule("csi");
pyModule = PyImport_ImportModule("wx");
if(!pyModule)
PyErr_Print();
return 0;
}
it fails:
->Traceback (most recent call last):
File "C:\Python27\lib\site-packages\wx-2.8-msw-ansi\wx\__init__.py", line 45, in <module>
from wx._core import *
File "C:\Python27\lib\site-packages\wx-2.8-msw-ansi\wx\_core.py", line 4, in <module>
import _core_
ImportError: DLL load failed: The specified module could not be found.
could be a problem with msvc*90.dll files?
I am using VS2005, Python 2.7, wxPython 2.8.12.1 (ansi) for Python 2.7
Further MS VC++ 2008/2010 Redistributables are installed.
thanks for help.

Finally I found the solution:
it is a Manifest problem. Thanks to Microsof dll-hell...
This helps:
//_core_.pyd ImportError
// _core_.pyd is in fact a dll. it can be renamed.
// The problem is that _core_.pyd depends on some Microsoft.VC90.CRT libraries
//this dependency must be added to make it work
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' language='*'\"")

Related

cannot run Py_Initialize(), application exited with code -1073741191

I'm currently testing around with embedding Python in a C application, however I couldn't get it to work at all. I can build it just fine, but running it returns no error, only the Application error with the exit status of -1073741191.
Here's my code:
#define PY_SSIZE_T_CLEAN
#include <stdlib.h>
#include <stdio.h>
#ifdef _DEBUG
#undef _DEBUG
#include <C:\Users\name\anaconda3\include\Python.h>
#define _DEBUG
#else
#include <C:\Users\name\anaconda3\include\Python.h>
#endif
int
main(int argc, char* argv[])
{
printf("dummy");
Py_Initialize();
Py_Finalize();
return 0;
}
And in the linker, additional library directories: I put a path to python38.lib. I searched online and the exit code status has no meaning, and there's nothing that indicates what go wrong, only the application error after the apparent successful build.
Solved it, I just haven't included the python .dll file inside my project

C++ with Python No Module Named Encodings

I'm currently trying to run this simple program:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define PY_SSIZE_T_CLEAN
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
int main()
{
Py_Initialize();
Py_FinalizeEx();
return 0;
}
But I get this error message:
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
I looked through many other posts online and most of them say to set the PYTHONPATH environment variable, which I did. I'm using Windows 10, Anaconda, and C++ Visual Studio.
I set the PATH variable on my computer to "C:/Anaconda/envs/myEnv" and "C:/Anaconda/envs/myEnv/Scripts".
Despite that, I still get the error.
Is there anything I'm missing? Can I try setting the PYTHONPATH variable at runtime?
I've also tried doing this:
_putenv_s("PYTHONPATH", "C:/Anaconda/envs/myEnv");

Calling C DLL function using ctypes is not working

I'm trying to call a C dll I created in Python within Visual Studio on Windows.
Here's the header file,
#pragma once
#ifdef MYCLIB_EXPORT
#define MYCLIB_API extern "C" __declspec(dllexport)
#else
#define MYCLIB_API extern "C" __declspec(dllimport)
#endif
MYCLIB_API int PrintStuff();
And the source file:
#include "stdafx.h"
#include "MyCLib.h"
#include <stdio.h>
MYCLIB_API int PrintStuff()
{
printf("Stuff");
return 1;
}
I'm building 64-bit debug, I have MYCLIB_EXPORT defined in the preprocessor property. I also placed the MyCLib.dll in the directory of the Python script.
I created a Python project in the same Visual Studio solution, and have the following code:
import ctypes
from ctypes import *
lib = ctypes.WinDLL('MyCLib.dll')
#dll = cdll.LoadLibrary('MyCLib.dll')
print "Done"
When I run the script, I get the error: [Error 193] %1 is not a valid Win32 application.
Did I make a mistake in the dll creation or the dll loading?

Error of embedding Python in C++ : unable to read memory

I'm trying to embed Python in VS C++ but I've encountered this error : unable to read memory. The C++ code is
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pName = PyBytes_FromString("Test001");
//pName = PyBytes_FromString(argv[1]);
pModule = PyImport_Import(pName);
The pName contains attributes which are "unable to read memory", both for the line commented. So, the PyImport_Import doesn't work as a result, pModule is NULL.
I've set PYTHONPATH in the environment variable and set argv[1] to Test001. The file Test001.py is in the same folder as the .exe. I could not see the problem, I think there are some problems of configuration.
Thanks for help in advance!
I have tried your code with success using Visual Studio 2013 and Python 2.7.
The only issue I had was with the link in debug which required a python27_d.lib and i had to solve the issue using :
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
You should also check which version of python you are linking against.

How can I embed python in a bcc64 project?

I am porting a project from C++ Embarcadero XE6 32 bit to XE6 64 bit. We use embedded python libraries (and also some Python modules written in C).
I was able to get it to compile, but I can not link because there aren't any libs in the Python directory that the compiler bcc64 (a clang based compiler) can use.
Here's my code, it's just the simple "Here's how to embed python" example from the documentation really. We use Python 3.1; we could upgrade but I don't see the libs in newer versions either.
#define HAVE_ROUND
#define HAVE_ACOSH
#define HAVE_ASINH
#define HAVE_ATANH
#include <Python.h>
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Invoke the python intepreter
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
system("Pause");
return 0;
}
mkexp libpython31.a C:\Windows\System32\python31.dll
Thanks to Ignacio for the help.

Categories