Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X - Can anyone explain why I was getting an OS error in my .pythonrc.py file after installing ipython3 or give me a more robust solution than this?

I installed ipython3 using pip3.

pip3 install ipython[all]

When I ran python it was encountering an error (only on Python 3 not on Python 2) from my ~/.pythonrc.py file.

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
  File "/Users/Mike/.pythonrc.py", line 43, in <module>
    readline.read_history_file(history_path)
OSError: [Errno 22] Invalid argument

I couldn't find an answer on stackoverflow but did some digging and found a solution. I'm not sure why this works now or if there's a better solution.

In the .pythonrc.py file:

try:
   import readline
except ImportError:
   pass

history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
   readline.read_history_file(history_path)
atexit.register(lambda x=history_path: readline.write_history_file(x))

The solution was to import gnureadline instead:

try:
   import gnureadline as readline
except ImportError:
   pass

Can anyone tell me if there is a more robust solution or why this happened in the first place?

like image 634
Mike Avatar asked Oct 30 '25 19:10

Mike


1 Answers

I just found the answer:

import gnureadline as readline

Based on the link:

gnureadline for IPython OSX

On OSX, if you are using the built-in Python shipped by Apple, you will be missing a proper readline implementation as Apple ships instead a library called libedit that provides only some of readline’s functionality. While you may find libedit sufficient, we have occasional reports of bugs with it and several developers who use OS X as their main environment consider libedit unacceptable for productive, regular use with IPython.

Therefore, IPython on OS X depends on the gnureadline module. We will not consider completion/history problems to be bugs for IPython if you are using libedit.

like image 67
Mike Avatar answered Nov 02 '25 08:11

Mike