ivanov/vim-ipython

[Feature Request] Would it possible to port to python 3?

Opened this issue · 8 comments

You know, pyzmq, cython and ipython, all of them support python 3.

yes, it's possible, and on my TODO list.

I was trying to think a good way to properly implement Python 3 support. The simple approach of replacing :python with :python3 works but without supporting Python 2. I think it will be possible to rewrite the python code to run under both. Would a solution using a command like this work?

if !exists('g:ipy_py3')
    let g:ipy_py3 = 0
endif

if g:ipy_py3
    command! -nargs=+ IPy_py python3 <args>
else
    command! -nargs=+ IPy_py python <args>
endif

I'm happy to try and write it or try another approach if anyone has one.

This idea came from how the html syntax file handles backwards compatibility:

https://code.google.com/p/vim/source/browse/runtime/syntax/html.vim#23

I think you could try with vim's has function: has('python') or has('python3') to set a python variable (which would be the "compiler")... I know I saw this in another vim plugin but I have no idea how to set it up as I have no experience with writing vim code, but I'm sure it's possible :).

Right now I have vim compiled only with python3 and would really love to use this plugin.

The problem with this approach is that it won't work if vim was compiled with both python and python3 but python3 was first used... It gives an error if you try to mix python and python3 as per each vim session only one of them can be used, so if you say:

if has('python')
   # define compiler as python in a variable called py_compiler (lets say)
else if has('python3')
   # define compiler as python3 in the same py_compiler variable

# then use it here
# py_compiler do something

This first tries to run python, not python3, but if python3 was used before in another plugin... then this would fail. I don't know a work around this :D... just spin balling here

I have been using this with python3 for a bit, and it seems to work great. There seem to be only a couple of changes needed syntactically to make the python code valid for 2 or 3:

@@ -125 +125,4 @@ def km_from_string(s=''):
-    from Queue import Empty
+    try:
+        from queue import Empty
+    except ImportError:
+        from Queue import Empty
@@ -161 +164 @@ def km_from_string(s=''):
-        except IOError,e:
+        except IOError as e:
@@ -179 +182 @@ def km_from_string(s=''):
-        except KeyError,e:
+        except KeyError as e:
@@ -232 +235 @@ def echo(arg,style="Question"):
-        print "-- %s" % arg
+        print("-- %s" % arg)
@@ -630 +633 @@ def toggle_reselect():
-    print "F9 will%sreselect lines after sending to ipython"% (reselect and " " or " not ")
+    print("F9 will%sreselect lines after sending to ipython"% (reselect and " " or " not "))

If I change just those 5 things, it works for me with either python 2 or 3, whichever one ipython is using. (Initially, I had changed every occurrence of "python" to "python3" and "py" to "py3", which worked for python 3 but didn't seem to be necessary.)

Strange note: contrary to what I've read, this seems to work with python 2 if that is what ipython is running, even though my vim was compiled with python 3 only. Maybe I just haven't stumbled on the right thing to cause a problem. Also strange: within vim :py and :python are not valid commands (only :py3 and :python3), but in the ipy.vim script :py and :python work fine... ???

👍

I come back to this, wanted to mention that python3 does indeed work which is super cool! I only had to add unicode = bytes in the first try / except block, at the beginning of vim_ipython.py. Of course, I had to change all occurances of :py, :python to :py3, :python3 respectively in ipy.vim. Also I'm running ipython 3.2 and this came with a little complication... the doc command wouldn't work and I had add:

try:
    msg_id = kc.shell_channel.object_info(word, level)
except AttributeError:
    msg_id = kc.inspect(word, detail_level=level)

to the get_doc function. I haven't tried all of the other commands yet, just <c-s> on one line, and <localleader>d.

note I did all this on neovim, don't know if it's the same for vim (I think it should be)...

any forks with this patches for python3? Or this is already in master? I just tried one week ago with python3+ipython3+vim(python3 without python2) and was not working...I the end I returned to my the old and working vim+ipython in tmux.

I tried @chadawagner suggestion but vim-ipython was not able to connect to ipython3 kernel.

I hadn't updated ipy.vim in a while, so just pulled the latest.
I had to change

if isinstance(s,unicode):
    s=s.encode(vim_encoding)

to just

s=s.encode(vim_encoding)

and then had to change all references of python or py to python3 or py3. There was some weirdness before (around the time of my previous post) as to whether it needed that change or not. It didn't seem to need it at the time, but then shortly thereafter python and py wouldn't work anymore. Maybe something to do with virtual envs or the way homebrew is compiling vim with python3.

What I actually ended up doing was to modify the test for python at the top of ipy.vim:

if has('python3')
    command! -nargs=* Py python3 <args>
elseif has('python')
    command! -nargs=* Py python <args>
else
    echo 'python not found!'
    finish
endif

and then changing all references of python or py to just Py.

Also, last year I had to add stty stop undef to my .bashrc to get <ctrl-s> to work, but now it seems to need stty -ixon.

Weird that I needed to make all these changes, as it seems like it's working for others without them. (See commit d9105f0 from 2014-07-08: "Py3K support. Three at last, three at last!") YMMV, but mine is now working great.