Python is popular programming and scripting language. This popularity causes different use cases for Python scripts and codes. In this tutorial, we will learn what is a Python script, how to run Python code and script from the command line, GUI, IDe, etc.
Python是流行的编程和脚本语言。 这种流行导致了Python脚本和代码的不同用例。 在本教程中,我们将学习什么是Python脚本,如何从命令行,GUI,IDe等运行Python代码和脚本。
什么是Python脚本? (What Is Python Script?)
The python script is some code written in Python which can be run as an application or from a shell easily. Python scripts have some structures like below.
python脚本是一些用Python编写的代码,可以作为应用程序运行,也可以从shell轻松运行。 Python脚本具有以下类似的结构。
- Import required modules 导入所需的模块
- Define Class, Function and other structures定义类,函数和其他结构
- Execute and run code and script from imported modules, defined classes, and functions从导入的模块,已定义的类和函数执行并运行代码和脚本
示例Python脚本(Example Python Script)
Below we can find an example script which will simply print some text by using functions to the standard output.
在下面我们可以找到一个示例脚本,该脚本将通过使用函数将一些文本简单地输出到标准输出。
import os
def PrintHello(str):
print ("Hello World "+str)
PrintHello("Poftut.com")
We can use the command-line interface or the operating system terminal in order to run and execute Python scripts and code. There is a two-way method to run the Python script. First is using the python interpreter and provide the script file path where the interpreter will read the script file and run it. Second is making the script file executable and running from the command directly providing the script file name like a command or executable.
我们可以使用命令行界面或操作系统终端来运行和执行Python脚本和代码。 有两种方法可以运行Python脚本。 首先是使用python解释器并提供脚本文件路径,解释器将在该路径中读取脚本文件并运行它。 其次是使脚本文件可执行,并从命令运行,直接提供脚本文件名,例如命令或可执行文件。
使用Python解释器运行 (Run with Python Interpreter)
We can use Python interpreter which can be run directly from the command line. For Python2 python2
for Python3 python3
interpreter can be used. also the interpreter should be added to the OS Path or environment variables which are described below for Windows operating