Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void importModule(const std::string& moduleName) {
- const static std::string __key__ = "__";
- char* key = new char[moduleName.size() + 5];
- sprintf(key, "__%s__", moduleName.c_str());
- if (PyDict_GetItemString(mainDict_, key) == nullptr) {
- PyObject* pMod = PyImport_ImportModule(moduleName.c_str());
- if (nullptr != pMod) {
- PyDict_SetItemString(mainDict_, key, pMod);
- } else {
- LogWarn("Failed to import python module: " << moduleName);
- }
- }
- delete[] key;
- }
- void initPython(){
- wchar_t programName[] = L"PyInviwo";
- Py_SetProgramName(programName);
- #ifdef WIN32
- Py_NoSiteFlag = 1;
- #endif
- Py_InitializeEx(false);
- if (!Py_IsInitialized()) {
- LogError("Python is not Initialized");
- return;
- }
- PyEval_InitThreads();
- mainDict_ = PyDict_New();
- modulesDict_ = PyImport_GetModuleDict();
- importModule("builtins");
- importModule("sys");
- //importModule("os");
- //importModule("glob");
- //importModule("random");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement