Control characters in Python shell - python

I have a problem when I am using Python interactive shell.
I love emacs-style key bindings, but when I typed <ctrl> A or <ctrl> K, it echo ^A or ^K. Like:
:)[12:16]root:~ # python
Python 2.7.9 (default, May 15 2015, 01:13:44)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print ^A^K^H
Is it due to readline lib? I have updated readline, but it seems no help.

Install the readline development package and rebuild and reinstall Python.

Related

Why did the python version not switch after conda switched the environment?

python3.7.12 should be the python version of the hsm environment
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ which python
/home/leelee/miniconda3/envs/hsm/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ /home/leelee/miniconda3/envs/hsm/bin/python
Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:21)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
But if I type python directly, it will return a 3.9.5 python version
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ python
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
I guess when I type python, I entered /home/leelee/miniconda3/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ which -a python
/home/leelee/miniconda3/envs/hsm/bin/python
/home/leelee/miniconda3/bin/python
(hsm) leelee#ubuntu-PowerEdge-T440:~/tools/hsm-master/predict$ /home/leelee/miniconda3/bin/python
Python 3.9.5 (default, Jun 4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
Why does this happen and how can I solve it?
Looks like you added Python to the system PATH, which is not recommended when using conda. Simply check your PATH after conda activation and have a look at the sequence of the Python folders.

Emacs Doom Run Python in MacOS

System: macOS Mojave 10.14.6
Python3: Python 3.7.4
My emacs config ( according to https://github.com/hlissner/doom-emacs/issues/212 );
(setq python-shell-interpreter "/usr/local/bin/python3" flycheck-python-pycompile-executable "/usr/local/bin/python3")
Trying to use run-python in emacs doom. Having:
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup failed, <class 'ModuleNotFoundError'>: No module named 'readline'
Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native-enable’ was t and "python3" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally.
import readline works perfectly in python3 in terminal.
Default python 2.7.10 doesn't work too (no my emacs config).
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import codecs, os;__pyfile = codecs.open('''/var/folders/y3/g447wqxd4l97rk7th2fszwb00000gn/T/pyQo4DAR''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/y3/g447wqxd4l97rk7th2fszwb00000gn/T/pyQo4DAR''');exec(compile(__code, '''/var/folders/y3/g447wqxd4l97rk7th2fszwb00000gn/T/pyQo4DAR''', 'exec'));
python.el: native completion setup failed, <type 'exceptions.Exception'>: libedit based readline is known not to work,
see etc/PROBLEMS under "In Inferior Python mode, input is echoed".
>>>
How to make emacs doom run python3 right?

Python logging module platform differences: OSX vs. Linux

Can anyone help me understand why python's logging module creates a StreamHandler on import, but only on some distributions? weird.. (note: happens on any python3)
On Mac
Python 3.5.6 (default, Dec 19 2019, 14:59:39)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.getLogger().handlers
[<logging.StreamHandler object at 0x108fc2630>]
Linux:
Python 3.7.4 (default, Jan 3 2020, 19:27:19)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.getLogger().handlers
[]
>>>
I can't seem to find any explanation about this behavior difference!
EDIT:
A bit more investigation suggests that some module installed in the virtualenv is causing this behavior mismatch. I created a fresh one with the same python interpreter and no handler gets made. I'm trying to investigate which module seems to cause this.
This isn't actually a platform difference, but was a result of an installed library.
I filed the issue upstream https://github.com/stuaxo/vext/issues/63

Save Interactive Python (using script)

I would like to save the input and the output of a Python session when providing the input as a file.
In interactive mode, I would type a command of expressions and get their evaluation, as shown:
Python 2.7.11 (default, Mar 8 2016, 18:01:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> abs(3)
3
>>> abs(-3)
3
The text above is my desired output.
When I write these two expressions in p.py:
abs(3)
abs(-3)
and execute python p.py, I don't get any output, which is fine because Python runs in non-iteractive mode. If I execute python -i < p.py, I just get the output, not the input.
So, I dediced to use the script command.
However, with script, I get different results in MacOs and Linux (and none of them save what I would like).
If I execute script -q output.txt python -i <p.py on MacOs, file output.txtcontains
abs(-3)
abs(3)
Python 2.7.11 (default, Mar 8 2016, 18:01:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> abs(-3)
3
>>> abs(3)
3
which is the desired output but with the input of p.py preppended to it.
If I execute script -q output.txt -c "python -i <p.py" on Linux, file output.txt contains
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3
>>> 3
which is far from I would like.
In brief:
How could I save a file that reproduces the interactive Python session when redirecting input to it?
Additionally,
Why do the MacOs and Linux implementations of script work so differently?

Caffe import working with python2 but not python2.7 / ipython notebook, despite both being 2.7.10?

I've successfully compiled the caffe library and the python module.
I can do this:
Jamess-Air:~ james$ python2 -V
Python 2.7.10
Jamess-Air:~ james$ python2
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>
But, bizarrely, this fails:
Jamess-Air:~ james$ python2.7 -V
Python 2.7.10
Jamess-Air:~ james$ python2.7
Python 2.7.10 (default, Jun 19 2015, 15:39:31)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Segmentation fault: 11
Jamess-Air:~ james$
I cannot understand this at all! Whenever I try to run using iPython notebook I get the same crash.
Any ideas as to what may be causing this, and how I might fix it, or at least get iPython Notebook to use the different python version so I can run this thing?
Thanks!

Categories