-1

I want to run a python program directly in vim. I type

:w !python 

I get error

'no module named...' or 'no such file or directory: 'file.json''.

It occurs only when I load file after opening vim. When I start vim typing gvim filename.py, it works fine (:w !python works). What is wrong here?

4
  • 1
    I'm thinking this is caused because you're not running the script from the same directory. That might be the reason the script is failing to load file.json. Try starting vim when you're inside the same directory, load the file and do :w !python Commented May 30, 2016 at 5:38
  • I am using Windows and start VIM from my Start menu. On my Linux computer it is the same problem but I think it is weird to start VIM inside the same directory. I may change file and then what? Reload VIM? It is weird... Commented May 30, 2016 at 10:57
  • In that case, how about changing working directory to the directory where the file is and then running it. Like :cd %:h and then :w !python Commented May 31, 2016 at 11:26
  • 1
    If the program you are running is contained in a single file and you don't want to change directories, then you can use :w !python %. The percent sign % represents the current file open in vim. This is like calling :w !python /path/to/current/file.py Commented May 31, 2016 at 16:45

1 Answer 1

0

I solved it by adding this to my _vimrc config file:

noremap <F5> :w !python %<CR>
inoremap <F5> <ESC>:w !python %<CR>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.