002_Using the Python Interpreter_使用Python解释器

002_Using the Python Interpreter_使用Python解释器,第1张

概述2.1.InvokingtheInterpreter调用解释器ThePythoninterpreterisusuallyinstalledas/usr/local/bin/python3.5onthosemachineswhereitisavailable;putting/usr/local/bininyourUnixshell’ssearchpathmakesitpossibletostartitbytypingthe

2.1. Invoking the Interpreter 调用解释器
The Python interpreter is usually installed as /usr/local/bin/python3.5 on those machines where it is available;
putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by tyPing the command:
在装了python3.5的类Unix风格的系统上,Python解释器通常于/usr/local/bin/python3.5
把/usr/local/bin加入Unix shell的系统搜索路径,之后在终端或字符界面输入以下命令:
python3.5
to the shell.
[1] Since the choice of the directory where the interpreter lives is an installation option, other places are possible;
由于放Python解释器的目录在安装时可以选择自定义,所以也有可能在其他路径
check with your local Python guru or system administrator.
如果不在默认路径,请联系你本地的Python专家或系统管理员
(E.g., /usr/local/python is a popular alternative location.)
比如/usr/local/python就是一个常见的自定义安装位置

On windows machines,
在windows中
the Python installation is usually placed in C:\python35, though you can change this when you’re running the installer.
Python通常安装在C:\python35,不过在运行安装程序时你可以更改安装路径
To add this directory to your path,
把Python的安装目录添加到PATH中
you can type the following command into the command prompt in a DOS Box:
你可以在命令提示符的DOS窗口中输入以下命令:
set path=%path%;C:\python35

TyPing an end-of-file character (Control-D on Unix, Control-Z on windows) at the primary prompt causes the interpreter to exit with a zero exit status.
在主提示符模式下,输入文件结束符()可以让Python解释器正常退出
If that doesn’t work, you can exit the interpreter by tyPing the following command: quit().
如果以上方式没用,你也可以输入quit()来退出解释器

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline.
在支持readline的系统上,Python命令行编辑特性包括:交互式编辑,上下键回溯复制历史Python代码,还有代码补全

Perhaps the quickest check to see whether command line editing is supported is tyPing Control-P to the first Python prompt you get.
也许检查是否支持命令行编辑的最快方式是:在Python提示符里输入Ctrl+P

If it beeps, you have command line editing;
如果响起嘟的一声,表明有命令行编辑
see Appendix Interactive input Editing and History Substitution for an introduction to the keys.
请参阅附录《交互式输入编辑和回溯并复制历史代码》里相关快捷键的介绍
If nothing appears to happen, or if ^P is echoed, command line editing isn’t available;
如果什么都没发生,或者显示^P,则命令行编辑不可用
you’ll only be able to use backspace to remove characters from the current line.
你只能用退格键删除这行字符

The interpreter operates somewhat like the Unix shell:
Python解释器的行为有点像Unix shell
when called with standard input connected to a tty device,
当用连接tty设备的标准输入来调用Python的时候
it reads and executes commands interactively;
它交互地读取并执行命令
when called with a file name argument or with a file as standard input,
当调用时跟着文件名和参数,或者拿一个文件作为标准输入来调用时
it reads and executes a script from that file.
它将读取并执行该文件中的脚本

A second way of starting the interpreter is python -c command [arg] …,
第二种启动解释器的方式是Python -c 命令 [参数] …
which executes the statement(s) in command, analogous to the shell’s -c option.
它会执行命令中的语句,类似于shell的-c选项
Since Python statements often contain spaces or other characters that are special to the shell,
因为Python语句经常包含空格或者其他shell特殊字符
it is usually advised to quote command in its entirety with single quotes.
通常建议把全部命令放在单引号里

Some Python modules are also useful as scripts.
有些python模块也是可执行的脚本
These can be invoked using python -m module [arg] …,
这些模块可以使用python -m 模块 [参数] …直接调用
which executes the source file for module as if you had spelled out its full name on the command line.
这和在命令行输入完整的路径名执行模块的源文件是一样的
When a script file is used,
有时在用一个脚本文件的时候
it is sometimes useful to be able to run the script and enter interactive mode afterwards.
想要在这个脚本跑完之后进入交互模式
This can be done by passing -i before the script.
这可以通过在脚本前面加上-i 选项实现
All command line options are described in Command line and environment.
我们会在后续章节命令行和环境中介绍命令行的所有选项

2.1.1. Argument Passing传递参数
When kNown to the interpreter,
传递给解释器后
the script name and additional arguments thereafter are turned into a List of strings and assigned to the argv variable in the sys module.
脚本名称和名称后面的其他参数被转换成一个字符串列表并赋值给sys模块中的argv变量
You can access this List by executing import sys.
你可以import sys拿到这个列表
The length of the List is at least one;
列表的长度至少是1
when no script and no arguments are given, sys.argv[0] is an empty string.
如果没有给出脚本和任何参数,sys.argv[0]是一个空字符串
When the script name is given as ‘-’ (meaning standard input), sys.argv[0] is set to ‘-’.
当脚本名称指定为‘’(代表标准输入),sys.argv[0]被设为’
When -c command is used, sys.argv[0] is set to ‘-c’.
当使用-c命令时,sys.argv[0]被设为‘-c’
When -m module is used, sys.argv[0] is set to the full name of the located module.
当使用-m模块时,sys.argv[0]被设定为指定模块的全名
Options found after -c command or -m module are not consumed by the Python
Python解释器不会解析-c命令或-m模块后面的选项
interpreter’s option processing but left in sys.
这些选项保存在sys.argv中
argv for the command or module to handle.
供命令或模块使用

2.1.2. Interactive Mode交互模式
When commands are read from a tty, the interpreter is saID to be in interactive mode.
当从tty读取命令时,解释器处于交互模式
In this mode it prompts for the next command with the primary prompt,
这种模式下,解释器以主提示符提示下一个命令
usually three greater-than signs (>>>);
主提示符通常为三个大于号>>>
for continuation lines it prompts with the secondary prompt,
对于命令行中换行后续的行
by default three dots (…).
解释器以 从提示符 提示,默认为三个点…
The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:
在第一个提示符之前,解释器会打印出一条欢迎信息,声明它的版本号和授权公告

$ python3.5
Python 3.5 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

Continuation lines are needed when entering a multi-line construct. As an example,
take a look at this if statement:
当进入多行结构时,需要后续的行,例如这个if语句

the_world_is_flat = True
if the_world_is_flat:
… print(“Be careful not to fall off!”)

Be careful not to fall off!
For more on interactive mode, see Interactive Mode.
更多关于交互模式的信息,请参阅交互模式

2.2. The Interpreter and Its Environment编译器及环境
2.2.1. Source Code EnCoding源程序的编码
By default, Python source files are treated as encoded in UTF-8.
Python源文件默认以UTF-8编码
In that enCoding,
在这种编码下
characters of most languages in the world can be used simultaneously in string literals, IDentifIErs and comments
世界上大多数语言的字符可以在字符串,标识符和注释中使用
— although the standard library only uses ASCII characters for IDentifIErs,
尽管标准库中的标识符只使用ASCII字符
a convention that any portable code should follow.
这是可移植代码应该遵循的一个惯例
To display all these characters properly,
为了能正确显示这一切字符
your editor must recognize that the file is UTF-8,
你的编辑器必须能够识别文件是UTF-8编码
and it must use a Font that supports all the characters in the file.
且必须使用支持文件中所有字符的字体
It is also possible to specify a different enCoding for source files.
也可以给源文件指定一个不同的编码
In order to do this,
为此
put one more special comment line right after the #! line to define the source file enCoding:
可以在#!行之后加一个特殊注释行来定义文件编码

-- Coding: enCoding --
With that declaration, 通过此声明
everything in the source file will be treated as having the enCoding enCoding instead of UTF-8.
源文件中的所有字符将被视为由enCoding指定的方式编码而不是UTF-8编码
The List of possible enCodings can be found in the Python library Reference,
可用编码列表在Python库手册里有
in the section on codecs.
见关于编码器的一节
For example, 例如
if your editor of choice does not support UTF-8 encoded files and insists on using some other enCoding,
如果你选择的编辑器不支持UTF-8编码的文件而只能用其他编码
say windows-1252, you can write:
比如windows-1252,你可以这样写:
-- Coding: cp-1252 --
and still use all characters in the windows-1252 character set in the source files.
然后让源文件中的所有字符用windows-1252字符集
The special enCoding comment must be in the first or second line within the file.
特殊编码注释必须在文件中的第一行或第二行中
Footnotes

[1] On Unix, the Python 3.x interpreter is by default not installed with the executable named python,
so that it does not conflict with a simultaneously installed Python 2.x executable.
在Unix上,为了不在系统还装了Python2的时候和它发生冲突,默认情况下,python3的可执行文件不占用了python这个名字

总结

以上是内存溢出为你收集整理的002_Using the Python Interpreter_使用Python解释器全部内容,希望文章能够帮你解决002_Using the Python Interpreter_使用Python解释器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1184627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存