comparison runtime/doc/if_pyth.txt @ 10722:7598ce51bf2a v8.0.0251

patch 8.0.0251: not easy to select Python 2 or 3 commit https://github.com/vim/vim/commit/f42dd3c3901ea0ba38e67a616aea9953cae81b8d Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 28 16:06:38 2017 +0100 patch 8.0.0251: not easy to select Python 2 or 3 Problem: It is not so easy to write a script that works with both Python 2 and Python 3, even when the Python code works with both. Solution: Add 'pyxversion', :pyx, etc. (Marc Weber, Ken Takata)
author Christian Brabandt <cb@256bit.org>
date Sat, 28 Jan 2017 16:15:04 +0100
parents 584c835a2de1
children 523cd59d6db0
comparison
equal deleted inserted replaced
10721:9177c4f6a229 10722:7598ce51bf2a
14 6. Tab page objects |python-tabpage| 14 6. Tab page objects |python-tabpage|
15 7. vim.bindeval objects |python-bindeval-objects| 15 7. vim.bindeval objects |python-bindeval-objects|
16 8. pyeval(), py3eval() Vim functions |python-pyeval| 16 8. pyeval(), py3eval() Vim functions |python-pyeval|
17 9. Dynamic loading |python-dynamic| 17 9. Dynamic loading |python-dynamic|
18 10. Python 3 |python3| 18 10. Python 3 |python3|
19 11. Python X |python_x|
19 20
20 {Vi does not have any of these commands} 21 {Vi does not have any of these commands}
21 22
22 The Python 2.x interface is available only when Vim was compiled with the 23 The Python 2.x interface is available only when Vim was compiled with the
23 |+python| feature. 24 |+python| feature.
709 ============================================================================== 710 ==============================================================================
710 8. pyeval() and py3eval() Vim functions *python-pyeval* 711 8. pyeval() and py3eval() Vim functions *python-pyeval*
711 712
712 To facilitate bi-directional interface, you can use |pyeval()| and |py3eval()| 713 To facilitate bi-directional interface, you can use |pyeval()| and |py3eval()|
713 functions to evaluate Python expressions and pass their values to VimL. 714 functions to evaluate Python expressions and pass their values to VimL.
715 |pyxeval()| is also available.
714 716
715 ============================================================================== 717 ==============================================================================
716 9. Dynamic loading *python-dynamic* 718 9. Dynamic loading *python-dynamic*
717 719
718 On MS-Windows and Unix the Python library can be loaded dynamically. The 720 On MS-Windows and Unix the Python library can be loaded dynamically. The
810 dynamically, these has() calls will try to load them. If only one can be 812 dynamically, these has() calls will try to load them. If only one can be
811 loaded at a time, just checking if Python 2 or 3 are available will prevent 813 loaded at a time, just checking if Python 2 or 3 are available will prevent
812 the other one from being available. 814 the other one from being available.
813 815
814 ============================================================================== 816 ==============================================================================
817 11. Python X *python_x* *pythonx*
818
819 Because most python code can be written so that it works with python 2.6+ and
820 python 3 the pyx* functions and commands have been writen. They work exactly
821 the same as the Python 2 and 3 variants, but select the Python version using
822 the 'pyxversion' setting.
823
824 You should set 'pyxversion' in your |.vimrc| to prefer Python 2 or Python 3
825 for Python commands. If you change this setting at runtime you may risk that
826 state of plugins (such as initialization) may be lost.
827
828 If you want to use a module, you can put it in the {rtp}/pythonx directory.
829 See |pythonx-directory|.
830
831 *:pyx* *:pythonx*
832 The `:pyx` and `:pythonx` commands work similar to `:python`. A simple check
833 if the `:pyx` command is working: >
834 :pyx print("Hello")
835
836 To see what version of Python is being used: >
837 :pyx import sys
838 :pyx print(sys.version)
839 <
840 *:pyxfile* *python_x-special-comments*
841 The `:pyxfile` command works similar to `:pyfile`. However you can add one of
842 these comments to force Vim using `:pyfile` or `:py3file`: >
843 #!/any string/python2 " Shebang. Must be the first line of the file.
844 #!/any string/python3 " Shebang. Must be the first line of the file.
845 # requires python 2.x " Maximum lines depend on 'modelines'.
846 # requires python 3.x " Maximum lines depend on 'modelines'.
847 Unlike normal modelines, the bottom of the file is not checked.
848 If none of them are found, the 'pyxversion' setting is used.
849 *W20* *W21*
850 If Vim does not support the selected Python version a silent message will be
851 printed. Use `:messages` to read them.
852
853 *:pyxdo*
854 The `:pyxdo` command works similar to `:pydo`.
855
856 *has-pythonx*
857 You can test if pyx* commands are available with: >
858 if has('pythonx')
859 echo 'pyx* commands are available. (Python ' . &pyx . ')'
860 endif
861
862 When compiled with only one of |+python| or |+python3|, the has() returns 1.
863 When compiled with both |+python| and |+python3|, the test depends on the
864 'pyxversion' setting. If 'pyxversion' is 0, it tests Python 3 first, and if
865 it is not available then Python 2. If 'pyxversion' is 2 or 3, it tests only
866 Python 2 or 3 respectively.
867
868 Note that for has('pythonx') to work it may try to dynamically load Python 3
869 or 2. This may have side effects, especially when Vim can only load one of
870 the two.
871
872 If a user prefers Python 2 and want to fallback to Python 3, he needs to set
873 'pyxversion' explicitly in his |.vimrc|. E.g.: >
874 if has('python')
875 set pyx=2
876 elseif has('python3')
877 set pyx=3
878 endif
879
880 ==============================================================================
815 vim:tw=78:ts=8:ft=help:norl: 881 vim:tw=78:ts=8:ft=help:norl: