Python Basics6
Python Basics6
Table of Content:
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:
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
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.
Step 1) Open the command line and type the Python command followed by the
script file name.
Following would be the code to be written in the command line to invoke a Python
script:
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}
Spyder and PyCharm are two Python IDEs that enable the programmer to run IDEs
that enable the programmer to run Python programs.
Step 6) In the blank Python file created in PyCharm IDE, write the following piece of
code as shown below: –
Python Code:
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:
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.
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:
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.
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...
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_.
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
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
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.
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?
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
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.
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
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
https://www.guru99.com/run-python-scripts.html 15/15