0

I'm new to Vim. I ran the code

print("Hello world")
name = input("Name: ")
print("Hello",name)

and then ran it using :w !python and got the result

Hello world
Name: Traceback (most recent call last):
   File "<stdin>", line 2, in <module>
EOFError: EOF when reading a line

I attempted the fix suggested at https://stackoverflow.com/questions/38149646/vim-eof-error-when-python-code-requires-input however that did not work. I also tried it with #!/usr/bin python instead since I couldn't find the /env in /usr/bin however could find python (I'm on a fedora VM by the way). Neither worked as whenever I would run :!% all I would get was

/bin/bash: HelloWorld.py: command not found

shell returned 127

Press ENTER or type command to continue

How do I sort this out so that I can use inputs in my code. Does this problem persist in other languages too?

1

For :!% to work, you need a few prerequisites to be fulfilled.

First, you need the file itself to be executable. If it's a Python script, that means it needs to have a proper "shebang" at the start, such as #!/usr/bin/env python or #!/usr/bin/python or similar. It looks like you added one of those, so try and make sure it's actually correct. Additionally, you need to make sure the file is actually marked with executable permissions, which you can set using chmod +x and the path to the script file.

Once the file is actually executable, you can try to run it using :!%, but that will try to execute it, using the name Vim knows the file by. If you opened the file using vim HelloWorld.py, since you're running it on the directory where the file resides, then Vim will try to execute the command HelloWorld.py.

But if you run a command on the shell and the command doesn't include any /s (in other words, it doesn't look like a path to a file), then the shell will look for the command in the directories listed in the $PATH environment variable, and the variable typically doesn't include ., which is the current directory. (It's a bad idea to add . for the current directory to $PATH, I'd recommend not doing that.)

That's why you'll typically see instructions to run scripts from the current directory using the ./ prefix, do they do look like a path to a file and the shell will simply execute the script instead of doing a $PATH lookup. In your case, if you execute ./HelloWorld.py, you might get it to run (assuming it's properly set as executable, with a working shebang.)

From inside Vim, you could use the %:p modifier to have Vim use a full path to the file. By using a path that will forcefully include /s, you'll have the shell execute that particular script instead of looking for the script's name as a command in $PATH.

So that's a potential solution:

:!%:p

But a much better solution is to just call Python explicitly when running your script. That way, not only you don't need to worry about the shell's $PATH lookup, but you also don't need to make the script executable or add a shebang to it.

Assuming command python works for you (sometimes you'll need python3), you can use:

:!python %

This is much more robust than depending on all the other factors mentioned above being properly configured.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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