2. Using the Python Interpreter

2.1. Invoking the Interpreter

【原文】The Python interpreter is usually installed as on those machines where it is available; putting /usr/local/bin/python3.12/usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:

python3.12

to the shell.  [1 ]Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative location.)

【译】Python 解释器通常被安装程序安装在可用的机器上在你的Unix shell's搜索路径/usr/local/bin/python3.12/usr/local/bin键入命令python3.12进入shell。[1]在安装选项可以选择设置解释器的路径,即解释器可以放在其他位置;请咨询您本地的Python大师或系统管理员。(例如,/usr/local/python是常用的替代位置。)

【参考】Unix shell,是一种命令行解释器,用于与操作系统内核进行交互。它提供了一个用户界面,可以通过输入命令来执行各种操作,运行程序、管理文件和目录、配置系统设置等。Unix shell提供了丰富的命令和工具,使用户可以高效的完成任务。

Shell,是一种命令行解释器,它是用户与操作系统内核之间的接口,用户可以通过shell向内核发送命令,并接受来自内核的响应。

【实际操作】

2023年10月9日下载在win10系统安装

www.python.org下载Python安装文件。请注意,Python3.9及以上的版本不能在Windows 7或更早的版本上使用。

用Win 10默认方式安装Python3.12.0,python.exe的路径。
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe。

Customer方式安装,更改安装路径为D:\python。

在Python安装文件夹下键入python,调用Python解释器的命令行窗口。

调用退出python解释器

【原文】On Windows machines where you have installed Python from the Microsoft Store, the python3.12 command will be available. If you have the py.exe launcher installed, you can use the py command. See Excursus: Setting environment variables for other ways to launch Python.

【译】在已从Microsoft Store安装了Python Windows 计算机上,Python3.12命令可用。如果你安装 py.exe launcher,可以使用py命令。具体内容可参阅附录:设置环境变量。了解启动Python的其他方法。

【实际操作】

2023年10月9日的WIN+R运行窗口

打开的是Microsoft Store,python3.11的下载。

【原文】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. If that doesn’t work, you can exit the interpreter by typing the following command:  quit().

在主提示符下键入结束符(Unix系统用Control-D, Windows系统用Control-Z)解释器会以“零退出”状态退出。如果这不起作用,您可以通过quit()命令退出。

【参考】zero exit status:零退出状态,程序正常结束,没有发生错误和异常。

【原文】The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing  Control-P  to the first Python prompt you get. 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; you’ll only be able to use backspace to remove characters from the current line.

【译】在支持 GNU Readline 库的系统上,解释器的在线编辑功能包括交互式编辑、命令行历史和代码补全。您编辑的命令行是否被支持,最快的检查方法是用 Control-P 收到的第一个Python 提示状态。如果它是蜂鸣声,命令行编辑可用;相关内容请参阅附录交互式输入编辑和历史记录替换按键介绍。如果什么都没有发生,或者Control-P是回显,命令行编辑不可用;您只能使用退格键从当前行中删除字符。

【参考】GNU Readline是一个C库,它提供了命令行编辑、自动补全和命令历史回溯功能。这个库可以帮助程序实现用户体验良好的交互式命令行界面(REPL)。

【注】不了解GNU系统,没有实际操作。

【原文】The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, 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.

【译】解释器的操作很多地方和Unix shell类似:当使用标准调用连接到tty设备,它以交互方式读取和执行命令; 当使用文件名参数或文件作为标准输入调用时,它读取文件并从该文件执行脚本。

【参考】tty device,tty设备是终端设备的一种,它由虚拟控制台、串口以及伪终端设备组成。

【原文】A second way of starting the interpreter is python -c command [arg] ...,which executes the statement(s) in command, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety.

【译】启动解释器的第二种方法是 python –c command [arg],它执行命令中的语句,类似于 shell -c选项。由于 Python 语句通常包含空格或其他shell特有的字符,建议引用整个命令。

【操作】

python -c 输出hello world!

注意:在Windows 10命令提示符下,由于其自身的语法解析问题,使用 python -c "print('hello, world!')" 时,如果命令行中已经存在一个或多个双引号,可能会导致错误。在这种情况下,可以使用单引号 ' 来替代双引号 "

【原文】Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line.

【译】一些 Python 模块作为脚本也很有用。它们可以使用python -m module [arg] ...调用。如果你在命令行上拼写了模块的全名,可以调用这些模块,模块将作为源文件执行。

【操作】

python -m 运行turtle

【原文】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在脚本运行之前进入交互模式。

【操作】

python -i

【原文】All command line options are described in Command line and environment.

【译】“命令行和环境部分介绍所有命令行选项。

记录:接触新的版本,阅读原文档,裨益多多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值