{"id":22585,"date":"2023-10-27T17:11:32","date_gmt":"2023-10-27T17:11:32","guid":{"rendered":"https:\/\/linuxways.net\/?p=22585"},"modified":"2023-10-27T17:11:32","modified_gmt":"2023-10-27T17:11:32","slug":"how-do-i-run-a-python-script-on-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-do-i-run-a-python-script-on-ubuntu-22-04\/","title":{"rendered":"How do I Run a Python Script on Ubuntu 22.04?"},"content":{"rendered":"<p>Python is a popular and useful Programming Language mainly used for building Software Applications. Apart from Software Applications, Python Programming can be used to carry out automated tasks in an Operating system, it can be used to build Web Applications as well. With Python, specific tasks can be performed by creating custom scripts.<\/p>\n<p>This article discusses how you can run a Python Script on Ubuntu 22.04.<\/p>\n<h2><strong>How do I Run a Python Script on Ubuntu 22.04?<\/strong><\/h2>\n<p>Most of the utilities and programs in Linux are written in Python and for this reason, Python Programming is a preferable way for task automation and running scripts in Ubuntu. Before running Python Scripts in Ubuntu, ensure Python is installed. Install Python by following the <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-python-on-ubuntu-22-04\/\">Install Python on Ubuntu 22.04<\/a> Guide if any previous version is not added or installed in Ubuntu.<\/p>\n<h2><strong>Method 1: Using the python3 Command<\/strong><\/h2>\n<p>The latest Python Version is \u201cpython3\u201d. A Python Script in Ubuntu can be easily run in the Terminal directly with the \u201cpython3\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">python3 <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span>.sh<\/div><\/div>\n<p>In our case, we have the \u201cpythonScript.sh\u201d Script File in the Home Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"498\" class=\"wp-image-22586\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-1.png 900w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-1-300x166.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-1-768x425.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p>To run the \u201cpythonScript.sh\u201d using the Terminal, the \u201cpython3\u201d command will be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">python3 pythonScript.sh<\/div><\/div>\n<p>The Python Script will be executed and the Output can be seen in the Terminal. In our case, the \u201cpythonScript.sh\u201d Python Script prints the string \u201cLinuxWays!\u201d, and can be seen on the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"83\" class=\"wp-image-22587\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-2.png 730w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-2-300x34.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/p>\n<p>Similarly, any other Python Script File can be run with the \u201cpython3\u201d Command in the Ubuntu Terminal.<\/p>\n<h2><strong>Method 2: Using the \u201cchmod\u201d Command<\/strong><\/h2>\n<p>The \u201cchmod\u201d command is a useful way of changing the file permissions. It can also execute a file, i.e. a Python Script File. To run a Python Script using the \u201cchmod\u201d command, you have to make a file with the \u201c.sh\u201d extension. Once the file is created, the \u201cchmod\u201d command can make the \u201c.sh\u201d file executable. In our case, we made the \u201cscript.sh\u201d file with the \u201ctouch\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">touch<\/span> script.sh<\/div><\/div>\n<p>The \u201cscript.sh\u201d file is created in the same Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"742\" height=\"285\" class=\"wp-image-22588\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-3.png 742w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-3-300x115.png 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>Open the newly created \u201cscript.sh\u201d file with the \u201cnano\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> script.sh<\/div><\/div>\n<p>The Text editor for the \u201cscript.sh\u201d file will open:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"485\" class=\"wp-image-22589\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-4.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-4-300x199.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Add the following Python Script in the \u201cscript.sh\u201d File:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#! \/usr\/bin\/env python<\/span><br \/>\n<br \/>\nprint<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Welcome to LinuxWays!&quot;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>The \u201c#! \/usr\/bin\/env\u201d selects the python version to use.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"479\" class=\"wp-image-22590\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-5.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-5-300x194.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/p>\n<p>Once you have added the script to the text editor, press the \u201cctrl+o\u201d keys to write the script and then press the \u201cctrl+x\u201d keys to close the text editor. Now, Before Running the Script File, use the \u201cchmod\u201d command which ensures to make the file executable.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">chmod<\/span> +x <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span>.sh<\/div><\/div>\n<p>In our case, we have the \u201cscript.sh\u201d file, so the \u201cchmod\u201d command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">chmod<\/span> +x script.sh<\/div><\/div>\n<p>The \u201c+x\u201d makes the \u201cscript.sh\u201d file executable. The cursor moves to the next line meaning the file can now be executed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"130\" class=\"wp-image-22591\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-6.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-6-300x53.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<p>The executable file can be run directly in the Terminal with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/&lt;&lt;<\/span>strong<span class=\"sy0\">&gt;<\/span>fileName<span class=\"sy0\">&lt;\/<\/span>strong<span class=\"sy0\">&gt;&gt;<\/span><\/div><\/div>\n<p>In our case, the file name is \u201cscript.sh\u201d. To run the Python Script now, the command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/<\/span>script.sh<\/div><\/div>\n<p>This will display the output of the Script File, in our case \u201cscript.sh\u201d, in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"140\" class=\"wp-image-22592\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-7.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-7-300x57.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>To learn more about making files executable and \u201cchmod\u201d, study the <a href=\"https:\/\/linuxways.net\/de\/linux-commands\/understanding-chmod-x-making-files-executable\/\">chmod +X &#8211; Making Files Executable <\/a>Guide.<\/p>\n<h2><strong>Method 3: Running Python Script With the \u201cpython\u201d Command<\/strong><\/h2>\n<p>The \u201cpython\u201d command can also be used to run a Python Script in Ubuntu. It executes the selected Script File and displays the output in the Terminal:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">python <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the file name is \u201cscript.sh\u201d, as seen in the Home Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"390\" class=\"wp-image-22593\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-8.png 746w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-8-300x157.png 300w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/p>\n<p>To run the \u201cscript.sh\u201d Script File, the \u201cpython\u201d command will be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">python script.sh<\/div><\/div>\n<p>This will display the output of the Script File in the Terminal, in our case, the output \u201cWelcome to LinuxWays!\u201d is displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"77\" class=\"wp-image-22594\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-9.png 724w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22585-9-300x32.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Python Script in Ubuntu can be run by using either the \u201cpython3\u201d Command, the \u201cchmod\u201d method, or by using the \u201cpython\u201d Command in the Terminal. With the \u201cchmod\u201d Command, a file with a \u201c.sh\u201d extension is created first, and then the file permissions are changed to make the file executable. This article explained all the commands to run a Python Script on Ubuntu.<\/p>","protected":false},"excerpt":{"rendered":"<p>Python Script in Ubuntu can be run by using either the \u201cpython3\u201d Command, the \u201cchmod\u201d method, or by using the \u201cpython\u201d Command in the Terminal.<\/p>","protected":false},"author":110,"featured_media":22595,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22585"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22595"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}