Skip to main content
GameDev.net gamedev.net
🔒 Locked

Embedded Python and Packages

Started by PAndersson Apr 12, 2010 at 3:30 PM 3 replies 1.5k views
Original Post
PAndersson
PAndersson
Hey all, I'm working on a game and while the game engine is written in C I have planned to write most of the gameplay functionality in python. It works well, except that I'm a bit unsure on how to make python find any python packages I have written. I have no problem making it find the C extension it needs in order to interact with the game engine though. I have looked into several solutions such as PySys_SetPath() and Py_SetProgramName() but none of them seems to help in solving this problem. Py_SetProgramName() seems to be the function that is meant for this and I call it before intializing python, with the directory where the package can be found (and yes, I define the package with a __init__.py file) relative to the games path. Py_GetPath()'s return value seems to not be affected at all no matter what path I feed Py_SetPrgoramName(). Any help in explaining how to solve this would be much appreciated!
ninmonkeys
ninmonkeys
First, test with the python file in the same directory as your main exe.
Or is this working, that modules aren't?

Not totally sure on howto with C++ on top of it.
*But* if you're using python, and you have

the following file at:

c:/python/pandas/pandsLib/__init__.py

and system environment variable:
PYTHONPATH = "c:/python/pandas"

then any python file can now do:
import pandsLib# ... 
AndrewBC
AndrewBC
I don't know much of anything about python's C API, but I can show you how to do it from python easily enough, and I think that will help you out. You don't -have- to set environment variables, and if it's embedded then that may not be an option anyway.

import sysimport osmodule_dir = os.path.join('C:', 'path', 'to', 'module', 'directory')sys.path.append(module_dir)import module


Now provided that module is a module that lives in C:\path\to\module\directory, you should be good to go. You can even do some crazy stuff with dynamic imports using __import__, but that's only if you only know the module name at runtime.
PAndersson
PAndersson
Quote:
Original post by AndrewBC
I don't know much of anything about python's C API, but I can show you how to do it from python easily enough, and I think that will help you out. You don't -have- to set environment variables, and if it's embedded then that may not be an option anyway.

*** Source Snippet Removed ***

Now provided that module is a module that lives in C:\path\to\module\directory, you should be good to go. You can even do some crazy stuff with dynamic imports using __import__, but that's only if you only know the module name at runtime.


Thanks a lot, it seem to work well when I add the variable to sys.path. Though unsure if running a bit of python code in the C funtion that initialzes the C engine actually is a good way to do it.
PAndersson
PAndersson
Incase anyone wonders, I got it to work exactly as I want it too. Apparently the sys.path variable can look for paths relative to your application...
Here is the source C code used, works like a charm

	char pacPath[124]; //Path to python packages and modules	char modPath[5120]; //Module path	//Define path to script folder	strcpy(pacPath, "Data\\Scripts");	//Append module path to list of all python module paths	strcpy(modPath, Py_GetPath());	strcat(modPath, ";");	strcat(modPath, pacPath);	Py_Initialize();	//Update module path	PySys_SetPath(modPath);

Topic Locked

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

Sign in to reply to this topic.