Jump to content
  • Advertisement
Sign in to follow this  
PAndersson

Embedded Python and Packages

This topic is 3085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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
# ...

Share this post


Link to post
Share on other sites
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 sys
import os

module_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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!