Python is an interpreter language. It means it executes the code line by line. Python provides a Python Shell, which is used to execute a single Python command and display the result. Show
It is also known as REPL (Read, Evaluate, Print, Loop), where it reads the command, evaluates the command, prints the result, and loop it back to read the command again. To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write Now, you can enter a single statement and get the result. For example, enter a simple expression like Execute Python Script As you have seen above, Python Shell executes a single statement. To execute multiple
statements, create a Python file with extension For example, enter the following statement in a text editor such as Notepad.
Save it as Thus, you can execute Python expressions and commands using Python REPL to quickly execute Python code. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Running Python Scripts One of the most important skills you need to build as a Python developer is to be able to run Python scripts and code. This is going to be the only way for you to know if your code works as you planned. It’s even the only way of knowing if your code works at all! This step-by-step tutorial will guide you through a series of ways to run Python scripts, depending on your environment, platform, needs, and skills as a programmer. You’ll have the opportunity to learn how to run Python scripts by using:
This way, you’ll get the knowledge and skills you’ll need to make your development cycle more productive and flexible. Scripts vs ModulesIn computing, the word script is used to refer to a file containing a logical sequence of orders or a batch processing file. This is usually a simple program, stored in a plain text file. Scripts are always processed by some kind of interpreter, which is responsible for executing each command sequentially. A plain text file containing Python code that is intended to be directly executed by the user is usually called script, which is an informal term that means top-level program file. On the other hand, a plain text file, which contains Python code that is designed to be imported and used from another Python file, is called module. So, the main difference between a module and a script is that modules are meant to be imported, while scripts are made to be directly executed. In either case, the important thing is to know how to run the Python code you write into your modules and scripts. What’s the Python Interpreter?Python is an excellent programming language that allows you to be productive in a wide variety of fields. Python is also a piece of software called an interpreter. The interpreter is the program you’ll need to run Python code and scripts. Technically, the interpreter is a layer of software that works between your program and your computer hardware to get your code running. Depending on the Python implementation you use, the interpreter can be:
Whatever form the interpreter takes, the code you write will always be run by this program. Therefore, the first condition to be able to run Python scripts is to have the interpreter correctly installed on your system. The interpreter is able to run Python code in two different ways:
How to Run Python Code InteractivelyA widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in Here’s an example of how to do this on Linux:
The standard
prompt for the interactive mode is Now, you can write and run Python code as you wish, with the only drawback being that when you close the session, your code will be gone. When you work interactively, every expression and statement you type in is evaluated and executed immediately: >>>
An interactive session will allow you to test every piece of code you write, which makes it an awesome development tool and an excellent place to experiment with the language and test Python code on the fly. To exit interactive mode, you can use one of the following options:
If you’ve never worked with the command-line or terminal, then you can try this:
How Does the Interpreter Run Python Scripts?When you try to run Python scripts, a multi-step process begins. In this process the interpreter will:
The whole process to run Python scripts is known as the Python Execution Model. How to Run Python Scripts Using the Command-LineA Python interactive session will allow you to write a lot of lines of code, but once you close the session, you lose everything you’ve written. That’s why the usual way of writing Python programs is by using plain text files. By convention, those files will use the Python code files can be created with any plain text editor. If you are new to Python programming, you can try Sublime Text, which is a powerful and easy-to-use editor, but you can use any editor you like. To keep moving forward in this tutorial, you’ll need to create a test script. Open your favorite text editor and write the following code:
Save the file in your
working directory with the name Using the python CommandTo run Python scripts with the
If everything works okay, after you press Enter, you’ll see the phrase If this doesn’t work right, maybe you’ll need to check your system This is the most basic and practical way to run Python scripts. Redirecting the OutputSometimes it’s useful to save the output of a script for later analysis. Here’s how you can do that:
This operation redirects the output of your script to If Finally, if you want to add the output of consecutive executions to the end of
Now, the output will be appended to the
end of Running Modules With the -m OptionPython offers a series of command-line options that you can use according to your needs. For example, if you want to run a Python module, you can use the command The
Using the Script FilenameOn recent versions of Windows, it is possible to run Python scripts by simply entering the name of the file containing the code at the command prompt:
This is possible because Windows uses the system registry and the file association to determine which program to use for running a particular file. On Unix-like systems, such as GNU/Linux, you can achieve something similar. You’ll only have to add a first line with the text For Python, this is a simple comment, but for the operating system, this line indicates what program must be used to run the file. This line begins with the There are two ways to specify the path to the interpreter:
This last option is useful if you bear in mind that not all Unix-like systems locate the interpreter in the same place. Finally, to execute a script like this one, you need to assign execution permissions to it and then type in the filename at the command-line. Here’s an example of how to do this:
With execution permissions and the shebang line properly configured, you can run the script by simply typing its filename at the command-line. Finally, you need to note that if your script isn’t located at your current working directory, you’ll have to use the file path for this method to work correctly. How to Run Python Scripts InteractivelyIt is also possible to run Python scripts and modules from an interactive session. This option offers you a variety of possibilities. Taking Advantage of importWhen you import a module, what really happens is that you load its contents for later access and use. The interesting thing about this process is that When the module contains only classes, functions, variables, and constants definitions, you probably won’t be aware that the code was actually run, but when the module includes calls to functions, methods, or other statements that generate visible results, then you’ll witness its execution. This provides you with another option to run Python scripts: >>>
You’ll have to
note that this option works only once per session. After the first >>>
These two There are some requirements for this method to work:
To know what’s in your current PMSP, you can run the following code: >>>
Running this code, you’ll get the list of directories and Using importlib and impIn the Python Standard Library, you can find
With >>>
Once you’ve imported a module for the first time, you won’t be able to continue using >>>
An important point to note here is that the argument of >>>
If you use a string as an argument, then
Finally, if you are using Python 2.x, then you’ll have >>>
In Python 2.x, Using runpy.run_module() and runpy.run_path()The Standard Library includes a module called Here’s an example of how you can use it: >>>
The module is located using a standard The first argument
of On the other hand, >>>
Like The
Hacking exec()So far, you’ve seen the most commonly used ways to run Python scripts. In this section, you’ll see how to do that by using
>>>
This statement opens The above example is a little bit out there. It’s just a “hack” that shows you how versatile and flexible Python can be. Using execfile() (Python 2.x Only)If you prefer to use Python 2.x, you can use a built-in function called The first argument of >>>
Here, How to Run Python Scripts From an IDE or a Text EditorWhen developing larger and more complex applications, it is recommended that you use an integrated development environment (IDE) or an advanced text editor. Most of these programs offer the possibility of running your scripts from inside the environment itself. It is common for them to include a Run or Build command, which is usually available from the tool bar or from the main menu. Python’s standard distribution includes IDLE as the default IDE, and you can use it to write, debug, modify, and run your modules and scripts. Other IDEs such as Eclipse-PyDev, PyCharm, Eric, and NetBeans also allow you to run Python scripts from inside the environment. Advanced text editors like Sublime Text and Visual Studio Code also allow you to run your scripts. To grasp the details of how to run Python scripts from your preferred IDE or editor, you can take a look at its documentation. How to Run Python Scripts From a File ManagerRunning a script by double-clicking on its icon in a file manager is another possible way to run your Python scripts. This option may not be widely used in the development stage, but it may be used when you release your code for production. In order to be able to run your scripts with a double-click, you must satisfy some conditions that will depend on your operating system. Windows, for example, associates the extensions When you have a script with a command-line interface, it is likely that you only see the flash of a black window on your screen. To avoid this annoying situation, you can add a statement like This trick has its drawbacks, though. For example, if your script has any error, the execution will be aborted before reaching the On Unix-like systems, you’ll probably be able to run your scripts by double-clicking on them in your file manager. To achieve this, your script must have execution permissions, and you’ll need to use the shebang trick you’ve already seen. Likewise, you may not see any results on screen when it comes to command-line interface scripts. Because the execution of scripts through double-click has several limitations and depends on many factors (such as the operating system, the file manager, execution permissions, file associations), it is recommended that you see it as a viable option for scripts already debugged and ready to go into production. ConclusionWith the reading of this tutorial, you have acquired the knowledge and skills you need to be able to run Python scripts and code in several ways and in a variety of situations and development environments. You are now able to run Python scripts from:
These skills will make your development process much faster, as well as more productive and flexible. Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Running Python Scripts How do you shell in Python?To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.
Which shell commands can be used on the command line to start the Python interactive shell in Windows?To start the Python language shell (the interactive shell), first open a terminal or command prompt. Then type the command python and press enter.
Which shell is best for Python?Here I present you 4 popular interactive shells for Python: IPython, bpython, DreamPie, and Spyder:. IPython. IPython is an enhanced interactive Python shell. ... . bpython. bpython is a fancy interface to the Python interpreter for Linux, BSD, OS X and Windows. ... . DreamPie. ... . Spyder.. What are the two ways of Python shell?Python has two basic modes: script and interactive. The normal mode is the mode where the scripted and finished . py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory.
|