If you thought nothing gets easier than learning Python, executing Python programs is even easier.
To run the python program, all you have to do is follow the given command syntax:
And if you are running python2, you use only
python instead of
You may use dedicated IDEs like PyCharm for a more immersive Python coding experience, but this is sufficient for running a couple of Python scripts.
Let's see it in more detail.
Run Python program in Ubuntu command line
For your reference, I will be using the following Python code throughout this tutorial:
print('Hello Ubuntu users, greetings from sagar sharma')
Yes, that's a simple hello-world python program that is saved as
hello.py in my system.
To execute the program, all you have to do is append the filename to the
python3 command and that's it:
As I mentioned earlier, I will be using
hello.py so my command will be:
And that's it!
Run Python program as a script in Ubuntu command line
You might not want to use the
python3 command just for executing the python program.
In that case, you can call the interpreter to do the work for you!
But to do so, you will have to make a slight change in your existing code.
You will have to add the following line at the beginning of your python code:
Once you do that, you will have to make this file executable using the chmod command:
chmod +x Filename.py
And finally, you can execute the python program as a script:
And as you can see, the program was executed the same way as the scripts.
Want to learn Python for free?
Sure, there are a bunch of free tutorials on the internet but we handpicked the best for you!
You can refer to the following guide that contains some of the best (and free) Python courses and ebooks:
I hope you will find this guide helpful.
And if you still have any doubts related to Python or anything else related to Linux, feel free to ask me in the comments.