In Windows, the file types are identified by the last 3 chars after the . However, in linux (and in Ubuntu) .py just helps us humans figure out what kind of file it is. You have to tell Ubuntu that the script you wrote is a python script and it should be run with a python interpreter. You can do this in two ways:
First you can call python
followed by the name of the script. That is open a terminal and type:
python hellowworld.py
This will work.
Second, however, you want to use the GUI and run it by double clicking. To this you need a line in the script that tells Ubuntu that the next lines are to be interpreted as a python script. This first line is called the shabang line. For python it looks like:
#! /usr/bin/python
Note the #!
at the beginning. The first two characters of your script must be those two followed by the rest of the line. So your script will look like:
#! /usr/bin/python
print"hello world"
vary = raw_input("Enter your Name")
print ("hey " + vary)
As you noted the script must be tagged as executable. To do that, right click on the script's icon and go to Properties>Permissions and check Allow executing file as program.
Now when you double click on the script it will show you a Window with the choices:
Run in terminal
, Display
, Cancel
, Run
You can try Run in terminal
. It will open a terminal and show you the prompt:
Enter your name
When you enter your name the terminal will close as the script completes. So you will never see "hey [your name]" as it will happen too fast for your eyes to catch it. You will have to modify the script so that it pauses and wait for you to dismiss it after you see the display.
Hope this helps
Specify the path. If you're in the directory with the binary, that's as simple as:
./python
Or yes, do as Muru suggested and add it to the path. That said, changing the path could interfere with how applications (running under your user) call Python. If you deflect them to the wrong version, they may function incorrectly.
There is not a good way to override the global python
.
It should point to the version that ships with Ubuntu.
Best Answer
There's two things needed.
#!
line telling the OS which interpreter to use. In your case your very first line in the code must be#!/usr/bin/env python3
You need to open file manager , go to Edit -> Preferences -> Behavior, and select what to do with executable files
chmod +x /path/to/script.py
and in GUI, right click on the file and alter its Properties -> PermissionsThe very first line is called shebang line and must start with
#!
; whatever comes next is the name of the interpreter that will read your code. In case you are usingpython3
you could use either#!/usr/bin/python3
or#!/usr/bin/env python3
for portability. If you are not using code that will be specific to python version - just use#!/usr/bin/env python
Note on the script output:
If your script prints output to console, it will need to have terminal window, or alternatively use GUI dialogs such as
zenity
. Prefer usingRun in Terminal
option if you want to see the code. If you want the script to do something without seeing console output - useRun
option.In addition, if you have command line parameters , such as
sys.argv[1]
in the script , you can't set them unless you have terminal window open.