Embedded Python 3.3 problem...

Started by
1 comment, last by Vortez 9 years, 11 months ago

Hey, i just ran into a little snag with an app i've made that use python 3.3. The thing is, it work perfectly on a machine with python installed on it, but when i tried it on another machine, at first, it complained about Python33.dll. So, i found the file in the system32 folder and copied it in the app directory, but now, it crash, giving me the error 0x40000015. From what i could dig up, i though it was some missing msvc runtime dlls, so i tryed installing vs2012 redist, to no avail (i have vs2010 installed on my machine, so no need for those). I even tryed copying all files starting with msvcxxxx.dll i could find, didn't fixed it either. Im a bit lost. Do they really expect us to install python on every machine we want our app to run?

How the hell can i know what file is missing... I tryed dependency walker on the dev machine, didn't even detect any python dll... It seem i just can't find the problem. To be honest, it seem that previous python version where better(no @#$@ unicode everywhere, delphi/cpp builder did that too and i hate that), but i haven't tryed them.

What the heck with forcing us to use unicode everywhere??? It's really fun to convert from UnicodeString, AnsiString, string and C-String in a c++ builder app, really fun........

Most old python code in c++ don't work either because of that too, and it's really pissing me off. For example, today i typed the 'é' char in a save file dialog box, only to see it transformed to an '@'....... angry.pngangry.pngangry.png

Advertisement

Do they really expect us to install python on every machine we want our app to run?

No, one of the goal of embedding python is that you don't need to ask your user to install it on their system. Your executable is linked against the python library so you must distribute the python dll (and the microsoft c runtimes). That being said I've only worked with python 2.7 and do not know what dll you are missing in your specific case. You can always use the Windows Dependency Walker to find out the missing dlls.

Edit: What compiler did you use to build your game executable? It must be the same one as the one used to build python, wich is Visual Studio 2010 (if you downloaded the official python release)

I know this post is really old now, but i just wanted to add the solution i just found in case that could help someone in a similar situation. The problem was that python use the registry to know where to find it's library (you don't just need the dll you also need the python files), and for a machine with no python installed it search in directories relative to the application directory, so that's why it crashed/failed ect.

The solution i've found is to call PySys_SetPath() with the 3 required directory: "<python dir>/DLLs" "<python dir>/Lib" and "<python dir>/Lib/lib-tk"

<python dir> can be a directory or even a .zip file.

For example, if your program is in "C:\MyApp", the value to pass to PySys_SetPath() should be

"C:\MyApp\Python27\DLLs;C:\MyApp\Python27\Lib;C:\MyApp\Python27\Lib\lib-tk"

or

"C:\MyApp\Python27.zip\DLLs;C:\MyApp\Python27.zip\Lib;C:\MyApp\Python27.zip\Lib\lib-tk"

So, using the poorly written but working code, all works


char* ExtractFilePath(char *fullpath)
{
	static char Path[MAX_PATH];
	ZeroMemory(Path, MAX_PATH);

	int Pos = -1;
	int Size = strlen(fullpath);

	for(int i = Size-1; i >= 0; i--){
		if(fullpath[i] == '\\'){
			Pos = i;
			break;
		}
	}

	if(Pos > 0)
		memcpy(Path, fullpath, Pos); 

	return Path;
}

// Set paths to directories
/*void SetPythonPaths(char *AppDir)
{
	LPCSTR DllsDir = "\\Python27\\DLLs";
	LPCSTR LibDir = "\\Python27\\Lib";
	LPCSTR libtkDir = "\\Python27\\Lib\\lib-tk";

	int PathBufSize = (strlen(AppDir) * 3) + strlen(DllsDir) + strlen(LibDir) + strlen(libtkDir) + 3;
		
	char *PythonPaths = new char[PathBufSize];
	ZeroMemory(PythonPaths, PathBufSize);
	sprintf(PythonPaths, "%s%s;%s%s;%s%s", AppDir, DllsDir, AppDir, LibDir, AppDir, libtkDir);
	PySys_SetPath(PythonPaths);
	delete [] PythonPaths;
}*/

// Set paths to .zip file
void SetPythonPaths(char *AppDir)
{
	LPCSTR DllsDir = "/DLLs";
	LPCSTR LibDir = "/Lib";
	LPCSTR libtkDir = "/Lib/lib-tk";
	
	LPCSTR ZipName = "\\Python27.zip";

	int PathBufSize = (strlen(AppDir) * 3) + (strlen(ZipName) * 3) + strlen(DllsDir) + strlen(LibDir) + strlen(libtkDir) + 3;
		
	char *PythonPaths = new char[PathBufSize];
	ZeroMemory(PythonPaths, PathBufSize);
	sprintf(PythonPaths, "%s%s%s;%s%s%s;%s%s%s", AppDir, ZipName, DllsDir, AppDir, ZipName, LibDir, AppDir, ZipName, libtkDir);
	PySys_SetPath(PythonPaths);
	delete [] PythonPaths;
}

void CPythonScript::InitPython()
{
	if(!Initialized){

		Py_NoSiteFlag=1;
		Py_Initialize();
		module = PyImport_AddModule("__main__");
		dictionary = PyModule_GetDict(module);

		SetPythonPaths(ExtractFilePath(Py_GetProgramFullPath()));

		Initialized = true;
	}
}

Also, i had to add Py_NoSiteFlag=1; to the init function for some reason.

This topic is closed to new replies.

Advertisement