Python Forum
[CPython] Can't run script python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [CPython] Can't run script python (/thread-37269.html)



[CPython] Can't run script python - VietVH - May-21-2022

Hi all,

I have a problem need your help.
Thanks in advance.

1. Context:
I customized source code CPython 3.9.6 (https://www.python.org/downloads/release/python-396/)
In detailed, instead of reading directories from config, i fixed some directories sys._base_executable = '/', sys.base_prefix = '/mfp', sys.base_exec_prefix = '/mfpsys',...

2. Problem
I run into a problem when run script Python. I got the following log
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = '/'
sys.base_prefix = '/mfpsys'
sys.base_exec_prefix = '/mfpsys'
sys.platlibdir = 'lib'
sys.executable = '/'
sys.prefix = '/mfpsys'
sys.exec_prefix = '/mfpsys'
sys.path = [
'/site-packages',
'/mfpsys/lib/python3.9',
'/mfpsys/lib/python3.9/lib-dynload',
'/mfpsys/lib/python3.9/site-packages',
]
LookupError: no codec search functions registered: can't find encoding

I have investigated in source code Python 3.9.6, found the related source code show above error log.

\Python-3.9\Python\codecs.c
PyObject* _PyCodec_Lookup(const char* encoding)
{
...
const Py_ssize_t len = PyList_Size(interp->codec_search_path);

if (len == 0) {
PyErr_SetString(PyExc_LookupError,
"no codec search functions registered: "
"can't find encoding");
goto onError;
}
...
}

I have no idea to this problem. Please give me your kind suggestion.


RE: [CPython] Can't run script python - Gribouillis - May-21-2022

Clearly the list interp->codec_search_path is empty and the code expected this list to have been populated by some other part of the code. I suggest to grep codec_search_path in the source tree to find where it should be populated.

Apart from that, the value '/' for sys.executable looks invalid because this variable normally contains the path to the python command.


RE: [CPython] Can't run script python - VietVH - May-21-2022

(May-21-2022, 04:21 PM)Gribouillis Wrote: Clearly the list interp->codec_search_path is empty and the code expected this list to have been populated by some other part of the code. I suggest to grep codec_search_path in the source tree to find where it should be populated.

Apart from that, the value '/' for sys.executable looks invalid because this variable normally contains the path to the python command.

Thanks for your suggestion.

I have found the following logic to append value to codec_search_path.
int PyCodec_Register(PyObject *search_function)
{
...
return PyList_Append(interp->codec_search_path, search_function); // <==== Set breakpoint
...
}

Tried setting breakpoint here, but it does not hit. It seems there is something wrong causes logic of append is not called.
I'm working on it.