7
votes
  • open gVim.
  • then using the File Menu and MenuItem Open to open a file pi.py which has the following tiny script:

enter image description here

How do I execute this code using gVim?


EDIT

If I use either :! python pi.py or :w !python - then I get the following:

enter image description here

4
@SatA - apologies - correct Gvim - whytheq
@Will Hardy solution seems to work :) - SatA
@SatA - on my machine if I type :w !python then I get the error I've added to the OP. Do you think I need to configure Vim more so that it finds the python interpretter? - whytheq
Hey, yes, I just added a proposal for a solution, does that help? - SatA

4 Answers

12
votes

You don't need to save the file, you can run the current buffer as stdin to a command such as python by typing:

:w !python -

(The hyphen at the end probably isn't necessary, python will generally use stdin by default)

edit: seeing as you are new to vim, note that this will not save the file, it will just run it. You will probably want to learn how to save your file.

5
votes

If you have python support compiled into vim you can use :pyfile % to run the current file. (python 2.7)

If you have python 3 support use :py3file % instead

pyfile help

4
votes

Something like this. Type following command in vi command model.

:! python test.py
2
votes

It seems your %path% environment variable doesn't contain your python installation path.

Follow the following steps:

  1. Right-Click "My Computer"
  2. Choose "Properties"
  3. Goto the advanced settings
  4. Goto "Environment Variables"
  5. Select "PATH" and click on "Edit"
  6. Add the following to the end of the line: ";C:\python27\;" (without quotes)

Note: change the directory to your python directory (could be c:\python30, for example)

Save everything and then - Close vim and all CMDs and try again.