Python shell under Mavericks: two sides of the readline.so bug - python

There is a problem with readline in Python shell under Mavericks. Two issues, answered here in stackoverflow separately but not both at the same time.
a) segfault after entering the 2nd line and/or
b) line editing does not work.
I provide links to both issues below.
Basically one can either A) do nothing and enjoy segfaults while typing a second line in the shell, or B) - "fix" it by renaming readline.so to readline.so.disabled -- but in this case loosing editing capabilities in the Python shell.
I could not find a recipe that solves both issues. And all of the sudden it's really hard to use Python shell. Please help.
Here are more details:
a) The segfault issue is answered here and here:
Python crashing when running two commands (Segmentation Fault: 11)
Segmentation fault: 11 in OS X
b) the "up arrow" issue in python interpreter is answered here:
Seeing escape characters when pressing the arrow keys in python shell
Here is how to reproduce the problem on my system:
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
Segmentation fault
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%sudo mv ./lib/python2.7/lib-dynload/readline.so ./lib/python2.7/lib-dynload/readline.so.disabled
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
>>> ^[[A
File "<stdin>", line 1
^
SyntaxError: invalid syntax

This will not fix your system install, but I think a virtualenv would not have this problem. I use Python every day on Mavericks with a virtualenv and never have any trouble.

Related

Make Python module available for all Linux users

I have a Python 2.7 question, if somebody can help.
When we install a Python module using pip, how do we make it available to all users? Please, see the example below (with module faker). The import works when I am root, but doesn’t work when I am ubuntu user.
I have already tried to install using option --system, and also changing umask, as recommended in some articles I have found. Didn’t work so far. Any ideas?
If we run "which python", both users point to the same one.
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
>>>
>>> exit()
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# exit
exit
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$ python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named faker
>>>
Ok, I solved the issue.
In my case, the problematic module was "faker". But, when we install the faker, another additional module is installed as well (in this case - text-unidecode).
Then I uninstalled both modules, ran "umask 022" and re-installed the faker.
This solved the issue for all other users.
Thanks all for the help!

Python unicode, UCS4 build or UCS2 build

On my Linux machine, directly execute python command, it shows that my Python is UCS4 build.
Python 2.7.3 (default, Jan 8 2018, 17:43:28)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> if sys.maxunicode > 65535:
... print 'UCS4 build'
... else:
... print 'UCS2 build'
...
UCS4 build
However, when I call python in C++ program using
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("if sys.maxunicode > 65535:\n print 'UCS4 build'\nelse:\n print 'UCS2 build'");
it prints "UCS2 build".
Other information from the python called by c++ are:
platform:Linux-2.6.32_1-19-0-0-x86_64-with-centos-6.3-Final
('Python', '2.7.5 (default, Apr 13 2016, 14:25:24)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]')
('Python', '******/venv')
I have double checked the python executable path. They are from the same path, but python version and gcc version are different.
Anyone know the reason of this strange sympotom?
Well, I solved this problem. Using ldd command, I found that libpython2.7.so.1.0 pointed to a wrong default path, not the one printed before. Correct the LD_LIBRARY_PATH solved this confusion.....

Using Appkit and Python to hide mouse cursor on OSX

I'm trying to script hiding the mouse cursor on OSX 10.9. I have Chrome starting and going full screen for a kiosk and I'd like to periodically run a script to hide the cursor.
Applescript no longer directly supports "call method" to call the objective C method, so I thought the simplest method would be to use AppKit from the provided python.
It crashes:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AppKit
>>> AppKit.NSCursor.hide()
Assertion failed: (CGAtomicGet(&is_initialized)), function CGSConnectionByID, file Services/Connection/CGSConnection.c, line 123.
Abort trap: 6
I suspect there is a pre-requisite call I need to make to initialize something, but I haven't found anything yet while digging through docs/google.
What am I missing?
I've had luck with the Quartz included with PyObjC:
import Quartz
Quartz.CGDisplayHideCursor(Quartz.CGMainDisplayID())

Why do I get a segmentation fault when using modulo in the Python interpreter? [duplicate]

This question already has answers here:
Python crashing when running two commands (Segmentation Fault: 11)
(2 answers)
Closed 8 years ago.
MacBook-Air:~ sgarza62$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 % 10
1
>>> 10 % 1
Segmentation fault: 11
Also happens with modulo operations such as 1 % 1 and 2 % 2.
Running Mac OS 10.9.2 on this machine.
This has nothing to do with the modulo operation. The interactive interpreter will crash on every 2nd command invoked.
The OS 10.9 (Mavericks) upgrade caused instability in some Python releases. To fix this specific issue, download and run the patch through the Terminal:
>>> curl -O http://bugs.python.org/file32324/patch_readline_issue_18458.sh
>>> openssl sha1 patch_readline_issue_18458.sh
>>> sh ./patch_readline_issue_18458.sh
Newer Python releases have fixed the problems introduced with Mavericks. Better to install the latest version at https://www.python.org/downloads/
Thanks to Hyperboreus for finding the solution in the question comments.

How to cancel interpreter command in emacs python-mode

If I mistype a command in the python intepreter, sometimes I just see .... For example if I type help(random.unif and press enter I cannot enter a new command. I have to exit emacs and start python and the interpreter again. Is there way to correct this?
As Pavel Anossov explains, you want to send Python a CTRL-C to interrupt it.
But in emacs, by default, CTRL-C is a prefix key.
Fortunately, in most interactive-shell modes, including python-mode and the alternatives, hitting CTRL-C twice in a row sends a ctrl-C to the interpreter. Or, more technically, CTRL-CCTRL-C is bound to comint-interrupt-subjob. (You can, of course, run it any other way—even META-X comint-interrupt-subjob if you really want.) The result looks like this:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(f
... ^C ^C
KeyboardInterrupt
>>>
Another alternative is to use the quoted-insert command, usually bound to CTRL-Q, and then hit CTRL-C. However, because this will not interrupt the usual line input, you will usually have to follow it with a newline. The result looks like this:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(f
... ^C
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>>
Usually CTRL-C works. Not sure about emacs-embedded interpreter. Alternatively, just provide the interpreter with whatever it's waiting for (in your example, an )).

Categories

Resources