0

I have recently switched to vim from VS Code. I have Vim 8.2.

I have put these lines in my _vimrc file so that I can run python in vim by pressing F9:-

autocmd FileType python map <buffer> <F9> :w<CR>:exec '!py -3' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F9> <esc>:w<CR>:exec '!py -3' shellescape(@%, 1)<CR>

I have tried running a few python programs and all of them ran smoothly except one in which my program has to read a JSON file which is present in the same directory as that of the script, but the shell in vim tells me that it couldn't find that file. it says:-

FileNotFoundError: [Errno 2] No such file or directory: 'main_dic.json'
shell returned 1

I run the same program on my Windows CMD and it works perfectly.

Why is this happening? How can I fix this?

2

1 Answer 1

1

I presume you run (g)Vim with a shortcut.

  1. there is current working directory in vim :lcd to see it and :lcd dir to change it. (if you run (g)vim.exe or shortcut it would be path to (g)vim executable)
  2. depending on how your python script is implemented, it might read this json file from current working directory (it is inherited by :!) not from your script location directory.

so either change current working direcory :lcd path/to/your/python/script/ or change your python script to read file relative to script location.

1
  • Using :lcd %:h (literally) will work too
    – filbranden
    Commented Sep 7, 2020 at 13:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.