002 使用 Python 解释器

2. 使用 Python 解释器

2.1. Python 解释器的调用

Python 解释器通常被安装在机器的 /usr/local/bin/python3.6 目录下,如果该目录是可用的话;用你的 Unix shell 搜索 /usr/local/bin 目录然后键入如下命令开始:[1]

python3.6

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.)

On Windows machines, the Python installation is usually placed in C:\Python36, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:

set path=%path%;C:\python36

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().

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline. 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.

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.

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 with single quotes.

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.

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.

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. You can access this list by executing import sys. The length of the list is at least one; when no script and no arguments are given, sys.argv[0] is an empty string. When the script name is given as ‘-‘ (meaning standard input), sys.argv[0] is set to ‘-‘. When -c command is used, sys.argv[0] is set to ‘-c‘. When -m module is used, sys.argv[0] is set to the full name of the located module. Options found after -c command or -m module are not consumed by the Python interpreter’s option processing but left in sys.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. 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.6
Python 3.6 (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:

>>>

>>> 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. 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, a convention that any portable code should follow. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.

To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows:

# -*- coding: encoding -*-

where encoding is one of the valid codecs supported by Python.

For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:

# -*- coding: cp-1252 -*-

One exception to the first line rule is when the source code starts with a UNIX “shebang” line. In this case, the encoding declaration should be added as the second line of the file. For example:

#!/usr/bin/env python3
# -*- coding: cp-1252 -*-

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.
时间: 2024-10-08 16:23:09

002 使用 Python 解释器的相关文章

python 解释器内建函数

python解释器内建函数列表如下: 001.abs() 求绝对值 #!/usr/bin/python if __name__=="__main__": print(abs(-100))#打印100 002.all() 如果参数列表中所有的值都是True,all函数才返回True #!/usr/bin/python if __name__=="__main__": conditions=[True,False] print(all(conditions))#打印Fa

Python解释器

1        Python解释器 1.1   CPython 当我们从Python官方网站下载并安装好Python 3.5后,我们就直接获得了一个官方版本的解释器:CPython.这个解释器是用C语言开发的,所以叫CPython.在命令行下运行python就是启动CPython解释器. CPython是使用最广的Python解释器.教程的所有代码也都在CPython下执行. 1.2   IPython IPython是基于CPython之上的一个交互式解释器,也就是说,IPython只是在交

python解释器快捷键

13. 交互式输入的编辑和历史记录 某些版本的 Python 解释器支持编辑当前的输入行和历史记录,类似于在 Korn shell 和 GNU Bash shell 中看到的功能.这是使用GNU Readline库实现的,它支持 Emacs 风格和 vi 风格的编辑.这个库有它自己的文档,在这里我不就重复了:然而,基本原理很容易解释.本章讲述的交互式编辑和历史记录功能在 Unix 版本和 Cygwin 版本中是可选的. 13.1. 行编辑 如果支持,无论解释器打印主提示符还是从属提示符,输入行一

最全Pycharm教程(4)——有关Python解释器的相关配置

最全Pycharm教程(1)——定制外观 最全Pycharm教程(2)——代码风格 最全Pycharm教程(3)——代码的调试.运行 1.准备工作 (1)Pycharm版本为3.4或者更高. (2)电脑上至少已经安装了一个Python解释器. (3)如果你希望配置一个远程解释器,则需要服务器的相关支持. 2.本地解释器配置 配置本地解释器的步骤相对简洁直观: (1)单击工具栏中的设置按钮. (2)在Settings/Preferences对话框中选中 Project Interpreter页面,

设置PyCharm软件的配色方案和Python解释器

设置PyCharm软件的配色方案 设置Python解释器(用于Python2 or 3 的切换)

winfrom桌面程序调用python解释器

Winfrom桌面程序调用python解释器执行py脚本后台执行完成具体的功能,为什么要这样处理呢?因为我现在的大部分过项目都是后台的脚本处理,界面基本的输入完成之后,将参数按照规则传入到脚本的入口,根据参数的不同执行不同的脚本流程,如果要修改某一个处理过程或者添加新的模块,不需要修改前台的任何代码,只需要在脚本中修改就可以达到需求的效果,简单.方便,风险较小,影响可控等优点. 因此,我做了一个demo,仅供参考,不足之处,请赐教! 界面如图: Start_exe_py的执行代码如下: 1 pr

Python解释器的探讨:第二部分代码对象

本文是python解释器系列文章的一部分,第一部分文章请看这里: http://blog.csdn.net/caimouse/article/details/47342357 从第一部分文章里可知,主要分析了一下函数对象相关的内容.本文里继续深入分析python解析器相关内容,主要分析函数的代码对象. >>> def foo(a): ...     x = 3 ...     return x + a ... >>> foo <function foo at 0x

Unix类环境调用python解释器

在Unix类环境中我们编写python脚本时总是需要调用python解释器,调用方式通常是以下两种: 1 #!/usr/bin/python 2 #!/usr/bin/env python 那么,这两种调用python解释器的方法有什么区别呢? 其实这两种写法都是对的,其中#!/usr/bin/python直接告诉操作系统执行此脚本的时间,去/usr/bin目录下找python解释器.而写成#!/usr/bin/env python的形式则是为了去适应python没有被默认安装在/usr/bin

Python解释器的探讨:第一部分函数对象

Python解释器的探讨:第一部分函数对象 最近三个月,我和Ned Batchelder花费了大量时间在开发byterun.这个项目byterun(https://github.com/nedbat/byterun)是使用python开发的python字节码的解释器.开发解释器byterun过程中,让我学习到很多东西,以及带来很大的乐趣.在本文系列里,我将带你来体验一下我的开发乐趣,以及使用byterun是非常高兴的事情.不过,在体验快乐之前,先要理解一些基础的知识,就像做运动之前先来暖身一下: