安搭
软件安装搭建教程网是专门为软件开发、IT运维人员提供Apache、Centos、dotNET、Eclipse、Ftp、Git、java、mssqlserver、MySQL、Nginx、php、python、Redis、SVN、Vscode、VUE等服务器服务系统的安装教程、搭建教程、配置教程信息网。

如何使用示例运行/执行Python脚本教程?

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")
What Is Python Script?
What Is Python Script?
什么是Python脚本?

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 python3interpreter can be used. also the interpreter should be added to the OS Path or environment variables which are described below for Windows operating systems. Linux adds path information by default.

我们可以使用可以直接从命令行运行的Python解释器。 对于Python2,可以使用适用于Python3的python2 python3解释器。 还应将解释器添加到以下针对Windows操作系统描述的OS路径或环境变量中。 Linux默认添加路径信息。

$ python2 python_script.py


$ python3 python_script.py


$ python python_script.py
Run with Python Interpreter
Run with Python Interpreter
使用Python解释器运行

运行以使Python脚本文件可执行(Run Making Python Script File Executable)

For Linux Python script files can be made executable. In order to make a script directly executable from the shell, we have added a line which specifies the script type as Python and the python interpreter.

对于Linux,可以将Python脚本文件设为可执行文件。 为了使脚本可以直接从shell执行,我们添加了一行,将脚本类型指定为Python和python解释器。

#!/usr/bin/python

The script file the latest content will be like below.

脚本文件的最新内容将如下所示。

#!/usr/bin/python

import os

def PrintHello(str):
    print ("Hello World "+str)

PrintHello("Poftut.com")
Run Making Python Script File Executable
Run Making Python Script File Executable
运行以使Python脚本文件可执行

Then we will make the script file named python_script.py user executable with the following chmod command. And now we can run the script file directly from the command line by specifying its name.

然后,我们将使用以下chmod命令使名为python_script.py的脚本文件可执行。 现在,我们可以通过指定其名称直接从命令行运行脚本文件。

$ chmod u+x python_script.py 

$ ./python_script.py
Run Making Executable
Run Making Executable
运行可执行文件

从Python交互模式或Python Shell运行Python脚本(Run Python Script From Python Interactive Mode or Python Shell)

Python interpreter is also provided as an interactive shell. Python interactive shell provides the same environment for the Python scripts and programs. We can run a script from the Python shell. directly typing it or copy-paste. First, we will open a Python interactive shell with one of python2, python3 or python commands according to our Python version selection. In this example, we will use python2 shell.

Python解释器也作为交互式外壳提供。 Python交互式外壳为Python脚本和程序提供了相同的环境。 我们可以从Python Shell运行脚本。 直接输入或复制粘贴。 首先,根据我们选择的Python版本,我们将使用python2python3python命令之一打开一个Python交互式外壳。 在此示例中,我们将使用python2 shell。

$ python3

$ python2
Run Python Script From Python Interactive Mode or Python Shell
Run Python Script From Python Interactive Mode or Python Shell
从Python交互模式或Python Shell运行Python脚本

We paste the python script we want to run it will immediately process by the interactive shell-like below.

我们粘贴要运行的python脚本,它将立即由下面的交互式shell进行处理。

了解更多“找不到pip命令”错误解决方案Pip和Pip3对于Linux,Debian,Ubuntu,CentOS,Mint
Run Python Script From Python Interactive Mode or Python Shell
Run Python Script From Python Interactive Mode or Python Shell
从Python交互模式或Python Shell运行Python脚本

像从IDE一样运行Python脚本(Run Python Script From IDE Like)

While deloping simple or complex Python scripts or applications IDE’s can be used. Eclipse, PyCharm, Atom, VSCode and other IDE’s provide directly run the script from IDE and show the output inside the IDE output pane.

在简化简单或复杂的Python脚本或应用程序时,可以使用IDE。 Eclipse,PyCharm,Atom,VSCode和其他IDE可以直接从IDE运行脚本,并在IDE输出窗格中显示输出。

通过双击从文件管理器运行Python脚本 (Run Python Script By Double Click From File Manager)

Another way is using the operating system file explorer and double click to the script file. This can be done operating systems like Linux, Windows, MacOSX but the script file should be executable which is described in Run Making Python Script File Executable .Then double click to the python script file.

另一种方法是使用操作系统文件浏览器,然后双击脚本文件。 这可以在Linux,Windows,MacOSX等操作系统上完成,但是脚本文件应该是可执行文件,如Run Making Python Script File Executable 。然后双击python脚本文件。

翻译自:

文章来源:https://blog.csdn.net/cunjiu9486/article/details/109077628