For example , I write a Python program by vim , but how can I run it ? It just show a editor program when I double tap on it .
2 Answers
You can alternatively add a shebang at the start of your Python script and mark the script executable, which will cause it to run correctly when executed normally.
Add this line to the top of the script:
#!/usr/bin/env python
Note that due to difference between Python 2.x and 3, usually that will default to Python 2.7, so use this if you want Python 3:
#!/usr/bin/env python3
Then make it executable. This tells the shell/GUI that this is a program that can be executed.
chmod a+x <name of script>
Then you can run it without putting "python " in front - either by just typing <name of script>
at the prompt or double-clicking or whatever in the GUI.
Whether you do this or not is a matter of taste, you may prefer to invoke them with python <name of script>
or by this method.
Type
python name-of-python-script-here.py
in the terminal. Make sure the directory of the terminal is the same as the location of the file you created in vim.
-
2it doesn't have to be the same directory, the full path can be specified (e.g.
/path/to/script.py
. Also, using the shebang line#!/usr/bin/env python
at the begining of the script should tells programs to use python to run it.– WilfJul 3, 2015 at 2:05