如何运⾏您的Python脚本
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!
作为Python开发⼈员,您需要构建的最重要的技能之⼀就是能够运⾏Python脚本和代码。 这将是您知道代码是否按计划⼯作的唯⼀⽅法。 它甚⾄是知道您的代码是否完全有效的唯⼀⽅法!
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.
本分步指南将根据您的环境,平台,需求和程序员的技能,指导您完成⼀系列运⾏Python脚本的⽅法。
You’ll have the opportunity to learn how to run Python scripts by using:
您将有机会使⽤以下⽅法来学习如何运⾏Python脚本:
The operating system command-line or terminal
The Python interactive mode
The IDE or text editor you like best
The file manager of your system, by double-clicking on the icon of your script
操作系统命令⾏或终端
Python互动模式
您最喜欢的IDE或⽂本编辑器
双击脚本图标,即可创建系统⽂件管理器
This way, you’ll get the knowledge and skills you’ll need to make your development cycle more productive and flexible.
这样,您将获得使开发周期更加⾼效和灵活所需的知识和技能。
Free Bonus: that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
免费红利: 通过简单的⽰例向您展⽰了Python的最佳实践,您可以⽴即应⽤这些⽰例编写更精美的Py
thonic代码。
脚本与模块 (Scripts vs Modules)
In 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.
旨在由⽤户直接执⾏的包含Python代码的纯⽂本⽂件通常称为script ,这是⼀个⾮正式术语,表⽰顶级程序⽂件 。
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.
另⼀⽅⾯,包含设计⽤于从另⼀个Python⽂件导⼊和使⽤的Python代码的纯⽂本⽂件称为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.
⽆论哪种情况,重要的是要知道如何运⾏您写⼊模块和脚本中的Python代码。
什么是Python解释器? (What’s the Python Interpreter?)
Python is an excellent programming language that allows you to be productive in a .
Python是⼀种出⾊的编程语⾔,可让您在⾼效 。
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.
Python还是称为解释器的软件。 解释器是运⾏Python代码和脚本所需的程序。 从技术上讲,解释器是⼀层软件,可以在您的程序和计算机硬件之间运⾏,以使代码运⾏。
Depending on the Python implementation you use, the interpreter can be:
根据您使⽤的Python实现,解释器可以是:
A program written in C, like , which is the core implementation of the language
A program written in Java, like
A program written in Python itself, like
A program implemented in .NET, like
⽤C编写的程序,例如 ,是该语⾔的核⼼实现
⽤Java编写的程序,例如
⽤Python本⾝编写的程序,例如
在.NET中实现的程序,例如
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 .
⽆论解释器采⽤哪种形式,您编写的代码将始终由该程序运⾏。 因此,能够运⾏Python脚本的⾸要条件是 。
The interpreter is able to run Python code in two different ways:
解释器能够以两种不同的⽅式运⾏Python代码:
As a script or module
As a piece of code typed into an interactive session
作为脚本或模块
作为⼀段代码输⼊到交互式会话中
如何以交互⽅式运⾏Python代码 (How to Run Python Code Interactively)
A 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 python, or python3 depending on your Python installation, and then hit Enter.
运⾏Python代码的⼀种⼴泛使⽤的⽅式是通过交互式会话。 要启动Python交互式会话,只需打开命令⾏或终端,然后根据您的Python安装键⼊python或python3 ,然后按Enter即可 。
Here’s an example of how to do this on Linux:
这是如何在Linux上执⾏此操作的⽰例:
$ python3
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Type "help", "copyright", "credits" or "license" for more information.
>>>
> >>
The standard prompt for the interactive mode is >>>, so as soon as you see these characters, you’ll know you are in.
交互式模式的标准提⽰是>>> ,因此⼀旦您看到这些字符,便会知道⾃⼰在其中。
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.
现在,您可以根据需要编写和运⾏Python代码,唯⼀的缺点是,当关闭会话时,您的代码将消失。
When you work interactively, every expression and statement you type in is evaluated and executed immediately:
当您进⾏交互⼯作时,您输⼊的每个表达式和语句都将⽴即求值并执⾏:
>>>
>>>
>>>  print ( 'Hello World!' )
Hello World!
>>>  2 + 5
7
>>>  print ( 'Welcome to Real Python!' )
Welcome to Real Python!
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.
交互式会话将使您能够测试您编写的每段代码,这使其成为⼀个了不起的开发⼯具,并且是尝试使⽤该语⾔并即时测试Python代码的绝佳场所。
python虚拟机To exit interactive mode, you can use one of the following options:
要退出交互模式,可以使⽤以下选项之⼀:
quit() or exit(), which are built-in functions
The Ctrl+Z and Enter key combination on Windows, or just Ctrl+D on Unix-like systems
quit()或exit() ,它们是内置函数
在Windows上按Ctrl + Z和Enter键组合,或者在类似Unix的系统上仅按Ctrl + D
Note: The first rule of thumb to remember when using Python is that if you’re in doubt about what a piece of Python code does, then launch an interactive session and try it out to see what happens.
注意:使⽤Python时要记住的第⼀条经验法则是,如果您不确定⼀段Python代码的功能,请启动⼀个交互式会话并尝试⼀下以查看会发⽣什么。
If you’ve never worked with the command-line or terminal, then you can try this:
如果您从未使⽤过命令⾏或终端,则可以尝试以下操作:
On Windows, the command-line is usually known as command prompt or MS-DOS console, and it is a program The path to this program can vary significantly from one system version to another.
A quick way to get access to it is by pressing the Win+R key combination, which will take you to the Run dialog.
Once you’re there, type in cmd and press Enter.
On GNU/Linux (and other Unixes), there are several applications that give you access to the system command-line.
Some of the most popular are xterm, Gnome Terminal, and Konsole. These are tools that run a shell or terminal like Bash, ksh, csh, and so on.
In this case, the path to these applications is much more varied and depends on the distribution and even on the desktop environment you use. So, you’ll need to read your system documentation.
On Mac OS X, you can access the system terminal from Applications → Utilities → Terminal.
在Windows上,命令⾏通常称为命令提⽰符或MS-DOS控制台,它是⼀个名为的程序。 从⼀个系统版本到另⼀个系统版本,此程序的路径可能会有很⼤不同。
快速访问它的⽅法是按Win + R组合键,这将带您进⼊“运⾏”对话框。 cmd , 输⼊cmd并按Enter 。
在GNU / Linux(和其他Unix)上,有⼏个应⽤程序可让您访问系统命令⾏。 最受欢迎的是xterm,Gnome Terminal和Konsole。
这些是运⾏Shell或终端程序(如Bash,ksh,csh等)的⼯具。
在这种情况下,通向这些应⽤程序的路径会更加多种多样,并取决于分布甚⾄您使⽤的桌⾯环境。 因此,您需要阅读系统⽂档。
在Mac OS X上,您可以从应⽤程序→实⽤程序→终端访问系统终端。
⼝译员如何运⾏Python脚本? (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:
当您尝试运⾏Python脚本时,将开始⼀个多步骤过程。 在此过程中,⼝译员将:
1. Process the statements of your script in a sequential fashion
2. Compile the source code to an intermediate format known as bytecode
This bytecode is a translation of the code into a lower-level language that’s platform-independent. Its purpose is to optimize code execution. So, the next time the interpreter runs your code, it’ll bypass this compilation step.
Strictly speaking, this code optimization is only for modules (imported files), not for executable scripts.
3. Ship off the code for execution
At this point, something known as a Python Virtual Machine (PVM) comes into action. The PVM is the runtime engine of Python. It is a cycle that iterates over the instructions of your bytecode to run them one by one.
The PVM is not an isolated component of Python. It’s just part of the Python system you’ve installed on your machine. Technically, the PVM is the last step of what is called the Python interpreter.
1. 以顺序⽅式处理脚本的语句
2. 将源代码编译为称为字节码的中间格式
该字节码是将代码翻译成与平台⽆关的较低级语⾔的⽅法。 其⽬的是优化代码执⾏。 因此,下次解释器运⾏您的代码时,它将绕过此编译步骤。
严格来说,此代码优化仅适⽤于模块(导⼊的⽂件),⽽不适⽤于可执⾏脚本。
3. 交付代码以执⾏
此时,称为Python虚拟机(PVM)的东西开始起作⽤。 PVM是Python的运⾏时引擎。 这是⼀个循环,遍历字节码的指令以逐⼀运⾏它们。
PVM不是Python的独⽴组件。 它只是您安装在计算机上的Python系统的⼀部分。 从技术上讲,PVM是所谓的Python解释器的最后⼀步。
The whole process to run Python scripts is known as the Python Execution Model.
运⾏Python脚本的整个过程称为Python执⾏模型 。
Note: This description of the Python Execution Model corresponds to the core implementation of the language, that is, CPython. As this is not a language requirement, it may be subject to future changes.
注意: Python执⾏模型的此描述对应于该语⾔的核⼼实现,即CPython。 由于这不是语⾔要求,因此可能会随时更改。
如何使⽤命令⾏运⾏Python脚本 (How to Run Python Scripts Using the Command-Line)
A 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 .py extension. (On Windows systems the extension can also be .pyw.)
Python交互式会话将允许您编写许多代码⾏,但是⼀旦关闭会话,您将丢失编写的所有内容。 这就是为什么编写Python程序的通常⽅法是使⽤纯⽂本⽂件。 按照约定,这些⽂件将使⽤.py扩展名。 (在Windows系统上,扩展名也可以是.pyw 。)
Python code files can be created with any plain text editor. If you are new to Python programming, you can try , which is a powerful and easy-to-use editor, but you can use any editor you like.
可以使⽤任何纯⽂本编辑器创建Python代码⽂件。 如果您不 Python编程,可以尝试 ,它是⼀个功能强⼤且易于使⽤的编辑器,但是您可以使⽤任何喜欢的编辑器。
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 hello.py. With the test script ready, you can continue reading.
将⽂件保存在您的⼯作⽬录中,名称为hello.py 。 准备好测试脚本后,您可以继续阅读。
使⽤python命令 (Using the python Command)
To run Python scripts with the python command, you need to open a command-line and type in the word python, or python3 if you have both versions, followed by the path to your script, just like this:
要使⽤python命令运⾏Python脚本,您需要打开命令⾏并输⼊python或python3如果您同时拥有这两个版本),然后输⼊脚本路径,如下所⽰:
$ python3 hello.py
$ python3 hello.py
Hello World!
Hello World!
If everything works okay, after you press Enter, you’ll see the phrase Hello World! on your screen. That’s it! You’ve just run your first Python script!
如果⼀切正常,请按Enter键 ,您将看到短语Hello World! 在屏幕上。 ⽽已! 您刚刚运⾏了第⼀个Python脚本!
If this doesn’t work right, maybe you’ll need to check your system PATH, your Python installation, the way you created the hello.py script, the place where you saved it, and so on.
如果此⽅法⽆法正常⼯作,则可能需要检查系统PATH ,Python安装, hello.py脚本的创建⽅式,保存位置等。
This is the most basic and practical way to run Python scripts.
这是运⾏Python脚本的最基本,最实⽤的⽅法。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。