Hello world program in python mentioning interpreter using shebang.

In the last post, we have created “hello world” program and executed it from command line using python command.

Here we will check usage of shebang sequence followed by the python interpreter or binary path.

Lets see the old program again and what happens if we try to execute it directly.

Old Scenario

user@ngelinux$ cat first.py 
print("Hello, World!")
user@ngelinux$ 

### Error generated if we try to execute it.
user@ngelinux$ ./first.py 
./first.py: line 1: syntax error near unexpected token `"Hello, World!"'
./first.py: line 1: `print("Hello, World!")'

Create First Python Hellow world program with interpreter/shebang

### First check path of python interpreter
user@ngelinux$ which python
/usr/bin/python

### Create python script with first line shebang sequence
user@ngelinux$ cat second.py 
#!/usr/bin/python
print("Hello, World!")
user@ngelinux$ 

### Now execute the script and check it is successfully executed.
user@ngelinux$ ./second.py 
Hello, World!
user@ngelinux$ 

Leave a Reply

Your email address will not be published.