:!% 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
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
So that's a potential solution:
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.
python works for you (sometimes you'll need
python3), you can use:
This is much more robust than depending on all the other factors mentioned above being properly configured.