Skip to main content
gamedev.net
🔒 Locked

Embedding Python Help

Started by Squirell Mar 24, 2010 at 10:00 PM 1 replies 1.1k views
Original Post
Squirell
Squirell
So I'm trying to embed python in a C++ application I'm writing. I'm trying to change python's path so it will find some modules I wrote but I can't seem to change the path. The code is here:

#include <Python.h>
#include <string>

int main(int argc, char *argv[]) {
	Py_Initialize();
	
	const char* oldPath = Py_GetPath();
	
	char* newPath = new char[strlen(oldPath)+256];
	strcpy(newPath, oldPath);
	strcat(newPath, ":../Resources");

	printf("Path: %s\n", newPath);

	PySys_SetPath(newPath);
	printf("Path: %s\n", Py_GetPath());
	
	delete newPath;
		
	Py_Finalize();
	return 0;
}



The code just grabs the old path and then concatenates the path I want to add, in this case ":../Resources". When I print the variable 'newPath' it shows my addition. However when I check with Py_GetPath(), it shows the old path without my addition. I have no clue why not, does anybody have any ideas? I've included the output of the program below for reference.

Path: /Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload:
../Resources
Path: /Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload



SiCrane
SiCrane
Py_GetPath() doesn't return the current value of sys.path, it returns the value that sys.path was initialized with. Later modifications to sys.path aren't reflected in Py_GetPath().
Squirell
Squirell
Well that would make sense then, a lot of headache over nothing then. For anyone who is curious, you can use PySys_GetObject("path") to find the current value. Note it returns a list not a string.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.