Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python segmentation fault in interactive mode

The python is installed with conda:

(base) [kangl@login05]~% which python
~/miniconda3/bin/python

When directly run python in iteractive mode, a segmentation fault will apear:

(base) [kangl@login05]~% python
Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
zsh: segmentation fault  python

However, the python script runs successfully:

(base) [kangl@login05]~% python -c "print('hello world')"
hello world

And ipython runs successfully:

(base) [kangl@login05]~% ipython
Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

Then I try to debug it with gdb:

(base) [kangl@login04]~% gdb python
GNU gdb (GDB) 11.2
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-conda-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...
(gdb) run
Starting program: /users/kangl/miniconda3/bin/python
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaab96b691 in __strlen_sse2_pminub () from /lib64/libc.so.6

And

(gdb) run
Starting program: /users/kangl/miniconda3/bin/python
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Python 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Program received signal SIGSEGV, Segmentation fault.
0x00002aaaab96b691 in __strlen_sse2_pminub () from /lib64/libc.so.6
(gdb) backtrace
#0  0x00002aaaab96b691 in __strlen_sse2_pminub () from /lib64/libc.so.6
#1  0x00002aaaaac53f49 in _rl_init_locale ()
   from /users/kangl/miniconda3/lib/python3.10/lib-dynload/../../libreadline.so.8
#2  0x00002aaaaac54044 in _rl_init_eightbit ()
   from /users/kangl/miniconda3/lib/python3.10/lib-dynload/../../libreadline.so.8
#3  0x00002aaaaac32797 in rl_initialize ()
   from /users/kangl/miniconda3/lib/python3.10/lib-dynload/../../libreadline.so.8
#4  0x00002aaaaaad4a7d in setup_readline (mod_state=0x2aaab21b02a0)
    at /usr/local/src/conda/python-3.10.10/Modules/readline.c:1289
#5  PyInit_readline () at /usr/local/src/conda/python-3.10.10/Modules/readline.c:1502
#6  0x000055555576939a in _PyImport_LoadDynamicModuleWithSpec (fp=0x0,
    spec=<ModuleSpec(name='readline', loader=<ExtensionFileLoader(name='readline', path='/users/kangl/miniconda3/lib/python3.10/lib-dynload/readline.cpython-310-x86_64-linux-gnu.so') at remote 0x2aaab21b09a0>, origin='/users/kangl/miniconda3/lib/python3.10/lib-dynload/readline.cpython-310-x86_64-linux-gnu.so', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached=None) at remote 0x2aaab21b02e0>)
    at /usr/local/src/conda/python-3.10.10/Python/importdl.c:167
#7  _imp_create_dynamic_impl (module=<optimized out>, file=<optimized out>,
    spec=<ModuleSpec(name='readline', loader=<ExtensionFileLoader(name='readline', path='/users/kangl/miniconda3/lib/python3.10/lib-dynload/readline.cpython-310-x86_64-linux-gnu.so') at remote 0x2aaab21b09a0>, origin='/users/kangl/miniconda3/lib/python3.10/lib-dynload/readline.cpython-310-x86_64-linux-gnu.so', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached=None) at remote 0x2aaab21b02e0>)
    at /usr/local/src/conda/python-3.10.10/Python/import.c:2050
#8  _imp_create_dynamic (module=<optimized out>, args=args@entry=0x2aaab212e998, nargs=<optimized out>)
    at /usr/local/src/conda/python-3.10.10/Python/clinic/import.c.h:330
#9  0x0000555555694d14 in cfunction_vectorcall_FASTCALL (
    func=<built-in method create_dynamic of module object at remote 0x2aaaaab8e610>, args=0x2aaab212e998,
    nargsf=<optimized out>, kwnames=<optimized out>) at /usr/local/src/conda/python-3.10.10/Objects/methodobject.c:430
#10 0x000055555568a39b in do_call_core (kwdict={},
--Type <RET> for more, q to quit, c to continue without paging--

It looks the dependency on readline library is broken as Max said. But I have no idea how to fix it. I have tried to reinstall the readline with mamba install -c conda-forge readline --force-reinstall but it doesn't work.

like image 553
Kang Liang Avatar asked Sep 03 '25 13:09

Kang Liang


1 Answers

After encountering similar errors, I have found that the following lines of code solve the segmentation fault:

export LANGUAGE=UTF-8 
export LC_ALL=en_US.UTF-8 
export LANG=UTF-8 
export LC_CTYPE=en_US.UTF-8
export LANG=en_US.UTF-8 
export LC_COLLATE=$LANG 
export LC_CTYPE=$LANG 
export LC_MESSAGES=$LANG 
export LC_MONETARY=$LANG 
export LC_NUMERIC=$LANG 
export LC_TIME=$LANG 
export LC_ALL=$LANG
like image 193
Mathews24 Avatar answered Sep 05 '25 02:09

Mathews24