Mailing List Archive

Help on ImportError('Error: Reinit is forbidden')
Hi,

I Need some of your help.

I have the following C code to import *Import python.* It works 99% of
the time, but sometimes receives "*ImportError('Error: Reinit is
forbidden')*". error.
**We run multiple instances of the app parallelly.

*** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC
v.1914 64 bit (AMD64)]

PyObject * importPythonModule(const char* pmodName)
{
const char* errors = NULL;
int nlen = strlen(pmodName);
PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
PyObject *pModule = *PyImport_Import*(pName);
Py_DECREF(pName);
if (pModule == NULL) {
if (*PyErr_Occurred*()) {
handleError("PyImport_Import()");
}
}
}
void handleError(const char* msg)
{
...
"PyImport_Import() - ImportError('Error: Reinit is forbidden')"
}


Thanks
Jason
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help on ImportError('Error: Reinit is forbidden') [ In reply to ]
> On 17 May 2023, at 20:35, Jason Qian via Python-list <python-list@python.org> wrote:
>
> ? Hi,
>
> I Need some of your help.
>
> I have the following C code to import *Import python.* It works 99% of
> the time, but sometimes receives "*ImportError('Error: Reinit is
> forbidden')*". error.
> **We run multiple instances of the app parallelly.
>
> *** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC
> v.1914 64 bit (AMD64)]
>
> PyObject * importPythonModule(const char* pmodName)
> {
> const char* errors = NULL;
> int nlen = strlen(pmodName);
> PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
> PyObject *pModule = *PyImport_Import*(pName);
> Py_DECREF(pName);
> if (pModule == NULL) {
> if (*PyErr_Occurred*()) {
> handleError("PyImport_Import()");
> }
> }
> }
> void handleError(const char* msg)
> {
> ...
> "PyImport_Import() - ImportError('Error: Reinit is forbidden')"
> }

You do not seem to printing out msg, you have assumed it means reinit it seems.
What does msg contain when it fails?

Barry
>
>
> Thanks
> Jason
> --
> https://mail.python.org/mailman/listinfo/python-list
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: Help on ImportError('Error: Reinit is forbidden') [ In reply to ]
Hi Barry,

void handleError(const char* msg)
{
...
PyErr_Fetch(&pyExcType, &pyExcValue, &pyExcTraceback);
PyErr_NormalizeException(&pyExcType, &pyExcValue, &pyExcTraceback);

PyObject* str_value = PyObject_Repr(pyExcValue);
PyObject* pyExcValueStr = PyUnicode_AsEncodedString(str_value, "utf-8",
"Error ~");
const char **strErrValue* = PyBytes_AS_STRING(pyExcValueStr);

//where *strErrValue* = "ImportError('Error: Reinit is forbidden')"
...
}

What we imported is a Python file which import some pyd libraries.


Thanks
Jason


On Thu, May 18, 2023 at 3:53?AM Barry <barry@barrys-emacs.org> wrote:

>
>
> > On 17 May 2023, at 20:35, Jason Qian via Python-list <
> python-list@python.org> wrote:
> >
> > ? Hi,
> >
> > I Need some of your help.
> >
> > I have the following C code to import *Import python.* It works 99% of
> > the time, but sometimes receives "*ImportError('Error: Reinit is
> > forbidden')*". error.
> > **We run multiple instances of the app parallelly.
> >
> > *** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC
> > v.1914 64 bit (AMD64)]
> >
> > PyObject * importPythonModule(const char* pmodName)
> > {
> > const char* errors = NULL;
> > int nlen = strlen(pmodName);
> > PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
> > PyObject *pModule = *PyImport_Import*(pName);
> > Py_DECREF(pName);
> > if (pModule == NULL) {
> > if (*PyErr_Occurred*()) {
> > handleError("PyImport_Import()");
> > }
> > }
> > }
> > void handleError(const char* msg)
> > {
> > ...
> > "PyImport_Import() - ImportError('Error: Reinit is forbidden')"
> > }
>
> You do not seem to printing out msg, you have assumed it means reinit it
> seems.
> What does msg contain when it fails?
>
> Barry
> >
> >
> > Thanks
> > Jason
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
>
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help on ImportError('Error: Reinit is forbidden') [ In reply to ]
On 18 May 2023, at 13:56, Jason Qian <jqian@tibco.com> wrote:

?
Hi Barry,
void handleError(const char* msg)
{
...
PyErr_Fetch(&pyExcType, &pyExcValue, &pyExcTraceback);
PyErr_NormalizeException(&pyExcType, &pyExcValue, &pyExcTraceback);

PyObject* str_value = PyObject_Repr(pyExcValue);
PyObject* pyExcValueStr = PyUnicode_AsEncodedString(str_value, "utf-8",
"Error ~");
const char *strErrValue = PyBytes_AS_STRING(pyExcValueStr);
//where   strErrValue   = "ImportError('Error: Reinit is forbidden')"
...
}
What we imported is a Python file which import some pyd libraries.

Please do not top post replies.
Ok so assume the error is correct and hunt for the code that does the
reimport.
You may need to set break points in you C code to find tnw caller.
Barry

Thanks 
Jason 
On Thu, May 18, 2023 at 3:53?AM Barry <[1]barry@barrys-emacs.org> wrote:

> On 17 May 2023, at 20:35, Jason Qian via Python-list
<[2]python-list@python.org> wrote:
>
> ? Hi,
>
>   I Need some of your help.
>
> I have the following C code to import *Import python.*   It works
99% of
> the time, but sometimes  receives  "*ImportError('Error: Reinit is
> forbidden')*". error.
> **We run multiple instances of the app parallelly.
>
> *** Python version(3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51)
[MSC
> v.1914 64 bit (AMD64)]
>
> PyObject * importPythonModule(const char* pmodName)
> {
>    const char* errors = NULL;
>     int nlen = strlen(pmodName);
>     PyObject *pName = PyUnicode_DecodeUTF8(pmodName, nlen, errors);
>     PyObject *pModule = *PyImport_Import*(pName);
>     Py_DECREF(pName);
>     if (pModule == NULL) {
>     if (*PyErr_Occurred*()) {
>            handleError("PyImport_Import()");
>      }
>   }
> }
> void handleError(const char* msg)
> {
>  ...
>  "PyImport_Import() - ImportError('Error: Reinit is forbidden')"
> }

You do not seem to printing out msg, you have assumed it means reinit
it seems.
What does msg contain when it fails?

Barry
>
>
> Thanks
> Jason
> --
> [3]https://mail.python.org/mailman/listinfo/python-list
>

References

Visible links
1. mailto:barry@barrys-emacs.org
2. mailto:python-list@python.org
3. https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list