You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Khi một lỗi phát sinh, trình thông dịch in ra một thông báo lỗi và một vết stack(stack trace, một báo cáo về những khung stack tại một thời điểm trong khi chương trình đang chạy). Trong chế độ tương tác, nó sau đó trở về cửa sổ chính; khi ngõ vào(input) lấy từ một file, nó thoát ra với trạng thái thoát khác không(nonzero exit) sau khi in ra stack trace. \(Ngoại lệ được xử lý bởi một mệnh đề [`except`](https://docs.python.org/3/reference/compound_stmts.html#except) trong một phát biểu [`try`](https://docs.python.org/3/reference/compound_stmts.html#try) không phải là lỗi trong trường hợp này\). Một số lỗi vô điều kiện không thể tránh được và gây ra nonzero exit; xảy ra khi xuất hiện những mâu thuẫn nội bộ và một vài trường hợp bị đầy bộ nhớ. Tất cả tin nhắn lỗi được ghi vào luồng lỗi chuẩn(standard error stream); ngõ ra bình thường từ những câu lệnh được thực hiện được ghi vào ngõ ra chuẩn(standard output).
8
10
9
-
When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack trace. \(Exceptions handled by an [`except`](https://docs.python.org/3/reference/compound_stmts.html#except) clause in a [`try`](https://docs.python.org/3/reference/compound_stmts.html#try) statement are not errors in this context.\) Some errors are unconditionally fatal and cause an exit with a nonzero exit; this applies to internal inconsistencies and some cases of running out of memory. All error messages are written to the standard error stream; normal output from executed commands is written to standard output.
11
+
Việc gõ ký tự ngắt \(thường là `Control-C` hoặc `Delete`\) trong cửa sổ chính hoặc thứ cấp hủy ngõ vào và trở về cửa sổ chính. \[1\] Gõ một ngắt trong khi một câu lệnh đang thực hiện tạo ra một ngoại lệ [`KeyboardInterrupt`](https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt), điều này có thể được xử lý bằng phát biểu [`try`](https://docs.python.org/3/reference/compound_stmts.html#try).
10
12
11
-
Typing the interrupt character \(usually Control-C or Delete\) to the primary or secondary prompt cancels the input and returns to the primary prompt. [\[1\]](https://docs.python.org/3/tutorial/appendix.html#id2) Typing an interrupt while a command is executing raises the[`KeyboardInterrupt`](https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt) exception, which may be handled by a [`try`](https://docs.python.org/3/reference/compound_stmts.html#try) statement.
13
+
#### 16.1.2. Những đoạn mã(scripts) thực thi
12
14
13
-
#### 16.1.2. Executable Python Scripts
14
-
15
-
On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line
15
+
Trên các hệ thống Unix thuộc về BSD, những Python script có thể thực hiện trực tiếp, giống như shell script, bằng cách thêm vào dòng bên dưới
16
16
17
17
```text
18
18
#!/usr/bin/env python3.5
19
19
```
20
20
21
-
\(assuming that the interpreter is on the user’s `PATH`\)at the beginning of the script and giving the file an executable mode. The `#!`must be the first two characters of the file. On some platforms, this first line must end with a Unix-style line ending\(`'\n'`\), not a Windows \(`'\r\n'`\) line ending. Note that the hash, or pound, character, `'#'`, is used to start a comment in Python.
21
+
\(giả sử trình thông dịch nằm trên đường dẫn `PATH` của người dùng\)từ lúc bắt đầu script và tạo cho file một chế độ thực thi. `#!`\(mở đầu của [`shebang`](https://en.wikipedia.org/wiki/Shebang_\(Unix\))\) phải là hai ký tự đầu tiên của file. Với một số nền tảng, dòng đầu tiên này phải kết thúc theo kiểu Unix\(`'\n'`\), không phải kiểu Windows \(`'\r\n'`\). Lưu ý rằng kí tự `#` được sử dụng dể bắt đầu một nhận xét(comment) trong Python.
22
22
23
-
The script can be given an executable mode, or permission, using the **chmod** command.
23
+
Script có thể được đưa cho một chế độ thực thi, hoặc sự cho phép, bằng cách sử dụng lệnh **chmod**.
24
24
25
25
```text
26
26
$ chmod +x myscript.py
27
27
```
28
28
29
-
On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates `.py`files with `python.exe` so that a double-click on a Python file will run it as a script. The extension can also be `.pyw`, in that case, the console window that normally appears is suppressed.
29
+
Trên các hệ thống Windows, không có khái niệm về "chế độ hoạt động". Bộ cài đặt Python tự động liên kết các file `.py`với `python.exe`, vì vậy khi nhấn đúp vào một file Python thì sẽ chạy nó như một script. Phần mở rộng cũng có thể là `.pyw`, trong trường hợp này giao diện điều khiển thông thường bị loại bỏ.
30
30
31
-
#### 16.1.3. The Interactive Startup File
31
+
#### 16.1.3. File khởi động tương tác
32
32
33
-
When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named [`PYTHONSTARTUP`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP)to the name of a file containing your start-up commands. This is similar to the `.profile`feature of the Unix shells.
33
+
Khi bạn sử dụng Python tương tác, sẽ tiện hơn nếu có một vài câu lệnh chuẩn thực hiện mỗi lần trình thông dịch khởi động. Bạn có thể thực hiện chúng bằng việc thiết lập biến môi trường gọi là [`PYTHONSTARTUP`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP)là tên file chứa những dòng lệnh khởi động của bạn. Cái này tương tự tính năng `.profile`của Unix shells.
34
34
35
-
This file is only read in interactive sessions, not when Python reads commands from a script, and not when `/dev/tty`is given as the explicit source of commands \(which otherwise behaves like an interactive session\). It is executed in the same namespace where interactive commands are executed, so that objects that it defines or imports can be used without qualification in the interactive session. You can also change the prompts `sys.ps1`and `sys.ps2`in this file.
35
+
File này chỉ được đọc trong những phiên tương tác, không phải khi Python đọc những câu lệnh từ một script, cũng không phải khi `/dev/tty`được xem như là một nguồn rõ ràng của các câu lệnh \( điều mà ngược lại biểu hiện như một phiên tương tác\). Nó được thực thi trong cùng không gian tên nơi những dòng lệnh tương tác được thực thi, vì vậy đối tượng được nó định nghĩa hoặc nhập vào có thể được sử dụng mà không cần đánh giá trong phiên tương tác. Bạn cũng có thể thay đổi cửa sổ(prompt) `sys.ps1` và `sys.ps2`trong file này.
36
36
37
-
If you want to read an additional start-up file from the current directory, you can program this in the global start-up file using code like `if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())`. If you want to use the startup file in a script, you must do this explicitly in the script:
37
+
Nếu bạn muốn đọc một file khởi động bổ sung từ thư mục hiện tại, bạn có thể lập trình nó trong file khởi động toàn cục sử dụng mã lệnh như `if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())`. Nếu bạn muốn sử dụng file khởi động trong một script, bạn phải thực hiện nó một cách rõ ràng trong script:
38
38
39
39
```text
40
40
import os
@@ -45,23 +45,23 @@ if filename and os.path.isfile(filename):
45
45
exec(startup_file)
46
46
```
47
47
48
-
#### 16.1.4. The Customization Modules
48
+
#### 16.1.4. Những module tùy biến
49
49
50
-
Python provides two hooks to let you customize it: `sitecustomize`and`usercustomize`. To see how it works, you need first to find the location of your user site-packages directory. Start Python and run this code:>>>
50
+
Python cung cấp hai hook(đại loại như một nơi thường là một giao tiếp được cung cấp trong đoạn mã được đóng gói cho phép bạn thêm vào những tùy chỉnh) cho phép bạn tùy chỉnh nó: `sitecustomize`và`usercustomize`.Để hiểu được nó hoạt động như thế nào, trước tiên bạn cần phải tìm được vị trí của thư mục site-packages. Khởi động Python và chạy câu lệnh:
51
51
52
52
```text
53
53
>>> import site
54
54
>>> site.getusersitepackages()
55
55
'/home/user/.local/lib/python3.5/site-packages'
56
56
```
57
57
58
-
Now you can create a file named `usercustomize.py`in that directory and put anything you want in it. It will affect every invocation of Python, unless it is started with the [`-s`](https://docs.python.org/3/using/cmdline.html#cmdoption-s)option to disable the automatic import.
58
+
Bây giờ bạn có thể tạo một file có tên `usercustomize.py`trong thư mục đó và để bất cứ thứ gì bạn muốn vào trong đó. Nó sẽ ảnh hưởng tất cả thành phần gọi ra(invocations) của Python, nếu nó không được bắt đầu với lựa chọn [`-s`](https://docs.python.org/3/using/cmdline.html#cmdoption-s)để vô hiệu import một cách tự động.
59
59
60
-
`sitecustomize`works in the same way, but is typically created by an administrator of the computer in the global site-packages directory, and is imported before`usercustomize`. See the documentation of the [`site`](https://docs.python.org/3/library/site.html#module-site)module for more details.
60
+
`sitecustomize`hoạt động tương tự, nhưng nó thường được khởi tạo bởi quản trị viên của máy tính trong thư mục toàn cục site-packages, được import vào trước`usercustomize`. Xem thêm tài liệu của module [`site`](https://docs.python.org/3/library/site.html#module-site)để biết thêm chi tiết.
61
61
62
-
Footnotes
62
+
Chú thích:
63
63
64
-
|[\[1\]](https://docs.python.org/3/tutorial/appendix.html#id1)|A problem with the GNU Readline package may prevent this. |
64
+
|\[1\]|Một vấn đề với gói GNU Readline có thể cản trở điều này. |
0 commit comments