gpt4 book ai didi

python - 从不同的目录调用多个 python 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:29 25 4
gpt4 key购买 nike

我有一些代码将转到一个目录(用于演示目的的文件夹 1),然后调用文件 python_function.py 中名为 function 的函数。代码如下所示:

#include <Python.h>
#include <string>
#include <iostream>

int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;

setenv("PYTHONDONTWRITEBYTECODE", " ", 1);

// Initialize the Python Interpreter
Py_Initialize();

//CALL FUNCTION FROM FOLDER 1:
std::wstring pathWide = L"./Folder 1";
PySys_SetPath(pathWide.c_str());

// Build the name object
pName = PyUnicode_FromString((char*)"python_function");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, (char*)"function");

if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;

pResult = PyObject_CallFunction(pFunc, "");

Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}

// Clean up
Py_DECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return 0;
}

此代码在我的系统上编译并完美运行,但是当我想调用第二个目录(称为文件夹 2)中的另一个函数时,我收到错误:Segmentation Fault (core dumped) .这是代码:

#include <Python.h>
#include <string>
#include <iostream>

int main()
{
PyObject *pName, *pModule, *pDict, *pFunc;

setenv("PYTHONDONTWRITEBYTECODE", " ", 1);

// Initialize the Python Interpreter
Py_Initialize();

//CALL FUNCTION FROM FOLDER 1:
std::wstring pathWide = L"./Folder 1";
PySys_SetPath(pathWide.c_str());

// Build the name object
pName = PyUnicode_FromString((char*)"python_function");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, (char*)"function");

if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;

pResult = PyObject_CallFunction(pFunc, "");

Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}

//CALL FUNCTION FROM FOLDER 2:
pathWide = L"./Folder 2";
PySys_SetPath(pathWide.c_str());

// Build the name object
pName = PyUnicode_FromString((char*)"python_function");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, (char*)"function");

if (pFunc != NULL)
{
if (PyCallable_Check(pFunc))
{
PyObject *pResult;

pResult = PyObject_CallFunction(pFunc, "");

Py_DECREF(pResult);
}
else {PyErr_Print();}
}
else {std::cout << "pFunc is NULL!" << std::endl;}

// Clean up
Py_DECREF(pFunc);
Py_DECREF(pDict);
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Finalize();

return 0;
}

错误发生在我调用第一个函数之后,所以它似乎没有改变目录或其他东西。我正在使用 Ubuntu,我有 python 3.4

我尝试了其他改变目录的方法,不仅仅是PySys_SetPath,还有setenv("PYTHONPATH", path, 1);

注意:我现在不担心错误检测,我宁愿拥有在理想情况下工作的代码,然后担心不完美的情况。

编辑:

调试输出:

#0 0x7ffff79b16cb   PyModule_GetDict() (/usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0:??)
#1 0x4010e6 main() (/home/ben/Documents/Programming/Projects/PYTHON TEST/main.cpp:23)

奇怪的是,调试说错误发生在第 23 行,但如果您运行第一个代码段,第 23 行不会导致错误

对 PETER BRITTAIN 的回答的回应:

如果我将第二个 PyImport_Import() 替换为 PyImport_ReloadModule(),我会在控制台打印一条错误消息,如下所示:

ImportError: No module named 'imp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
if not enabled():
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
import re
ImportError: No module named 're'

Original exception was:
ImportError: No module named 'imp'

最佳答案

编辑:更新了对发现的错误的进一步回答。

您未能在调试输出中导入您的模块。当在调试器之外运行时,您会遇到无法仅使用相同导入调用导入的问题。完整的问题链是这样的。

调试时:

  1. 您的调试器没有设置正确的当前工作目录。
  2. 相对路径无效,但被 PySys_SetPath() 接受。
  3. 因此,您从 PyImport_Import() 得到 NULL,表示导入失败(如调试器下的 here 所记录。
  4. 由于您没有检查错误,您将 NULL 传递给下一个函数,该函数尝试取消引用指针并因段错误而失败。

我遇到了 Python2.7 的问题(使用 char* 而不是 wchar* - 如下面的评论所述)。把它放在一边,当正常运行时:

  1. 相对路径有效并被 PySys_SetPath() 接受。
  2. 因此您设法在第一次加载模块。
  3. 然后您运行其余代码,但这次它在第二次导入时出现段错误。这是因为您不能以这种方式重新加载模块。您需要改用 PyImport_ReloadModule()
  4. 即使进行了该修复,您仍会发现无法加载标准库。那是因为您已经删除了系统路径的其余部分。您需要遵循建议 here .

所以,修复是:

  1. 使用 std::string 代替 std::wstring(对于 Python 2.x)。
  2. 在重新加载模块时使用 PyImport_ReloadModule()
  3. 确保在设置路径时包含完整的 sys.path。
  4. 检查您的错误 - 对于大多数问题,我使用 PyErr_Print() 得到了明确的模块导入错误。

关于python - 从不同的目录调用多个 python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com