   Load a module with a full or relative path(Boost Python)
  I'm using python to store user data and was wondering how to load a file(configs.py) with a full or relative path? (Path is decided by user and can change)Here is code I use to load data when it's in the same folder as program.PyRun_SimpleString("import cStringIO");PyRun_SimpleString("import sys");PyRun_SimpleString("sys.dont_write_bytecode = True");PyRun_SimpleString("sys.stderr = cStringIO.StringIO()");object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));object main_namespace = main_module.attr("__dict__");PyImport_AppendInittab("Configs", &initConfigs );object data = import("configs");object o_getdata = extract<object>(data.attr("getdata"));o_getdata();class CPythonConfigs{public:void Load(int a, int b, int c){}};BOOST_PYTHON_MODULE(Configs){class_<CPythonConfigs>("CPythonConfigs").def("Load", &CPythonConfigs::Load);}import Configsdef getdata():con = Configs.CPythonConfigs()con.Load(100, 150, 461)return
  The easiest method for importing a file from a given directory is to prepend the directory to sys.path and then importing the file name. That said, using actual python code for a configuration file is generally a bad idea. Consider using an actual configuration file via the ConfigParser library module. Alternately, there are a lot of different third party config file parsers available for python (example).   ;Quote:Original post by SiCraneThe easiest method for importing a file from a given directory is to prepend the directory to sys.path and then importing the file name. How about this instead:object main_module((handle&lt;>(borrowed(PyImport_AddModule("__main__")))));object main = main_module.attr("__dict__");dict locals;locals["modulename"] = "configs"; locals["path"]   = "C:\\Users\\Joe\\Desktop\\configs.py"; exec("import imp\n""newmodule = imp.load_module(modulename,open(path),path,('py','U',imp.PY_SOURCE))\n", main,locals);object data =  locals["newmodule"];object o_getdata = extract&lt;object&gt;(data.attr("getdata"));o_getdata();Could this lead to any problems?Quote:Original post by SiCraneThat said, using actual python code for a configuration file is generally a bad idea.If you don't mind, why is it a bad idea?Thx ahead of time, Jacob Jingle   ;  Loading code as a config file can be a bad idea if you don't trust your users, since that code could do anything when you load it. Basically it gives your users an obvious entry point to start mucking with your program's execution. My game is open-source, so I don't really have to worry about that kind of thing -- if my users want to muck with things, then they can do so freely. There's still a certain amount of worry that someone could make a malicious plugin that messes with the player's files when imported, but I currently judge that risk pretty low (and I'm confident it can be dealt with). I put all of my configuration into Python dicts that can be serialized using the pretty-printer, making human-readable config files that can be trivially loaded and printed by the program. In addition to mucking with your path, you can also import a module from anywhere using Python's __import__ function, which accepts a path to a module to load, and optionally a list of symbol names to load from that module.   ;Quote:Original post by DerakonLoading code as a config file can be a bad idea if you don't trust your users, since that code could do anything when you load it. Basically it gives your users an obvious entry point to start mucking with your program's execution. IMHO, if bad people are going to do bad things then there are thousands of ways they could do it. From changing your program to your os you can't stop them, no matter what you do. Backup your data, use virus scan, etc.Oh wait, you can bludgeon them to death after they do it and bury them out in the woods. I'm pretty sure that's legal. [smile];  Well, what if you host game mods on your website, and someone sneaks in a mod that does nasty things to your legitimate players? It's not just the person playing the game you have to worry about. Anyway, this isn't necessarily relevant to your specific case, but it pays to be aware of vulnerabilities, regardless of whether or not they're worth addressing.   ;Quote:Original post by DerakonWell, what if you host game mods on your website, and someone sneaks in a mod that does nasty things to your legitimate players? It's not just the person playing the game you have to worry about. Anyway, this isn't necessarily relevant to your specific case, but it pays to be aware of vulnerabilities, regardless of whether or not they're worth addressing.I'm not doing anything that complex, but you have made me think twice and I'm going to add some code(c++) to check my file for tampering. Thx for the advice.