TransWikia.com

Python script not running through vim but runs on Windows CMD

Vi and Vim Asked by Rohan on August 31, 2021

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?

One Answer

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.

Answered by Maxim Kim on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP