Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Python Basics6

How to Check Python Version on Linux, Mac & Windows

Uploaded by

Sridhar M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Python Basics6

How to Check Python Version on Linux, Mac & Windows

Uploaded by

Sridhar M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

How to Run Python Scripts: Step by Step Guide


By : Logan Young Updated August 12, 2024

What is the Script in Python?


A script in Python can be defined as a file that consists of Python code or a
program. It ends with an extension as .py

An interpreter can execute a script in two distinct ways, as listed below: –

A script can be executed as a module or as a script itself.


A code that is written in an interactive Python command prompt session
manner.

Table of Content:

How to run Python code and scripts interactively?


Here are steps to do this:

Step 1) The programmer must open the command line in the interactive mode.
Step 2) In the next step, invoke the python interpreter in the command line by
typing the following command: –

Command:

python

Step 3) The programmer can sequentially write Python code and execute them in
the same order.
https://www.guru99.com/run-python-scripts.html 1/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

The following program can be typed in the command line as shown below:

Example: –
Python Code:

python
print ('hello world Guru99')

Output:

hello world Guru99

Let us take another Python code that could be written in the command line
interactively.

Example:
Python Code:

Python
a=25
b=50
if a > b:
... print ("a is more than b")
... else:
... print ("b is more than a")
...
b is more than a
>>>

Output:

https://www.guru99.com/run-python-scripts.html 2/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

b is more than a

Following is the screenshot: –

Explanation:
The above code compares two arguments and finds which argument is greater.
Ensure the statements are properly indented to avoid indentation errors. To exit the
windows commanded line, press Ctrl+Z and follow with an Enter.

How to run Python script files interactively from Windows GUI?


Here are steps to do this:

Step 1) Open the command line and type the Python command followed by the
script file name.

Step 2) Ensure that the script file is already created.


Step 3) The script file should end with either .py or. pyw. As a next step, open the
Windows command prompt.

Following would be the code to be written in the command line to invoke a Python
script:

Python Command line Code:


https://www.guru99.com/run-python-scripts.html 3/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Python example.py

Output:

Hello, world!

RELATED ARTICLES
→ Python Check if File Exists: How to Check If a Directory Exists?
→ Live Python Project: Data Analysis Project
→ Python Matrix: Transpose, Multiplication, NumPy Arrays Examples
→ Mutable & Immutable Objects in Python {EXAMPLES}

How to run Python scripts from an IDE


An integrated development environment is also referred to as IDE in short form. It
offers a virtual setup of file and folder hierarchy. It allows the programmer to build
Python projects quickly. IDE provides the organization of files into different folders.

These different folders belong to one common directory.

Spyder and PyCharm are two Python IDEs that enable the programmer to run IDEs
that enable the programmer to run Python programs.

Let us take an example of executing a Python script in PyCharm IDE:

Step 1) As the first step, create a new project in PyCharm.


Step 2) Name the project as Guru99Example and then click create.
Step 3) Select the Root folder that would be labeled as Guru99Example and do a
right click.
https://www.guru99.com/run-python-scripts.html 4/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Step 4) Go to the new option and create a new Python file.


Step 5) Give the name of the Python file
Here we have given Guru99Example.

Step 6) In the blank Python file created in PyCharm IDE, write the following piece of
code as shown below: –

Python Code:

print ('hello world Guru99')

Step 7) To run the above code, right-click on the editor and select the option “Run
File in Python console”.
This code will open the console window within IDE, and the following would be the
output:

Output:

PyDev console: starting.


Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932
64 bit (AMD64)] on win32
>>> runfile('C:/Users/OS
Name/PycharmProjects/Guru99Example/Guru99Example.py', wdir='C:/Users/OS
Name/PycharmProjects/Guru99Example')
hello world Guru99

Following is the Screenshot:

https://www.guru99.com/run-python-scripts.html 5/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Explanation:
The above code is a simple example of how to print hello world in the PyCharm IDE.

How to run Python scripts using a file manager?


Here are the steps to run the Python script using file manager:

Step 1) Open Notepad and add some Python code.


Step 2) Type print (‘Hello World Guru99’)
https://www.guru99.com/run-python-scripts.html 6/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Step 3) Save the script file with extension as either .py or. pyw.
Step 4) .py extension file is used for programs that have Python.exe.
Step 5) In the windows operating system, right-click the Python file and click open.
Refer to the below screenshot: –

When executed, it opens a command line screen for a moment. Refer to the below
screenshot: –

To ensure the screen remains for a while, add the following statement in the Python
script file as shown below: –

Python Code:

input ('Press provide enter to exit...')

https://www.guru99.com/run-python-scripts.html 7/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Until and unless there is no input from the user, the screen continues to run and
display Python output.

However, if the Python script has some error, the execution of the input command
is interrupted or aborted. Due to this, the user may not be able to see Python script
execution using file manager.

How to run Python scripts using script file name and


command line?
Here are the steps to run the Python script using script file name and command
line:

Step 1) Open Notepad and add some Python code.


Step 2) Type print (‘Hello World Guru99’)
Step 3) Save the script file with extension as either .py or. pyw.
Step 4) .py extension file is used for programs that have Python.exe.

https://www.guru99.com/run-python-scripts.html 8/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Step 5) In the windows operating system, right-click the Python file and click copy
as the path. Refer to the below screenshot: –

It helps to validate the path of the script file with the command line path. Open a
command line and type the script file name as created above with the name
followed by the extension .py. Refer to the below screenshot: –

Alternatively, the script file created above can be run as a python module. Type the
following command as shown below: –

Code:
https://www.guru99.com/run-python-scripts.html 9/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

python -m example

Output:

hello world
Press provides enter to exit...

Refer to the following screenshot:

Explanation:
The above code follows the following syntax:

Syntax:

Python -m

The above -m parameter searches for the sys. Path for the module referred to and
executed the code as part of the _main_.

How to run Python Scripts on Linux


Here are steps to do this:

Step 1) The programmer must open the Linux terminal in the interactive mode.
Step 2) In the next step, invoke the Python interpreter in the Linux terminal by
typing the following command: –
https://www.guru99.com/run-python-scripts.html 10/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Command:

python3

Step 3) The programmer can sequentially write Python code and execute them in
the same order.

The following program can be typed in the command line as shown below:

Example: –
Python Code:

python3
a=25
b=50
if a > b:
... print ("a is more than b")
... else:
... print ("b is more than a")
...
b is more than a
>>>

Output:

b is more than a

Following is the screenshot: –

https://www.guru99.com/run-python-scripts.html 11/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Alternatively, Here are the steps to run the Python script file using a Linux terminal:

Step 1) Open the text editor and add some Python code.
Step 2) Type print (‘Hello World’)
Step 3) Save the script file with extension as.py.
Step 4) .py extension file is used for programs that have Python.exe.
Step 5) In the next step, invoke the Python interpreter in the Linux terminal by
typing the following command: –

Command:

python3 /home/onworks/Desktop/Example.py

Output:

hello world

Refer to the following screenshot: –

https://www.guru99.com/run-python-scripts.html 12/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

Explanation:
The python interpreter in the Linux terminal runs the script file that is saved at the
desktop location.

Comparison of code, module, and script in Python


In the world of computing, the following is the comparison between module, code,
and script:

Parameters Code Module Script


Definition A code is A module is defined as a A script can be
defined as a series of codes arranged defined as a file that
series of sequentially. comprises logical
instructions. orders or a batch of
organized codes.

Executability They are run They are imported first The scripts are
through the in a script or a piece of generally run through
Python code and then executed a Python interpreter.
interpreter using the Python
interpreter

FAQ
❓ What is Python Interpreter?

🏅 Types of Python Interpreters

https://www.guru99.com/run-python-scripts.html 13/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

⚡ How does the Python interpreter execute scripts?

🌟 What Do you Need to Run a Python Script?

Conclusion
A Python script can be executed using several methods.
You can execute using the command line or using IDE.
The programmer can use PyCharm and Spyder IDE to develop sophisticated
Python scripts.
Ensure that the code adheres to indentation when using the if and else
statements in the command line.

You Might Like:

Top
Top Python How to Check
Interview Multithreading
Multithreading in Python Version on
Questions and… Python
Python with… Linux,…

Import module in
Python
Python with
Examples
Examples

Prev Report a Bug Next

https://www.guru99.com/run-python-scripts.html 14/15
12/13/24, 11:32 PM How to Run Python Scripts: Step by Step Guide

About
About Us
Advertise with Us
Contact Us

Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Xero
RemotePC
QuickBooks

Interesting
eBook
Blog
Quiz
SAP eBook
Privacy Manager

English © Copyright - Guru99 2024 Privacy Policy |


Affiliate Disclaimer | ToS | Editorial Policy

https://www.guru99.com/run-python-scripts.html 15/15

You might also like