2.1 Invoking the Interpreter
Python path
On Unix-like machines you can find the executable file under /usr/local/bin/
1 | $ file /usr/local/bin/python3.9 |
Since the choice of the directory where the interpreter lives is an installation option, other places are possible
Exit Python
enter EOF character (Ctrl-D on Unix, Ctrl-Z on Windows)
exit via cmd
quit()if exit gracefully, exit status will be 0
1
2
3
4
5
6
7$ python3.9
Python 3.9.1 (default, Dec 29 2020, 08:52:17)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ echo $?
0Editing
If Ctrl-P have beeps, line-edit is allowed
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.
TTY : stand for TeleTYpewriter
PTS : stand for Pseudo-TeletypeS
simple cmd :
python -c command [arg] ..., command is suggested to be quoted with single quotesuse module :
python -m module [arg] ...enter interactive mode after executing a script file :
1
2
3
4
5$ python3 test.py
hello
$ python3 -i test.py
hello
>>>
2.1.1. Argument Passing
The script name and additional arguments are turned into a list of string stored in variable sys.argv
- The length of the list is at least one
| condition | argv[0] |
|---|---|
| no script and no arguments | ‘’ (void string) |
the script name is given as '-'(stdin) |
‘_’ |
-c command is used |
‘-c’ |
-m module is used |
the full name of the located module |
- Options found after
-ccommand or-mmodule are not consumed by the Python interpreter’s option processing but left insys.argvfor the command or module to handle.
1 | # source of test.py |
1 | $ python3 test.py hello world |
2.1.2. Interactive Mode
2.2. The Interpreter and Its Environment
2.2.1. Source Code Encoding
By default, Python source code use
UTF-8encodingAll Python standard libraries use
ASCII, a convention that any portable code should followTo 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:
1
# -*- coding: encoding -*-
for example:
1
# -*- coding: cp1252 -*-
the first or second line must match the regular expression “coding[:=]\s*([-\w.]+)”
so
# -*- encoding: xxxxxx -*-also worksIf the source code starts with a UNIX “shebang” line, encoding massage should be the second line
1
2#!/usr/bin/env python3
# -*- coding: cp1252 -*-What does a “shebang” line do? —- make a python script a shell script
1
2print("Hello World")
# source of test.py1
2
3
4
5
6
7
8
9
10$ ./test.py
permission denied: ./test.py
$ ls -l test.py
-rw-r--r-- 1 ****** staff 21 Feb 2 **:** test.py
$ chmod +x test.py
$ ls -l test.py
-rwxr-xr-x 1 ****** staff 21 Feb 2 **:** test.py
$ ./test.py
./test.py: line 1: syntax error near unexpected token `'Hello World''
./test.py: line 1: `print('Hello World')'1
2
3#! /usr/bin/env python3
print("Hello World")
# source of test.py1
2$ ./test.py
Hello WorldNOTE that the declaration does NOT change the encoding acutually
it only applies to how Python reads the source code. It doesn’t apply to executing that code, so not to how printing, opening files, or any other I/O operations translate between bytes and Unicode.
usually used to solve UnicodeEncodeError