Hướng dẫn how do i convert python2 to python3 code? - làm cách nào để chuyển đổi mã python2 sang python3?

2TO3 là một chương trình Python đọc mã nguồn Python 2.x và áp dụng một loạt các trình sửa chữa để chuyển đổi nó thành mã Python 3.x hợp lệ. Thư viện tiêu chuẩn chứa một tập hợp các trình sửa chữa phong phú sẽ xử lý hầu hết tất cả các mã. Tuy nhiên, 2TO3 Thư viện hỗ trợ lib2to3 là một thư viện linh hoạt và chung chung, vì vậy có thể viết trình sửa chữa của riêng bạn cho 2to3.

Không được dùng từ phiên bản 3.11, sẽ bị xóa trong phiên bản 3.13: Mô -đun lib2to3 được đánh dấu chờ xử lý trong Python 3.9 (tăng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1 khi nhập) và không dùng nữa trong Python 3.11 (tăng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2). Công cụ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
3 là một phần của điều đó. Nó sẽ được loại bỏ trong Python 3.13.The lib2to3 module was marked pending for deprecation in Python 3.9 (raising
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1 on import) and fully deprecated in Python 3.11 (raising
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2). The
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
3 tool is part of that. It will be removed in Python 3.13.

Sử dụng 2to3¶

2to3 thường sẽ được cài đặt với trình thông dịch Python dưới dạng tập lệnh. Nó cũng được đặt trong thư mục

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
4 của gốc Python.

Các đối số cơ bản của 2to3 là ​​một danh sách các tệp hoặc thư mục để chuyển đổi. Các thư mục được đệ quy đi qua các nguồn Python.

Dưới đây là tệp nguồn Python 2.x mẫu,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
5:

def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

Nó có thể được chuyển đổi thành mã Python 3.x qua 2to3 trên dòng lệnh:

Một sự khác biệt so với tệp nguồn gốc được in. 2TO3 cũng có thể viết các sửa đổi cần thiết ngay vào tệp nguồn. (Một bản sao lưu của tệp gốc được thực hiện trừ khi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
6 cũng được đưa ra.) Viết các thay đổi được bật lại bằng cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
7:

Sau khi chuyển đổi,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
5 trông như thế này:

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

Nhận xét và thụt chính xác được bảo tồn trong suốt quá trình dịch thuật.

Theo mặc định, 2TO3 chạy một bộ trình sửa chữa được xác định trước. Cờ

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
9 liệt kê tất cả các trình sửa chữa có sẵn. Một tập hợp các trình sửa chữa rõ ràng để chạy có thể được đưa ra với
$ 2to3 -f imports -f has_key example.py
0. Tương tự như vậy,
$ 2to3 -f imports -f has_key example.py
1 vô hiệu hóa rõ ràng một trình sửa chữa. Ví dụ sau chỉ chạy bộ sửa chữa
$ 2to3 -f imports -f has_key example.py
2 và
$ 2to3 -f imports -f has_key example.py
3:predefined fixers. The
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
9 flag lists all available fixers. An explicit set of fixers to run can be given with
$ 2to3 -f imports -f has_key example.py
0. Likewise the
$ 2to3 -f imports -f has_key example.py
1 explicitly disables a fixer. The following example runs only the
$ 2to3 -f imports -f has_key example.py
2 and
$ 2to3 -f imports -f has_key example.py
3 fixers:

$ 2to3 -f imports -f has_key example.py

Lệnh này chạy mọi trình sửa chữa ngoại trừ trình sửa chữa

$ 2to3 -f imports -f has_key example.py
4:

$ 2to3 -x apply example.py

Một số trình sửa chữa rõ ràng, có nghĩa là chúng không được chạy theo mặc định và phải được liệt kê trên dòng lệnh sẽ được chạy. Ở đây, ngoài trình sửa lỗi mặc định, trình sửa

$ 2to3 -f imports -f has_key example.py
5 được chạy:

$ 2to3 -f all -f idioms example.py

Lưu ý cách vượt qua

$ 2to3 -f imports -f has_key example.py
6 cho phép tất cả các trình sửa chữa mặc định.

Đôi khi 2to3 sẽ tìm thấy một vị trí trong mã nguồn của bạn cần được thay đổi, nhưng 2to3 không thể tự động sửa chữa. Trong trường hợp này, 2to3 sẽ in cảnh báo bên dưới Diff cho một tệp. Bạn nên giải quyết cảnh báo để có mã 3.x tuân thủ.

2to3 cũng có thể tái cấu trúc tài liệu. Để kích hoạt chế độ này, hãy sử dụng cờ

$ 2to3 -f imports -f has_key example.py
7. Lưu ý rằng chỉ các tài liệu sẽ được tái cấu trúc. Điều này cũng không yêu cầu mô -đun phải là Python hợp lệ. Ví dụ, các ví dụ như tài liệu như trong tài liệu REST cũng có thể được tái cấu trúc với tùy chọn này.

Tùy chọn

$ 2to3 -f imports -f has_key example.py
8 cho phép đầu ra thêm thông tin về quy trình dịch.

Vì một số câu lệnh in có thể được phân tích cú pháp dưới dạng gọi chức năng hoặc câu lệnh, 2to3 không thể luôn đọc các tệp chứa hàm in. Khi 2TO3 phát hiện sự hiện diện của chỉ thị trình biên dịch

$ 2to3 -f imports -f has_key example.py
9, nó sẽ sửa đổi ngữ pháp nội bộ của nó để giải thích
$ 2to3 -x apply example.py
0 như là một hàm. Thay đổi này cũng có thể được kích hoạt thủ công bằng cờ
$ 2to3 -x apply example.py
1. Sử dụng
$ 2to3 -x apply example.py
1 để chạy bộ sửa lỗi trên mã đã có các câu lệnh in được chuyển đổi. Ngoài ra
$ 2to3 -x apply example.py
3 có thể được sử dụng để biến
$ 2to3 -x apply example.py
4 thành một hàm.

Tùy chọn

$ 2to3 -x apply example.py
5 hoặc
$ 2to3 -x apply example.py
6 cho phép đặc điểm kỹ thuật của một thư mục thay thế cho các tệp đầu ra được xử lý được ghi vào. Cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
6 được yêu cầu khi sử dụng điều này vì các tệp sao lưu không có ý nghĩa khi không ghi đè các tệp đầu vào.

Mới trong phiên bản 3.2.3: Tùy chọn

$ 2to3 -x apply example.py
5 đã được thêm vào.The
$ 2to3 -x apply example.py
5 option was added.

Cờ

$ 2to3 -x apply example.py
9 hoặc
$ 2to3 -f all -f idioms example.py
0 cho 2TO3 luôn ghi các tệp đầu ra ngay cả khi không cần thay đổi cho tệp. Điều này hữu ích nhất với
$ 2to3 -x apply example.py
5 để toàn bộ cây nguồn Python được sao chép với bản dịch từ thư mục này sang thư mục khác. Tùy chọn này ngụ ý cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
7 vì nó sẽ không có ý nghĩa khác.

Mới trong phiên bản 3.2.3: Cờ

$ 2to3 -x apply example.py
9 đã được thêm vào.The
$ 2to3 -x apply example.py
9 flag was added.

Tùy chọn

$ 2to3 -f all -f idioms example.py
4 chỉ định một chuỗi để nối vào tất cả các tên tệp đầu ra. Cờ
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
6 là bắt buộc khi chỉ định đây là bản sao lưu là không cần thiết khi viết cho các tên tệp khác nhau. Thí dụ:

$ 2to3 -n -W --add-suffix=3 example.py

Sẽ khiến một tệp được chuyển đổi có tên

$ 2to3 -f all -f idioms example.py
6 được viết.

Mới trong phiên bản 3.2.3: Tùy chọn

$ 2to3 -f all -f idioms example.py
4 đã được thêm vào.The
$ 2to3 -f all -f idioms example.py
4 option was added.

Để dịch toàn bộ dự án từ một cây thư mục sang sử dụng khác:

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

Người sửa chữa trong

Mỗi bước của mã chuyển đổi được gói gọn trong một bộ sửa lỗi. Lệnh

$ 2to3 -f all -f idioms example.py
8 liệt kê chúng. Như đã ghi lại ở trên, mỗi người có thể được bật và tắt riêng lẻ. Chúng được mô tả ở đây chi tiết hơn.documented above, each can be turned on and off individually. They are described here in more detail.

ứng dụng¶

Loại bỏ việc sử dụng

$ 2to3 -f all -f idioms example.py
9. Ví dụ
$ 2to3 -n -W --add-suffix=3 example.py
0 được chuyển đổi thành
$ 2to3 -n -W --add-suffix=3 example.py
1.

khẳng định

Thay thế tên phương thức

$ 2to3 -n -W --add-suffix=3 example.py
2 không dùng với các phương thức chính xác.

Từ

Đến

$ 2to3 -n -W --add-suffix=3 example.py
3

$ 2to3 -n -W --add-suffix=3 example.py
4

$ 2to3 -n -W --add-suffix=3 example.py
5

$ 2to3 -n -W --add-suffix=3 example.py
4

$ 2to3 -n -W --add-suffix=3 example.py
7

$ 2to3 -n -W --add-suffix=3 example.py
8

$ 2to3 -n -W --add-suffix=3 example.py
9

$ 2to3 -n -W --add-suffix=3 example.py
8

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
1

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
2

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
3

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
2

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
5

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
6

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
7

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
8

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
9

L = list(some_iterable)
L.sort()
0

L = list(some_iterable)
L.sort()
1

L = list(some_iterable)
L.sort()
0

L = list(some_iterable)
L.sort()
3

L = list(some_iterable)
L.sort()
4

L = list(some_iterable)
L.sort()
5

L = list(some_iterable)
L.sort()
4

Basestring¶

Chuyển đổi

L = list(some_iterable)
L.sort()
7 thành
L = list(some_iterable)
L.sort()
8.

đệm¶

Chuyển đổi

L = list(some_iterable)
L.sort()
9 thành
L = sorted(some_iterable)
0. Trình sửa lỗi này là tùy chọn vì API
L = sorted(some_iterable)
0 tương tự nhưng không chính xác giống như của
L = list(some_iterable)
L.sort()
9.

bắt buộc

Sửa các phương pháp lặp từ điển.

L = sorted(some_iterable)
3 được chuyển đổi thành
L = sorted(some_iterable)
4,
L = sorted(some_iterable)
5 thành
L = sorted(some_iterable)
6 và
L = sorted(some_iterable)
7 thành
L = sorted(some_iterable)
8. Tương tự,
L = sorted(some_iterable)
9, lib2to30 và lib2to31 được chuyển đổi tương ứng thành
L = sorted(some_iterable)
4,
L = sorted(some_iterable)
6 và
L = sorted(some_iterable)
8. Nó cũng kết thúc các cách sử dụng hiện có của
L = sorted(some_iterable)
4,
L = sorted(some_iterable)
6 và
L = sorted(some_iterable)
8 trong một cuộc gọi đến lib2to38.

ngoại trừ¶

Chuyển đổi lib2to39 thành

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
00.

người điều hành

Chuyển đổi câu lệnh

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
01 thành hàm
$ 2to3 -x apply example.py
4.

Execfile¶

Loại bỏ việc sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
03. Đối số của
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
03 được kết thúc trong các cuộc gọi đến
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
05,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
06 và
$ 2to3 -x apply example.py
4.

Lối ra

Thay đổi gán

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
08 để sử dụng mô -đun
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
09.

lọc¶

Kết thúc việc sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
10 trong cuộc gọi lib2to38.

funcattrs¶

Khắc phục các thuộc tính chức năng đã được đổi tên. Ví dụ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
12 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
13.

Tương lai¶

Xóa các câu

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
14.

getcwdu¶

Đổi tên

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
15 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
16.

has_key¶

Thay đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
17 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
18.

thành ngữ

Bộ sửa lỗi tùy chọn này thực hiện một số phép biến đổi làm cho mã Python thành ngữ hơn. Các so sánh loại như

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
19 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
20 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
21.
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
22 trở thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
23. Người sửa lỗi này cũng cố gắng sử dụng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
24 ở những nơi thích hợp. Ví dụ: khối này

L = list(some_iterable)
L.sort()

được thay đổi thành

L = sorted(some_iterable)

nhập khẩu¶

Phát hiện nhập khẩu anh chị em và chuyển đổi chúng thành nhập khẩu tương đối.

nhập khẩu

Xử lý mô -đun đổi tên trong thư viện tiêu chuẩn.

Nhập khẩu2¶

Xử lý các mô -đun khác đổi tên trong thư viện tiêu chuẩn. Nó tách biệt với trình sửa chữa

$ 2to3 -f imports -f has_key example.py
2 chỉ vì những hạn chế kỹ thuật.

đầu vào¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
26 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
27.

thực tập sinh

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
28 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
29.

isinstance¶

Khắc phục các loại trùng lặp trong đối số thứ hai của

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
30. Ví dụ,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
31 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
32 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
33 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
34.

itertools_imports¶

Loại bỏ nhập khẩu của

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
35,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
36 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
37. Nhập khẩu của
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
38 cũng được thay đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
39.

itertools¶

Thay đổi việc sử dụng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
35,
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
36 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
37 thành các tương đương tích hợp của chúng.
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
38 được thay đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
39.

Dài¶

Đổi tên

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
45 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
46.

bản đồ¶

Kết thúc

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
47 trong cuộc gọi lib2to38. Nó cũng thay đổi
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
49 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
50. Sử dụng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
51 vô hiệu hóa người sửa lỗi này.

Metaclass¶

Chuyển đổi cú pháp Metaclass cũ (

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
52 trong thân lớp) thành mới (
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
53).

Phương pháp

Khắc phục tên thuộc tính phương thức cũ. Ví dụ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
54 được chuyển đổi thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
55.

không có thứ tự

Chuyển đổi cú pháp không bình đẳng cũ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
56, thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
57.

tiếp theo¶

Chuyển đổi việc sử dụng các phương thức Iterator từ

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
58 thành hàm
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
58. Nó cũng đổi tên các phương thức
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
58 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
61.

khác không

Đổi tên các định nghĩa của các phương thức được gọi là

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
62 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
63.

Numliterals¶

Chuyển đổi chữ octal thành cú pháp mới.

nhà điều hành¶

Chuyển đổi các cuộc gọi đến các chức năng khác nhau trong mô -đun

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
64 sang các lệnh gọi chức năng khác, nhưng tương đương. Khi cần thiết, các câu lệnh
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
65 thích hợp được thêm vào, ví dụ:
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
66. Bản đồ sau đây được thực hiện:

Từ

Đến

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
67

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
68

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
69

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
70

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
71

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
72

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
73

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
74

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
75

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
76

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
77

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
78

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
79

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
80

vùng cao âm

Thêm dấu ngoặc đơn khi chúng được yêu cầu trong danh sách toàn diện. Ví dụ,

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
81 trở thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
82.

in¶

Chuyển đổi câu lệnh

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
83 thành hàm
$ 2to3 -x apply example.py
0.

nuôi¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
85 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
86 và
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
87 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
88. Nếu
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
89 là một tuple, bản dịch sẽ không chính xác vì việc thay thế các bộ dữ liệu cho các trường hợp ngoại lệ đã bị xóa trong 3.0.

Raw_Input¶

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
90 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
91.

giảm¶

Xử lý chuyển động của

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
92 đến
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
93.

Tải lại

Chuyển đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
94 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
95.

Đổi tên

Thay đổi

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
96 thành
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
97.

rệp lại

Thay thế Backtick repr với chức năng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
98.

set_literal¶

Thay thế việc sử dụng hàm tạo

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
99 bằng chữ viết. Fixer này là tùy chọn.

StandterErrorror¶

Đổi tên

$ 2to3 -f imports -f has_key example.py
00 thành
$ 2to3 -f imports -f has_key example.py
01.

sys_exc¶

Thay đổi các lỗi

$ 2to3 -f imports -f has_key example.py
02,
$ 2to3 -f imports -f has_key example.py
03,
$ 2to3 -f imports -f has_key example.py
04 để sử dụng
$ 2to3 -f imports -f has_key example.py
05.

ném¶

Khắc phục sự thay đổi API trong phương thức Generator

$ 2to3 -f imports -f has_key example.py
06.

Tuple_params¶

Loại bỏ tham số Tuple Inspicting Giải nén. Bộ sửa lỗi này chèn các biến tạm thời.

Loại

Sửa mã bị hỏng từ việc loại bỏ một số thành viên trong mô -đun

$ 2to3 -f imports -f has_key example.py
07.

unicode

Đổi tên

$ 2to3 -f imports -f has_key example.py
08 thành
L = list(some_iterable)
L.sort()
8.

Urllibn

Xử lý đổi tên của

$ 2to3 -f imports -f has_key example.py
10 và
$ 2to3 -f imports -f has_key example.py
11 cho gói
$ 2to3 -f imports -f has_key example.py
10.

WS_Comma¶

Loại bỏ khoảng trắng dư thừa khỏi các vật phẩm phân tách bằng dấu phẩy. Fixer này là tùy chọn.

xrange¶

Đổi tên

$ 2to3 -f imports -f has_key example.py
13 thành
$ 2to3 -f imports -f has_key example.py
14 và kết thúc các cuộc gọi
$ 2to3 -f imports -f has_key example.py
14 hiện có với lib2to38.

Xreadlines¶

Thay đổi

$ 2to3 -f imports -f has_key example.py
17 thành
$ 2to3 -f imports -f has_key example.py
18.

kéo dài ra

Kết thúc việc sử dụng

$ 2to3 -f imports -f has_key example.py
19 trong cuộc gọi lib2to38. Điều này bị vô hiệu hóa khi
$ 2to3 -f imports -f has_key example.py
21 xuất hiện.

lib2to3 - Thư viện 2to3

Mã nguồn: lib/lib2to3/ Lib/lib2to3/


Không dùng nữa kể từ phiên bản 3.11, sẽ bị xóa trong phiên bản 3.13: Python 3.9 đã chuyển sang trình phân tích cú pháp PEG (xem PEP 617) trong khi Lib2To3 đang sử dụng trình phân tích cú pháp LL (1) kém linh hoạt hơn. Python 3.10 bao gồm cú pháp ngôn ngữ mới không thể phân tích được bởi trình phân tích cú pháp LIB2TO3 LL (1) (xem PEP 634). Mô -đun lib2to3 đã được đánh dấu chờ xử lý trong Python 3.9 (tăng

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1 khi nhập) và không được phản đối trong Python 3.11 (tăng
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2). Nó sẽ được xóa khỏi thư viện tiêu chuẩn trong Python 3.13. Hãy xem xét các lựa chọn thay thế của bên thứ ba như Libcst hoặc PARSO.Python 3.9 switched to a PEG parser (see PEP 617) while lib2to3 is using a less flexible LL(1) parser. Python 3.10 includes new language syntax that is not parsable by lib2to3’s LL(1) parser (see PEP 634). The lib2to3 module was marked pending for deprecation in Python 3.9 (raising
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
1 on import) and fully deprecated in Python 3.11 (raising
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
2). It will be removed from the standard library in Python 3.13. Consider third-party alternatives such as LibCST or parso.

Ghi chú

API lib2to3 nên được coi là không ổn định và có thể thay đổi mạnh mẽ trong tương lai.

Tên của công cụ để chuyển đổi các tập lệnh Python 2 thành cú pháp Python 3 là gì?

2TO3 là một chương trình Python đọc mã Python 2. X Nguồn và áp dụng một loạt các trình sửa chữa để biến nó thành Python 3 hợp lệ. is a Python program that reads Python 2. x source code and applies a series of fixers to transform it into valid Python 3.

Chương trình Python 2 có hoạt động với Python 3 không?

Python phiên bản 3 không tương thích ngược với Python 2. Nhiều nhà phát triển gần đây đang tạo các thư viện mà bạn chỉ có thể sử dụng với Python 3. Nhiều thư viện cũ được tạo cho Python 2 không tương thích về phía trước.. Many recent developers are creating libraries which you can only use with Python 3. Many older libraries created for Python 2 is not forward-compatible.

Mã được viết trong Python 3 có tương thích ngược với Python 2 không?

Python 3 không tương thích ngược với Python 2. Python 2 chủ yếu được sử dụng để trở thành một kỹ sư DevOps.Nó không còn được sử dụng sau năm 2020. Python 3 được sử dụng trong rất nhiều lĩnh vực như kỹ thuật phần mềm, khoa học dữ liệu, v.v.. Python 2 was mostly used to become a DevOps Engineer. It is no longer in use after 2020. Python 3 is used in a lot of fields like Software Engineering, Data Science, etc.

Bạn có thể trộn Python 2 và 3 không?

Đừng "trộn".Khi các gói và mô -đun khác nhau có sẵn trong Python 3, hãy sử dụng chuyển đổi 2TO3 để tạo Python 3. Bạn sẽ tìm thấy một số vấn đề nhỏ.Khắc phục Python 2 của bạn để gói của bạn hoạt động trong Python 2 và cũng hoạt động sau khi chuyển đổi.. When the various packages and modules are available in Python 3, use the 2to3 conversion to create Python 3. You'll find some small problems. Fix your Python 2 so that your package works in Python 2 and also works after the conversion.