Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

[Tháng 6 năm 2016] Nơi được đề xuất cho thông tin về quá trình chuyển đổi là trang chính thức của Ubuntu Python. The recommended place for information on the transition is official Ubuntu Python page.


Từ Wiki Ubuntu:

Đối với cả Ubuntu và Debian, chúng tôi có các mục tiêu dự án liên tục để biến Python 3 thành phiên bản Python mặc định, ưa thích trong bản phân phối.

Điều này không có nghĩa là gì:does not mean:

  • rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    2 sẽ chỉ ra Python 3. Không, điều này sẽ không xảy ra (trừ khi PEP 394 ủng hộ khác, điều này rất đáng nghi ngờ cho tương lai gần).
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    2 và
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    4 sẽ chỉ vào Python 2.7 và
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    5 sẽ chỉ ra phiên bản Python 3 được hỗ trợ mới nhất.No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future).
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    2 and
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    4 will point to Python 2.7 and
    rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
    update-alternatives: --install needs    
    
    Use 'update-alternatives --help' for program usage information.   
    
    5 will point to the latest supported Python 3 version.

  • Python 2 sẽ được xóa khỏi kho lưu trữ. Không, điều này sẽ không xảy ra. Chúng tôi hy vọng Python 2.7 sẽ vẫn được hỗ trợ và có sẵn ở Ubuntu trong một thời gian dài, vì PEP 373 hứa hẹn hỗ trợ bảo trì sửa lỗi ngược dòng cho đến năm 2020.No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020.

Không nên thay đổi liên kết tượng trưng vì các phụ thuộc gói khác, nhưng họ "có các mục tiêu dự án liên tục để biến Python 3 thành phiên bản Python mặc định, ưa thích trong bản phân phối".


Để sử dụng CLI, như @radu rădeanu, tôi khuyên bạn nên đặt bí danh vào tệp

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
6,
rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
7 của người dùng (các tệp khác nhau, bao gồm
rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
8, được tải ít nhất một lần, chủ yếu là cho mục đích tổ chức, nhưng có thể thay đổi theo nền tảng). Môi trường ảo Python cũng hoạt động tốt.

Ví dụ bí danh:

alias python=python3

hoặc

alias python='/usr/bin/python3'

Các tập lệnh vẫn nên sử dụng một cái gì đó như

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
9 để tương thích chéo.

Sử dụng

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
0 là tốt để sử dụng hỗn hợp với môi trường ảo.

Lưu ý (nhờ @wjandrea): Bí danh là một phần của thời gian chạy bash, không phải môi trường người dùng. Do đó, chúng không có sẵn cho Shebang (

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
1). Nếu bạn thích bí danh python = python3, thì một số
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
2 không có shebang có thể được thực thi bằng cách gọi trình thông dịch bí danh như thế này
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
3. Bí quyết cũng có thể hữu ích cho các hệ thống có nhiều phiên bản Python3 như 3,4 và 3.6 cùng nhau.

274

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đã cố gắng đặt phiên bản Python mặc định thành

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
4 trong
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
5. Theo mặc định, đó là
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
6 (2.7). Tôi đã làm theo các bước dưới đây:

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3

Nhưng tôi đang gặp lỗi sau cho câu lệnh thứ hai,

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Siêu bão

4.7465 Huy hiệu vàng20 Huy hiệu bạc32 Huy hiệu Đồng5 gold badges20 silver badges32 bronze badges

Đã hỏi ngày 1 tháng 2 năm 2017 lúc 17:57Feb 1, 2017 at 17:57

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

RejeeshchandranrejeeshchandranRejeeshChandran

3.9983 Huy hiệu vàng23 Huy hiệu bạc32 Huy hiệu Đồng3 gold badges23 silver badges32 bronze badges

8

Dòng thứ hai được đề cập có thể được thay đổi thành

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Điều này ưu tiên 10 cho đường dẫn

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
4.

Nhược điểm của việc chỉnh sửa thay thế

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
8 là việc sử dụng các lệnh có
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
9 sẽ không hoạt động.

Nico Schlömer

49.1K24 Huy hiệu vàng186 Huy hiệu bạc225 Huy hiệu đồng24 gold badges186 silver badges225 bronze badges

Đã trả lời ngày 14 tháng 5 năm 2018 lúc 13:10May 14, 2018 at 13:10

10

EDIT:

Tôi đã viết điều này khi tôi còn trẻ và ngây thơ,

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0 là cách tốt hơn để làm điều này. Xem câu trả lời của @Pardhu.


Câu trả lời lỗi thời:

Mở tệp .bashrc của bạn

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
1. Nhập
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
2 vào một dòng mới ở đầu tệp sau đó lưu tệp bằng ctrl+o và đóng tệp bằng ctrl+x. Sau đó, trở lại tại dòng lệnh của bạn loại
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
3. Bây giờ bí danh của bạn nên là vĩnh viễn.

Nico Schlömer

49.1K24 Huy hiệu vàng186 Huy hiệu bạc225 Huy hiệu đồng24 gold badges186 silver badges225 bronze badges

Đã trả lời ngày 14 tháng 5 năm 2018 lúc 13:10Feb 1, 2017 at 18:17

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Tôi đã viết điều này khi tôi còn trẻ và ngây thơ,

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0 là cách tốt hơn để làm điều này. Xem câu trả lời của @Pardhu.Steampunkery

Câu trả lời lỗi thời:2 gold badges19 silver badges28 bronze badges

8

Mở tệp .bashrc của bạn

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
1. Nhập
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
2 vào một dòng mới ở đầu tệp sau đó lưu tệp bằng ctrl+o và đóng tệp bằng ctrl+x. Sau đó, trở lại tại dòng lệnh của bạn loại
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
3. Bây giờ bí danh của bạn nên là vĩnh viễn.

Đã trả lời ngày 1 tháng 2 năm 2017 lúc 18:17

SteamPunkeryStebunkery

  1. 3,8112 huy hiệu vàng19 Huy hiệu bạc28 Huy hiệu đồng

    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
    
    4

  2. Để thay đổi Python 3.6.8 như mặc định trong Ubuntu 18.04 thành Python 3.7.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
  1. Cài đặt Python 3.7

    Các bước để cài đặt Python3.7 và định cấu hình nó làm trình thông dịch mặc định.

  2. Cài đặt gói Python3.7 bằng apt-get

python3 --version
Python 3.7.1 

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Thêm Python3.6 & Python 3.7 vào

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0

Cập nhật Python 3 để chỉ vào Python 3.73 gold badges20 silver badges35 bronze badges

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
6 Nhập 2 cho Python 3.7Aug 24, 2019 at 23:03

5

Kiểm tra phiên bản Python

sudo apt install python-is-python3

Alvin Sartor

1.9673 huy hiệu vàng20 Huy hiệu bạc35 Huy hiệu đồngMay 18, 2020 at 20:12

Đã trả lời ngày 24 tháng 8 năm 2019 lúc 23:03silviot

Nếu bạn có Ubuntu 20.04 lts (Focal Fossa), bạn có thể cài đặt

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
7:4 gold badges37 silver badges50 bronze badges

3

trong đó thay thế cho Symlink trong

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
2 để trỏ đến
rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
5.

Đã trả lời ngày 18 tháng 5 năm 2020 lúc 20:12Feb 1, 2017 at 18:00

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

DantevoronoidantevoronoiDanteVoronoi

1.0731 Huy hiệu vàng12 Huy hiệu bạc20 Huy hiệu Đồng1 gold badge12 silver badges20 bronze badges

2

CẬP NHẬT: Kể từ khi Ubuntu 20.04, Python3 là phiên bản mặc định, nhưng vẫn vậy, Python không được đăng ký là Python3 theo mặc định. Để thực hiện điều đó, bạn có thể chỉ cần làm:
Since Ubuntu 20.04, the python3 is the default version, but still, python is not registered as python3 by default. In order to make that happen, you can simply do :

sudo apt install python-is-python3

Để biết thêm thông tin, bạn có thể kiểm tra điều này. Way Way:
Old way:

Làm

cd ~
gedit .bash_aliases

Sau đó viết một trong hai

alias python=python3

hoặc

alias python='/usr/bin/python3'

Lưu tệp, đóng thiết bị đầu cuối và mở lại. Bây giờ bạn sẽ ổn thôi! Liên kết
You should be fine now! Link

Đã trả lời ngày 15 tháng 9 năm 2017 lúc 18:34Sep 15, 2017 at 18:34

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

HosseinhosseinHossein

22.6K32 Huy hiệu vàng117 Huy hiệu bạc210 Huy hiệu đồng32 gold badges117 silver badges210 bronze badges

1

Một cách an toàn đơn giản sẽ là sử dụng bí danh. Đặt cái này vào tệp ~/.bashrc: Nếu bạn đã sử dụng Gedit Editor

gedit ~/.bashrc

Để đi vào tệp BASHRC và sau đó ở đầu tệp BASHRC sẽ thay đổi sau.

bí danh python = python3

Sau khi thêm các điều trên trong tệp. Chạy lệnh bên dưới

nguồn ~/.bash_aliases hoặc nguồn ~/.bashrc

example:

$ Python --Version

python3 --version
Python 3.7.1 
1

$ python3 --Version

python3 --version
Python 3.7.1 
2

$ bí danh Python = Python3

$ Python --Version

python3 --version
Python 3.7.1 
2

$ python3 --VersionFeb 9, 2018 at 10:32

$ bí danh Python = Python3Khan

Đã trả lời ngày 9 tháng 2 năm 2018 lúc 10:3210 silver badges11 bronze badges

0

Khankhan

  • 1.19410 Huy hiệu bạc11 Huy hiệu đồng
  • Chỉ cần làm theo các bước này để giúp thay đổi Python mặc định thành phiên bản Python mới nâng cấp. Làm việc tốt cho tôi.
  • python3 --version
    Python 3.7.1 
    
    4 Cài đặt phiên bản Python mới nhất mà bạn muốn
  • python3 --version
    Python 3.7.1 
    
    5 Nhập thư mục gốc nơi cài đặt Python
  • python3 --version
    Python 3.7.1 
    
    6 hoặc
    python3 --version
    Python 3.7.1 
    
    7. Hủy liên kết Python mặc định hiện tại

python3 --version
Python 3.7.1 
8 Liên kết Phiên bản Python đã tải xuống mớiDec 30, 2019 at 9:19

python3 --version
Python 3.7.1 
9 Kiểm tra phiên bản Python mới và bạn tốt để đi

alias python='/usr/bin/python3'
2

Đã trả lời ngày 30 tháng 12 năm 2019 lúc 9:19

alias python=python3

Lúc đầu cài đặt Python3 và PIP3

alias python='/usr/bin/python3'
4

Sau đó, trong cuộc chạy đầu cuối của bạnNov 25, 2019 at 18:32

Kiểm tra phiên bản Python trong máy của bạn.

Đã trả lời ngày 25 tháng 11 năm 2019 lúc 18:32

Để thêm thêm, bạn cũng có thể thêm bí danh cho PIP (trong .bashrc hoặc bash_aliases):

bí danh pip = 'pip3'
alias python='python3.6'

Bạn nhiều người thấy rằng một bản cài đặt sạch của Python3 thực sự chỉ vào Python3.x để bạn có thể cần:Mar 28, 2018 at 14:28

bí danh pip = 'pip3.6' bí danh python = 'python3.6'Paraic

Đã trả lời ngày 28 tháng 3 năm 2018 lúc 14:281 silver badge6 bronze badges

Paraicparaic

alias python='/usr/bin/python3'
5

1371 Huy hiệu bạc6 Huy hiệu đồng

Đây là một cách đơn giản làm việc cho tôi.

Bạn có thể thay đổi

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
5 cho đường dẫn đến Python3 (hoặc phiên bản bạn muốn).Jan 15, 2021 at 14:18

Nhưng hãy nhớ rằng

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0 có lẽ là lựa chọn tốt nhất.cbcram

Đã trả lời ngày 15 tháng 1 năm 2021 lúc 14:182 silver badges5 bronze badges

cbcramcbcram

1672 Huy hiệu bạc5 Huy hiệu Đồng

Như đã nói,

sudo apt install python-is-python3
2 nhu cầu và lập luận.

Bạn có liên kết (

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
2), tên (
sudo apt install python-is-python3
4) và đường dẫn (
rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
5), bạn đang thiếu ưu tiên.

sudo apt install python-is-python3
6 nói:

là một số nguyên; Các tùy chọn có số cao hơn có mức độ ưu tiên cao hơn ở chế độ tự động.Feb 1, 2017 at 19:30

Vì vậy, chỉ cần đặt một

sudo apt install python-is-python3
7 hoặc một cái gì đó ở cuối

alias python='/usr/bin/python3'
6

Đã trả lời ngày 1 tháng 2 năm 2017 lúc 19:30

alias python='/usr/bin/python3'
7

Nhận con đường Python từOct 16, 2018 at 4:26

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Sau đó đặt phiên bản Python của bạn

alias python='/usr/bin/python3'
8

Đã trả lời ngày 16 tháng 10 năm 2018 lúc 4:26

alias python='/usr/bin/python3'
9

Để thay đổi Python 3.6.8 như mặc định trong Ubuntu 18.04 từ Python 2.7, bạn có thể thử công cụ dòng lệnh

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0.

Nếu bạn gặp lỗi "Không có lựa chọn thay thế nào cho Python" thì hãy tự thiết lập một giải pháp thay thế bằng lệnh sau:

Thay đổi đường dẫn

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
5 thành phiên bản Python mong muốn của bạn cho phù hợp.

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
0

Đối số cuối cùng được chỉ định là ưu tiên của nó, nếu không có lựa chọn thay thế thủ công nào được thực hiện thay thế với số ưu tiên cao nhất sẽ được đặt. Trong trường hợp của chúng tôi, chúng tôi đã đặt mức độ ưu tiên 2 cho

sudo apt install python-is-python3
0 và kết quả là
sudo apt install python-is-python3
0 được đặt làm phiên bản Python mặc định tự động bằng lệnh Cập nhật-Diễn hóa.Jun 27, 2020 at 12:24

Chúng tôi có thể chuyển đổi bất cứ lúc nào giữa các phiên bản thay thế Python được liệt kê ở trên sử dụng lệnh bên dưới và nhập số lựa chọn:

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
1

Đã trả lời ngày 27 tháng 6 năm 2020 lúc 12:24

Đối với một cách tiếp cận duy nhất không xâm lấn, người dùng hiện tại:Mar 22, 2019 at 8:52

sudo apt install python-is-python3
4
sudo apt install python-is-python3
3 sẽ sẵn sàng trong một vỏ mới.tdihp

Đã trả lời ngày 22 tháng 3 năm 2019 lúc 8:522 gold badges22 silver badges40 bronze badges

Tdihptdihp

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
2

2.2692 Huy hiệu vàng22 Huy hiệu bạc40 Huy hiệu đồng

sudo apt install python-is-python3

Chỉ cần xóa

sudo apt install python-is-python3
4:

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
4

Và cài đặt

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
7:May 25, 2020 at 8:22

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

0

Nó sẽ tự động hóa quá trình chuyển sang Python3 mới. Tùy chọn bạn có thể thoát khỏi các gói còn lại sau:

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
5

Đã trả lời ngày 25 tháng 5 năm 2020 lúc 8:22

Đặt mức độ ưu tiên cho Python mặc định trong thiết bị đầu cuối Linux bằng cách thêm điều này:Nov 6, 2020 at 4:52

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
6

Ở đây, chúng tôi đặt

[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
4 để có ưu tiên 10 và
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
6 thành ưu tiên 1. Điều này sẽ làm cho
[sudo] update-alternatives --install /usr/bin/python python /usr/bin/python3 10
4 trở thành python mặc định. Nếu bạn muốn python2 làm mặc định thì hãy ưu tiên python2 cao hơn thì python3Oct 1, 2021 at 9:11

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

1

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
7

Đã trả lời ngày 6 tháng 11 năm 2020 lúc 4:52Jan 3 at 3:46

Đã trả lời ngày 1 tháng 10 năm 2021 lúc 9:11devp

Đã trả lời ngày 3 tháng 1 lúc 3:462 gold badges13 silver badges23 bronze badges

devpdevp

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
8

2.0822 Huy hiệu vàng13 Huy hiệu bạc23 Huy hiệu đồng

Cách tốt nhất trong Ubuntu 18.04 sẽ hoạt động cho tất cả người dùng là

update-alternatives --remove python /usr/bin/python2
update-alternatives --install /usr/bin/python python /usr/bin/python3
9

Đã trả lời ngày 22 tháng 3 năm 2019 lúc 10:12Mar 22, 2019 at 10:12

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Mian Asbat Ahmadmian Asbat AhmadMian Asbat Ahmad

3.02810 Huy hiệu vàng39 Huy hiệu bạc66 Huy hiệu Đồng10 gold badges39 silver badges66 bronze badges

Bạn đã không bao gồm đối số ưu tiên

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
0

Bạn có thể thay thế 5 bằng bất kỳ ưu tiên nào bạn muốn. Một thay thế ưu tiên cao hơn được ưu tiên hơn mức ưu tiên thấp hơn.

Đã trả lời ngày 2 tháng 9 năm 2020 lúc 12:26Sep 2, 2020 at 12:26

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Nếu có khả năng sử dụng trực tiếp phiên bản Python cụ thể, tôi sẽ sử dụng nó so với giải pháp

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
0 và
cd ~
gedit .bash_aliases
0.

Ex.

rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3
update-alternatives: --install needs    

Use 'update-alternatives --help' for program usage information.   
1

cd ~
gedit .bash_aliases
1 thực hiện mô -đun cụ thể cho phiên bản Python cụ thể đó. Dòng đầu tiên sẽ cài đặt pytest cho phiên bản và người dùng cụ thể đó ở vị trí có thể
cd ~
gedit .bash_aliases
2

Đã trả lời ngày 15 tháng 12 năm 2021 lúc 1:21Dec 15, 2021 at 1:21

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

JanpojanpoJanPo

1551 Huy hiệu bạc7 Huy hiệu đồng1 silver badge7 bronze badges

Trong trường hợp của tôi, nó đã xảy ra khi tôi chạy lệnh này trong thiết bị đầu cuối "bí danh python = python3" của tôi

Đã trả lời ngày 29 tháng 5 năm 2021 lúc 3:11May 29, 2021 at 3:11

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

Lúc đầu, đảm bảo Python3 được cài đặt trên máy tính của bạn

Chuyển đến thiết bị đầu cuối của bạn và loại:

cd ~
gedit .bash_aliases
3 để đi đến thư mục nhà của bạn

Nếu bạn chưa thiết lập

cd ~
gedit .bash_aliases
4 của mình, hãy nhập
cd ~
gedit .bash_aliases
5 để tạo .bash_profile của bạn.

Hoặc, nhập

cd ~
gedit .bash_aliases
6 để chỉnh sửa tệp.

Sao chép và lưu

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
2 trong .bash_profile.

Đóng và mở lại thiết bị đầu cuối của bạn. Sau đó nhập lệnh sau để kiểm tra xem Python3 có phải là phiên bản mặc định của bạn không:Python3 is your default version now:

python3 --version
Python 3.7.1 
9

Bạn sẽ thấy Python 3.x.y là phiên bản mặc định của bạn.python 3.x.y is your default version.

Cheers!

Đã trả lời ngày 21 tháng 9 năm 2019 lúc 19:13Sep 21, 2019 at 19:13

Hướng dẫn how do i use python3 instead of python2 linux? - làm cách nào để sử dụng python3 thay vì python2 linux?

1

Làm cách nào để làm cho Python3 mặc định trong Linux?

Linux đặt Python 3 làm mặc định..
Kiểm tra phiên bản hiện tại: $ Python --Version. ....
Nhận quyền gốc: $ sudo su. ....
Đặt Python3 làm mặc định: $ cập nhật-Alternative-install/usr/bin/python python/usr/bin/python3 1. ....
Xong. (Kiểm tra bởi $ Python --Version).

Làm cách nào để buộc một thiết bị đầu cuối trong Python 3?

Để bắt đầu một phiên tương tác Python, chỉ cần mở một dòng lệnh hoặc thiết bị đầu cuối và sau đó nhập Python, hoặc Python3 tùy thuộc vào cài đặt Python của bạn, sau đó nhấn Enter.open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .

Làm cách nào để chuyển đổi giữa các phiên bản trong Python?

Có, bạn sẽ có thể chuyển đổi giữa các phiên bản Python.Theo tiêu chuẩn, nên sử dụng lệnh Python3 hoặc Python3.7 để chọn một phiên bản cụ thể.Trình khởi chạy py.exe sẽ tự động chọn phiên bản Python gần đây nhất mà bạn đã cài đặt.use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed.

Làm cách nào để thay đổi phiên bản Python trong Linux?

Chuyển phiên bản Python trên Ubuntu & Debian..
Tạo một liên kết symlink từ/usr/bin/python2.....
Thay đổi liên kết Symlink thành/usr/bin/python3.....
Lặp lại bước 2 để thêm phiên bản Python vào nhóm, đã được cài đặt trên hệ thống của bạn ..
Tại thời điểm này, bạn đã thêm hai phiên bản nhị phân Python vào tên nhóm là Py Python.....
Đó là nó..