Python 3, vim 8 and the pythoncomplete error - python

Editing a python file in vim and hitting Ctrl+x followed by Ctrl+o in insert mode results in
Error: Required vim compiled with +python
E117: Unbekannte Funktion: pythoncomplete#Complete
I've read a lot of postings and did a lot of fumbling until this:
I'm using vim 8.0.54
installed python and vim are both 32 bit
Vim :vers returns ... +python/dyn and +python3/dyn
the python35.dll file mentioned in :vers -DDYNAMIC_PYTHON3_DLL=\"python35.dll\" is located in in the same folder as the gvim.exe
in my _vimrc let g:pymode_python = 'python3' should tell vim to user python3
:echo has ('python3') returns 1
But the error still remains... any idea besides the error in front of the keyboard?
update 2016-11-08:
It's not a good way, but it solved my problem. I changed
setlocal omnifunc=pythoncomplete#Complete to
setlocal omnifunc=python3complete#Complete in the python.vim file in the ftplugin-folder. Now it works just fine. Any recommendations to achieve the same without modifying the python.vim file?
update 2016-11-08:
Finally solved it with autocmd BufNewFile *.py :set omnifunc=python3complete#Complete in my _vimrc

Is this a Vim you compiled? The vers results looks correct. The error E117 is specific to calling a function using vim's eval feature.
However, the let g:pymode_python = 'python3 variable is for a specific "python-mode" plugin.
Is the python3 library in your search path?
Were python and python3 compiled with --enable-shared?
are the questions to answer.
Not sure if vim searches it's own executable folder first or not, but to check so, you can do:
To use the Python interface the Python DLL must be in your search path. In a
console window type "path" to see what directories are used.
A bit more insight possible in the docs: doc/if_pyth.html#python-dynamic
HTH

Related

VIM - unknown function: pythoncomplete#Complete

when I type the following code in a .py - file:
import numpy
numpy.
and then press the TAB key which triggers Omnicompletion, I get the following error:
Error: Required vim compiled with +python
E117: Unknown function: pythoncomplete#Complete
However, when I type vim --version | grep python, I get:
+cryptv +linebreak +python/dyn +viminfo
+cscope +lispindent +python3/dyn +vreplace
Can someone explain this behaviour or does someone have any ideas where to dig? There is a similar question on stackoverflow, but in that case VIM was compiled without python support.
Check again from within Vim with
:echo has('python')
It may be that even though your Vim has been compiled with Python, the dynamic loading of the Python interpreter fails, usually because the corresponding shared library cannot be found. You find its name in the :version output under Compilation:
Apparently, your vim is compiled with dynamic support for both python versions ( python3/dyn and python/dyn ). There is one thing you should keep in mind in this setup:
The command has('python') and has('python3') already invokes the usage of this specific python version exclusively.
So apparently, YCM invokes has('python') before has('python3'), so that python3 becomes active and python2 inactive for the rest of the vim-session. pythoncomplete#Complete somehow only works with python2.
So you could fix pythoncomplete#Complete by explicitly invoking python2 at the very top of your vimrc:
set nocompatible
if has('python') " if dynamic py|py3, this line already activates python2.
let s:python_version = 2
elseif has('python3')
let s:python_version = 3
else
let s:python_version = 0
endif
echomsg 'Using python'.s:python_version
In the rest of your vimrc you can check for python version with s:python_version, if you dont need it, the has commands already to the job.
You might need both python2 & 3 as in my case
let g:python3_host_prog='C:/Bin/Miniconda3/python.exe'
let g:python_host_prog='C:/Bin/miniconda2/python.exe'
:echo has('python') returned 0 until I did this.
Need for YCM & ensime & neoterm in my case
Downloaded directly from the neoterm site (windows obviously for this case)

global-font-lock-mode not working for python files in emacs

I cannot seem to turn off syntax highlighting by default in emacs when editing python files.
I am running GNU Emacs 23.1.1 on CentOS release 6.5. There are only two lines in my .emacs file:
(global-font-lock-mode 0)
(global-set-key (kbd "C-g") 'goto-line)
My C-g binding works, so I know the file is being read. When I open or create a .c file, there is no highlighting. But whenever I open or create a .py file, the syntax highlighting is still there and I have to turn it off with M-x font-lock-mode. The setting seems to work correctly for other types of code, like .c and .java files. It's only python where the highlighting still appears.
What's going on here? Does it have something to do with python-mode?
(global-font-lock-mode 0) should DTRT
Found a bug in components-python-mode, the development-branch of python-mode.el
fixed here
https://bugs.launchpad.net/python-mode/+bug/1410386
also fixed here:
https://github.com/pdee/pdee
Which python-mode do you use?

Python script gives `: No such file or directory`

I have several python scripts which work just fine but one script has (as of this morning) started giving me this error if I try to run it from the bash:
: No such file or directory
I am able to run the 'broken' script by doing python script_name.py and after looking around a bit the general idea that I picked up was that maybe my line ending of the hashbang got changed (silently) so I looked at the line ending of a working script and a broken script via the :set list option in VI as indicated in this question -> View line-endings in a text file
Both files appear to end using the same character (a $) so I am kind of at a loss on how to proceed from here. Specifically, how to actually 'see' the line ending in case the set list was not the right method.
PS: The script is executable and the shebang is in there, I stated that it's just this 1 script that was working fine before the weekend but it started giving me this error as of this morning.
-- edit: --
Running the script through dos2unix does get it working again but I would like to know of any way to visualize the line ending somehow in VI(M) or why Geany somehow converted the line endings in the first place (as I never work on a dos/windows system anyhow).
From the comments above it looks like you have dos line endings, and so the hashbang line is not properly processed.
Line ending style are not shown with :set list in Vim because that option is only used when reading/writing the file. In memory line endings are always that, line-endings. The line ending style used for a file is kept in a Vim per-file option, weirdly called fileformat.
To see/change the line ending style from Vim, you can use the following commands:
:set fileformat
:set ff
It will show dos or unix. You want unix, of course ;-).
To change it quickly you can save the file with:
:w ++ff=unix
Or if you prefer:
:set ff=unix
And then save the file normally.
So see all the gory details just do :help fileformat, :help file-formats and :help fileformats
You can also use the dos2unix command to convert the file format
dos2unix
This helped me to run the python scripts
This normally happens when we open files in windows do changes and save it.
if you open the file locate the ^M characters at the end of every line
Thanks
Personally, I find it kinda wrong using direct path to python interpreter. As you dont use windows platform, you should have program env, usually in /usr/bin (/usr/bin/env). Try using following shebang:
#!/usr/bin/env python
Different distros store python binary in /bin or /usr/bin (or some weird locations), and this one makes your script config-independent (as far as possible, here we have possibility that env is stored elsewhere; still - it is less possible that env is not in /usr/bin than that python is mislocated).
I had similiar problem (if not exactly the same) and that worked for me.
Also, I have both python interpreters (2.7.x and 3.x) installed, so I need to use "python3" argument for env. AFAIR usually distros link different names to different binaries, so "env python" will run python2.7 on my system, "env python3" (also python33, or smth like that) will run p3k, and "env python2" (also python27, etc) will run python 2.7.x. Declaring which version of interpreter should be used seems like a good idea too.
I came across this problem editing my code on Windows, checking it in with git, and checking out and running it on Linux.
My solution was: tell git to Do The Right Thing. I issued this command on the Windows box:
git config --global core.autocrlf true
Modified the files and checked them in; voila, no such problem any more.
As discussed on the Git documentation.

Can Python be run from within the vim editor? [duplicate]

This question already has answers here:
Executing Python with Gvim
(4 answers)
Closed 9 years ago.
Is it possible to run Python code from within the vim editor?
What is necessary to install the support along with Python syntax highlighting?
How would I install "python.vim : Enhanced version of the python syntax highlighting script" ?
I did not automatically create ~/.vim/syntax and I'm using a Mac, all I downloaded was the .app file, an executable that I don't know of its purpose and a readme file.
I've tried also creating a folder for the python.vim file, but that didn't work out either.
Personally:
When inside Vim editing my Python scripts, I simply hit CtrlZ so as to return in console mode.
Run my script with command $ python my_script.py.
When done, I enter $ fg in the command line and that gets me back inside Vim, in the state I was before hitting CtrlZ. (fg as in foreground)
Edit
Recently I have started using the :terminal mode of vim much more frequently.
I tend to prefer it to CtrlZZ because it may happen that I forget that I used Ctrl-z and open an additional vim session: it may become messy. Also, having a terminal pane is easier for dealing with line number in errors message, since the two views are available at the same time.
So the workflow I'm using nowadays has become:
:terminal (in my case I have a vim mapping with leader key) <leader>tm :terminal<cr> so that I don't even type :terminal manually)
Run my script with command $ python my_script.py.
$ exit in the bash command line if I want to close the terminal pane
You don't have to install any plugins to get syntax highlighting for any version of Python in Vim. The python-syntax plugin might have more features, but it's absolutely not needed and not important if you're new to Vim and Python. To enable syntax highlighting add this to your ~/.vimrc:
filetype plugin on
syntax on
Adding to Kent's answer you can also send arguments to the script you're running if you want to.
[args] is something you'd normally add after python script.py.
:w !python - [args]
Personally I prefer to have a seperate shell open in Tmux for running my scripts, and possibly play around with bpython. If I'm not using a Tmux session :sh works fine too, giving you a normal shell. You can get back to vim by doing exit or ctrld.
As for using Python with vim autocomplete is a big part of it, and I can recommend jedi-vim for this. If you want error checking/magic Syntastic is the tool for the job.
This might be a little overwhelming if you're new to both Vim and Python, but I suggest you take it as it comes, step by step. The first step for learning Vim is to do the Vimtutor. Run it by entering vimtutor in a shell.
If you are asking how to run Python code through vim, try this:
EDIT
thank #Zyx for pointing out the problems in my original line, I leave this line here, in order to let reader know where are the problems.
nmap <F9> :!python %<cr>
you could create this au (I saved has("autocmd") part...) in your vimrc:
autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(#%, 1)<cr>
put this in your .vimrc, then after you editing your .py file, press <f9> vim will try to compile and execute it (via external python interpreter).
how to install that plugin:
https://github.com/hdima/python-syntax#how-to-install

Vim :colorscheme on Python

I'm using Mac OSX Lion 10.7.2, Terminal.app supports 256 (output of :echo &t_Co).
In my vimrc I have (PATH/TO/vim/vimrc)
syntax on
filetype plugin indent on
set nobackup
When I "vim blah.py" and :colorscheme torte, syntax colors are not loading. For example python keyword doesn't have a proper colors (They have regular text color). That works for .c files but not python.
I updated my syntax/python.vim but still no luck.
Can someone tell me why?
markfw
Your answer is very good but let me just add one thing to it. In your .vimrc instead of adding just
let python_highlight_all=1
you should add it this way
autocmd BufRead,BufNewFile *.py let python_highlight_all=1
This way it only applies to Python file(s).
Hope this help.
if it works in c but not on py, the filetype file and/or syntax file is not at the right location for python.
vim manual should help you, but I also would try :scr command. This lists all the vim script loaded. So you start vim in two different way
vim your.c
vim your.py
and then in each vim session, type :scr. see how the syntax file for C is loaded (it is like chain reaction), and why it doesnt work that way for python may give you clue.
The way that I made it to work (I'm using Terminal) is to have let python_highlight_all = 1
in my ~/.vimrc file and now everything works fine and all objects such as list, tuple, ... are colored.
For more information please look at the syntax/python.vim.
Try to add the following lines to your ~/.vimrc:
set nocompatible
filetype on
syntax enabled
Quit and relaunch Vim or execute :so ~/.vimrc to reload the settings.
nocompatible remove compatibility with the original vi, this is recommended to get a fully functional Vim.
filetype on activate automatic file type detection, this is the option you want for your Python code to be colored.
syntax enabled activate code coloring, but i'm not sure if this is mandatory here.
You can get some more help by typing :help filetype in Vim.

Categories

Resources