How to Run Python Scripts in Ubuntu 22.04?


Python is a popularly used high-level programming language across the globe. It supports programming paradigms like OOP, structured programming, etc. In addition to this, it is frequently used as a scripting language and is compatible with all the major operating systems, including Ubuntu. Users can create a Python script in any text editor, IDE, terminal, etc. Once a script is created, it can be executed using different ways in Ubuntu.

This article will demonstrate below listed ways of running Python scripts in Ubuntu 22.04 LTS:

  • How to Run/Execute Python Script via the Command Line?
  • How to Run/Execute Bash Style Python Script via the Command Line?
  • How to Run/Execute Python Script via Interactive Mode?
  • How to Run/Execute Python Script via Text Editor?
  • How to Run/Execute Python Script via IDE?
  • How to Run/Execute Python Script via Jupyter Notebooks?

How to Run/Execute a Python Script via the Command Line?

A Python script is a set of Python instructions enclosed in a file. To create a Python script, first launch the terminal by pressing a combination of keys [Ctrl + Alt + T]. Then, run the command below to create a Python script via the Nano text editor:


The above command launches the Nano text editor. Type the contents of the script in a nano text editor:

print(“Hello World”)
print(“Sample Python Script”)

The above file will display “Hello World” and “Sample Python Script”. Press Ctrl+O and Ctrl+X to

Save and exit the Nano editor, respectively.

To verify the creation of the script, run the list command:

The output indicates the successful creation of the testscript.py script. To run the testscript.py, execute the following command:


The output indicates the successful execution of the testscript.py script as it prints “Hello World” and “Sample Python Script”.

How to Run/Execute Bash Style Python Script via the Command Line?

To create a bash-style Python script, first launch the terminal by pressing a combination of keys [Ctrl + Alt + T]. Then, execute the following command to create a bash-style Python script using the Nano text editor:

$ nano test_python_script.sh

The above command launches the Nano text editor. Type the contents of the scrip in a nano text editor:

#!/usr/bin/python3
print(“Hello World”)
print(“Sample python .sh script”)

Where, “#!/bin/bash” shell interpreter will be used, i.e., bash. “#!” indicates the start of the script and is called shebang. The shebang/hashbang directs the shell toward the interpreter. To find the location of the Python interpreter, run the following command:

The above file will display “Hello World” and “Sample python .sh script”. Press Ctrl+O and Ctrl+X to Save and exit the Nano editor, respectively.

To verify the creation of the script, run the list command:

The output indicates the successful creation of the test_python_script.sh script. Additionally, it can be seen that the script test_python_script.sh has file permissions -rw-rw-r–, i.e., it is not executable for any user group. To run the script, the script test_python_script.sh is made executable by executing the following command:

$ chmod +x test_python_script.sh

Where “+x” adds permission to a file:

The above output indicates that the file permission of test_python_script.sh is changed from -rw-rw-r– to -rwxrwxr-x, i.e., the file execution permission is enabled for everyone, i.e., group, user/owner, etc. To run the test_python_script.sh file by executing the following command:


The output indicates the successful execution of the test_python_script.sh script as it prints “Hello World” and “Sample python .sh script”.

How to Run/Execute Python Script via Interactive Mode?

Python interpreter is pre-installed in Ubuntu 22.04. To access Python interactive mode, execute the following command in the Terminal:


Python scripts are executed line by line in sequence in the interactive mode. For example,
>>> print(“Hello World”)

Another example, of running a python code via interactive mode can be seen below:

num1=5
num2=8
If num1<num2:
print (“num2 is greater than num1”)
else:
print(“num1 is greater than num2”)

To exit interactive mode, press exit():

How to Run/Execute Python Script via Text Editor?

Gedit is the default text editor of Ubuntu 22.04 that can be launched by using the application launcher. First, press the “Applications” button in the left bottom corner of the screen, then type “gedit” in the search bar and then select the “Text Editor” icon:

Type python code in the editor. Then, to enable color coding, select “Python! from the bottom as shown below:

To save the script, press the “Save” button:

To save the script, first select the path, then type the name of the script in the name tab followed by pressing the save button:

To enable the Python console, press the Menu button in the top right and select Preferences from the drop-down menu:

This will launch the Preferences pop-up window. Click the Plugins Tab and check mark Python Console and External Tools checkbox

To display the console, again click on the Menu button and press View from the drop-down menu:

Check mark the Bottom Panel. This will enable the Python console as shown below:

Next, to set a short-cut (Ctrl+R) to run the Python script, press the Menu button in the right corner of the screen and Manage External Tools from the drop-down menu:

This will launch the “Manage External Tools” pop-up window. Press the + button to add a tool. Rename the tool to Run(ctrl+R ) and enter the code in the right-hand section of the screen:

#!/usr/bin/env python3
import sys
exec(sys.stdin.read())

And Ctrl+R in the Shortcut key tab in the right-bottom section and close the Manage External Tools.

Now, the short-cut Ctrl+R is enabled. Next, we can run any script by pressing Ctrl+R to run the code:

The output of the script can be seen in the Python console in the bottom section. Other third-party text editors such as Sublime Text, Visual Studio Code, etc can also be used to run Python scripts.

How to Run/Execute Python Script via IDE?

Thonny is a cross-platform Python IDE that can be launched by using the application launcher. First, press the “Applications” button in the left bottom corner of the screen, then type “Thonny” in the search bar, and then select the “Thonny” icon:

To open a Python script, click the Open button at the top-left corner of the screen. This will launch the “Open File” pop-up window. Select the script hello.py and press OK:

This will launch the hello.py script. Click on the Play button:

The output will be displayed on the console:

Other IDEs such as Visual Studio Code, PyCharm, etc can also be used to run Python scripts.

How to Run/Execute Python Script via Jupyter Notebooks?

Python scripts can also run by using a web browser via Jupyter Notebooks. Jupyter Notebooks enables a user to include Python scripts, along with pictures, text, and images in a single document. To launch Jupyter Notebooks, run the link from any web browser, for example, Google Chrome, Firefox, etc and press “Try it in your browser”:

This will redirect you to the “Try Jupyter” page. Then, click “Jupyter Notebook”:

To open a new Notebook, click “File”, followed by “New” and “Notebook”:

Then press “Select” to enable a web-based interactive environment:

Jupiter Notebooks enables a user to type and run code in code blocks. Each code block is executed by pressing the key combination [Ctrl+Shift].

 

Conclusion

Python script can run via command line, interactive mode, text editors, and via Integrated Development Environment (IDE). To execute Python scripts via the command line, simply use the “python3 <script.py>” command. Alternatively, to run bash-style Python scripts, first convert the .sh script to an executable and then, run the “./<test_python_script.sh>” command.

To execute Python scripts in interactive mode, users need to run the code line by line in sequence. Furthermore, python scripts can be executed in Text editors and IDE by using their respective consoles and by Jupyter Notebooks web-based interactive environment. This article demonstrated different methods of running Python scripts in Ubuntu 22.04 LTS.

Print Friendly, PDF & Email