Saturday, August 26, 2017

Run Python Script Linux (Ubuntu) Command Line


You can run Python script through Linux command line.

It is a great feature of using Linux kernel base Operating System like Ubuntu, Linux Mint etc that you can easily run or compile Python file through Linux Command Line without installing any IDE for Python Language. Not only Python File, you can interpret Python statement on Terminal.

So this tutorial will let you learn -
To create Python File on Terminal:
  1. Open Terminal by Ctrl + Alt + T
  2. Open any Text Editor and create a Python file with file_name.py . If you have Sublime Text Editor then write the following command and hit Enter.
    subl file_name.py
    If you don't have any then write the following command and hit Enter.
    gedit file_name.py
    By default, Ubuntu has Gedit Text Editor.
Run Python Script Linux (Ubuntu) Command Line

When your text editor is ready then write your Python code and Save.
To run Python File on Terminal:
  1. Open Terminal by Ctrl + Alt + T, if it is opened, then no need to reopen.
  2. First check your current directory of created Python file. To check directory run the following command.
    pwd
  3. Then change the directory to your python file file_name.py. Suppose your current directory of your created file is /home/you/Desktop, then run the following command.
    cd /home/you/Desktop
  4. After changing directory from Terminal, put the following command.
    python file_name.py
Here, file_name.py is the name of your Python file. You will see the output of your python file.

To write Python Code and Run on Terminal:
  1. Open Terminal by Ctrl + Alt + T, if it is opened, then no need to re-open.
  2. Type Python and hit Enter
  3. Now write Python code and hit Enter to next statement.
Some Tips to write Python code on Terminal smoothly:
  • To copy and code on Terminal first select by mouse and Ctrl + Shift + C to copy and Ctrl + Shift + V to paste.
  • Since Indention is the most important for Python Programming Language, Put Spacebar twice when Indention is needed. Example: 
Fun with Python in Terminal:
  • >> Type 8 + 3 and hit Enter, you will see result 11
  • >> Type list = [3, 4, 2, 5] and hit Enter. Again Type list and hit Enter, you will see list = [3, 4, 2, 5]
  • >> Go on.
Have got this article helpful or not working? Put your comment below.