Script to run python program

How to Run a Python Script

Python is a well known high-level programming language. The Python script is basically a file containing code written in Python. The file containing python script has the extension ‘ .py ’ or can also have the extension ‘ .pyw ’ if it is being run on a windows machine. To run a python script, we need a python interpreter that needs to be downloaded and installed.

Here is a simple python script to print ‘ Hello World! ’:

Here, the ‘ print() ’ function is to print out any text written within the parenthesis. We can write the text that we want to be printed using either a single quote as shown in the above script or a double quote.

If you are coming from any other language then you will also notice that there is no semicolon at the end of the statement as with Python, you no need to specify the end of the line. And also we don’t need to include or import any files to run a simple python script.

Читайте также:  Html the default style

There is more than one way to run a python script but before going toward the different ways to run a python script, we first have to check whether a python interpreter is installed on the system or not. So in windows, open ‘cmd’ (Command Prompt) and type the following command.

This command will give the version number of the Python interpreter installed or will display an error if otherwise.

Different ways to run Python Script

Here are the ways with which we can run a Python script.

  1. Interactive Mode
  2. Command Line
  3. Text Editor (VS Code)
  4. IDE (PyCharm)
  1. Interactive Mode:
    In Interactive Mode, you can run your script line by line in a sequence. To enter in an interactive mode, you will have to open Command Prompt on your windows machine and type ‘ python ’ and press Enter . Example 1:
    Run the following line in the interactive mode:

    Output:

    Output: Example 3:
    Run the following line one by one in the interactive mode:

    Output:Note: To exit from this mode, press ‘ Ctrl+Z ’ and then press ‘Enter’ or type ‘ exit() ’ and then press Enter .
    Command Line
    To run a Python script store in a ‘ .py ’ file in command line, we have to write ‘ python ’ keyword before the file name in the command prompt.

    You can write your own file name in place of ‘hello.py’. Output:

  2. Text Editor (VS Code)
    To run Python script on a text editor like VS Code (Visual Studio Code) then you will have to do the following:
    • Go in the extension section or press ‘ Ctrl+Shift+X ’ on windows, then search and install the extension named ‘ Python ’ and ‘ Code Runner ’. Restart your vs code after that.
    • Now, create a new file with the name ‘hello.py’ and write the below code in it:

    Output:

  3. IDE (PyCharm)
    To run Python script on a IDE (Integrated Development Environment) like PyCharm, you will have to do the following:
    • Create a new project.
    • Give a name to that project as ‘GfG’ and click on Create.
    • Select the root directory with the project name we specified in the last step. Right click on it, go in New and click on ‘Python file’ option. Then give the name of the file as ‘hello’ (you can specify any name as per your project requirement). This will create a ‘hello.py’ file in the project root directory.
      Note: You don’t have to specify the extension as it will take it automatically.
    • Now write the below Python script to print the message:

    Источник

    How to Run a Python Script in Windows

    When it comes to operating systems, there are three main ones for personal computers: Microsoft Windows, Apple’s macOS, and Linux. You can use any of these operating systems to develop and run your Python programs. However, there are subtle differences you need to keep in mind while developing your projects. In this article, we’ll outline how to run a Python script in Windows.

    In general, we recommend you work with Python 3, which is the latest version. If you need some help getting Python installed, check out our article How to Install Python on Windows. To make sure you’re up to speed with the terminology we use in this article, take a look at Python Terms Beginners Should Know – Part 1.

    If you’re new to programming, a good place to start is our Python Basics track, which contains three useful courses to teach you the fundamentals. We have more course suggestions in the article How to Learn Python Online for Free.

    Your First Python Script

    To follow along with this article, you’ll need to create your own Python script. This script doesn’t have to be fancy – a ‘Hello, World!’ program will do. Simply open a text editor (such as Notepad) and type:

    Just make sure there aren’t any spaces before the print() statement. Then save it as ‘script.py’.

    Run Python Code from the Command Prompt

    The first way to work with Python in Windows is through an interactive session. To get an interactive session started, just open the Command Prompt. Search for cmd on the toolbar, then hit Enter. Once the command prompt is open, simply type python and hit Enter again.

    When you’re in an interactive session, every Python statement is executed immediately and any output is displayed directly beneath. Try typing:

    >>> print('Hello, World!') Hello, World!

    From here, you can write any Python code you want. To exit the interactive session, type quit() or exit() .

    To run the script that we created above, you can call the Python program from the Command Prompt and tell it which file to execute. From the Command Prompt, simply type:

    You’ll see the Hello, World! output printed directly to the screen. For more details on this method, check out How to Open and Run Python Files in the Terminal.

    This is the most basic method of executing scripts that you should be familiar with. You could develop your whole project in Notepad and run the script in this way. This is totally legit, but for many applications it’s not the easiest method.

    Run Python Code from an IDE

    A much more useful way to develop and run your Python projects is using an Integrated Development Environment (IDE). For some background reading, check out our article 4 Best Python IDE and Code Editors. My personal recommendation is to use Spyder (which has a nice mix of features and is user-friendly) but any IDE will do. You can download Spyder here.

    Once you’ve downloaded and installed it, open Spyder to get started. Since you already have your script written, you just need to open it in the editor. To run the script, simply click the play button in the top toolbar. You should see the output displayed in the interactive console.

    Using an IDE to develop and run your Python projects is the most convenient way to work.

    Run a Python Script from the File Manager

    The final way of running your Python script that we’ll discuss is double-clicking the .py file. This is possibly the least useful option, but it could be convenient once you have fully developed and tested your project. It could also require a little bit of configuration.

    You need to ensure your .py file is associated with python.exe. Right click the .py file, select Open with > Python. Now try double-clicking the .py file. You’ll see the Command Prompt flash briefly, then close again. To avoid this, you can add the following line to the end of your script:

    input('Press Enter to Continue')

    Now the script will stop when you hit the Enter key.

    Running Python Scripts in Windows: Next Steps

    In this article, we discussed three ways to run your Python scripts in Windows. This is something you’ll need to do often to develop and test your programs.

    Learning anything new can be challenging. For some tips on the best ways to learn Python, check out our article 5 Tips for Learning Python from Scratch. With a little bit of consistent effort, you’ll become a Python master in no time!

    Источник

    How to Run Python Program?

    Python Certification Course: Master the essentials

    In this step by step tutorial we are going to get an in-depth view of how to run any Python Program. We will cover every possible way to run our code seamlessly depending on various factors such as our coding environment, platform and needs.

    Introduction

    Do you know what is the most important step after writing any code? Yes, obviously to compile, execute and test the code! It is the only way to be sure if our code works at all, and whether we get the desired output.But since Python is an interpreted language, we can skip the compilation part and run the python script/file directly.

    To run a Python Script,first let’s understand what is a Python Script —

    Script:

    • It is a plain text file containing Python code.
    • It has the extension .py or .pyw
    • It can be executed directly.
    • Python interpreter is responsible for executing the Python scripts.

    So, what is an Interpreter? Let’s see —

    Interpreter:

    • It is a program which is needed to run Python Script or code.
    • It is mandatory for the Python Interpreter to be installed into one’s system to run the code.
    • It can run a Python code in two ways:
      1. As a Script or Module
      2. As a piece of code written in an interactive session

    Different Ways to Run Python Program

    There are various ways to run a Python Program, let us see them —

    1. The Python Interactive Mode

    This is the most frequently used way to run a Python code. Here basically we do the following:

    1. Open command prompt / terminal in your system
    2. Enter the command — python or python3 (based on python version installed in your system)
    3. If we see these : >>> then, we are into our python prompt. It will look something like this:

    Now we can start writing our code here.

    1. Best for testing every single line of code written.
    2. In this approach, each line of code is evaluated and executed immediately as soon as we hit ↵ Enter.
    3. Great deveopment tool for experimentation of Python code on the way.
    1. The code is gone as soon as we close the terminal(or the interactive session)
    2. Not user friendly way to write long code.

    To close the interactive session we can:

    1. Type quit() or exit()
    2. Press ctrl + z and hit ↵ Enter for windows. For linux, we can press ctrl+d .

    2. Using the Python Command Line

    Follow the below steps to run a Python Code through command line —

    1. Write your Python code into any text editor.
    2. Save it with the extension .py into a suitable directory
    1. Open the terminal or command prompt.
    2. Type python/python3 (based on installation) followed by the file name.py —

    Example 1: Saved with hello.py

    Example 2: Saved with factorial.py

    Note: If it doesn’t runs, then re-check your Python installation path & the place you saved your Python file.

    3. Run Python on Text Editor

    If you want to run your Python code into a text editor — suppose VS Code, then follow the below steps:

    1. From extensions section, find and install ‘Python’ extension. Install Python Extension
    2. Restart your VS code to make sure the extension is installed properly
    3. Create a new file, type your Python code and save it. Create Python File
    4. Run it using the VS code’s terminal by typing the command «py hello.py» PY Hello in Python
    5. Or, Right Click -> Run Python file in terminal Run Python File in Terminal

    Python File Output

    Example 1: Factorial of a number:

    Python Facorial File

    Python Facorial File Output

    OUTPUT:

    4. Run Python on IDLE

    Python installation comes with an Integrated Development and Learning Environment, which is popularly known as IDLE. Though Python IDLE are by-default included with Python installations on Windows and Mac, if you’re a Linux user, then you can easily download Python IDLE using your package manager.

    Steps to run a python program on IDLE:

    1. Open the Python IDLE(navigate to the path where Python is installed, open IDLE(python version))
    2. The shell is the default mode of operation for Python IDLE. So, the first thing you will see is the Python Shell — Python IDLE
    3. You can write any code here and press enter to execute Execute python IDLE code
    4. To run a Python script you can go to File -> New FileRun Python Script
    5. Now, write your code and save the file — Write Python Code

    Save Python Code6. Once saved, you can run your code from Run -> Run Module Or simply by pressing F5Run Python Module

    Python IDLE Output

    OUTPUT:

    5. Run Python On IDE (PyCharm)

    1. In the Project tool window, select the project root, right-click it, and select File -> New -> Python File Python IDE
    2. Then you will see a prompt, select the option Python File from the menu, and then type the new filename. Python IDE File Name
    3. Write your code in the Python file you have just created. Python IDE Code
    4. To run this file, Right click on the file name -> Run filenameRun Python IDE File
    5. PyCharm executes your code in the Run tool window. Check your output there: Output of code

    example code

    Example 1:

    output of code

    OUTPUT:

    Conclusion

    Congratulations on running your first script in Python! Let’s get an overview of what you learn throughout this article :

    • Script & Interpreter
    • Different ways to run Python Script through :
      1. The Python interactive mode
      2. Using the Python command line
      3. Run on Text Editor
      4. Run on IDLE
      5. Run On IDE (PyCharm)
    • We also saw detailed examples for each of them.

    Источник

Оцените статью