[C++-sig] use boost.python to append my own script directory to sys.path, but failed!

千里马肝 tlovexyj at 21cn.com
Mon Apr 12 11:59:23 CEST 2004


hello!

	I write code to instead of PyRun_SimpleString("import sys\nsys.path.append(...)"), like this:

		_main_module = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
		_main_namespace = dict(_main_module.attr("__dict__"));

		_sys_module = object(handle<>(PyImport_ImportModuleEx("sys", _main_namespace.ptr(), _main_namespace.ptr(), NULL)));
		_sys_namespace = dict(_sys_module.attr("__dict__"));

		list sysPath(_sys_namespace["path"]);
		sysPath.insert(0, script_directory);	<--- add the script directory
		_sys_namespace["path"] = sysPath;		<--- is the sysPath is temporary value?!?!

	follow I want to import a custom module in the script directory, but it failed:
		custom_module = object(handle<>(PyImport_ImportModuleEx("a", _main_namespace.ptr(), _main_namespace.ptr(), NULL)));

	What can be the problem ?!?! thanks for reply!





More information about the Cplusplus-sig mailing list