Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Tôi đang chạy Windows và Shell/OS tự động chạy Python dựa trên cài đặt đăng ký khi bạn chạy một chương trình trên dòng lệnh. Điều này sẽ bị hỏng nếu tôi cài đặt phiên bản Python 2.x và 3.x trên cùng một máy?

Tôi muốn chơi với Python 3 trong khi vẫn có thể chạy các tập lệnh 2.x trên cùng một máy.

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Alex

6.1009 Huy hiệu vàng49 Huy hiệu bạc102 Huy hiệu Đồng9 gold badges49 silver badges102 bronze badges

Hỏi ngày 4 tháng 12 năm 2008 lúc 16:18Dec 4, 2008 at 16:18

4

Giải pháp chính thức cho sự cùng tồn tại dường như là trình khởi chạy Python cho Windows, PEP 397 được đưa vào Python 3.3.0. Cài đặt các kết xuất phát hành

#!/bin/env python3.6
2 và
#!/bin/env python3.6
3 Launcher vào
#!/bin/env python3.6
4 (
#!/bin/env python3.6
5) sau đó được liên kết với các tập lệnh
#!/bin/env python3.6
6 và
#!/bin/env python3.6
7.

Để sử dụng trình khởi chạy mới (không cần thiết lập thủ công các liên kết của riêng bạn cho nó), hãy bật tùy chọn "Tiện ích mở rộng đăng ký". Tôi không hoàn toàn chắc chắn tại sao, nhưng trên máy của tôi, nó đã để lại PY 2.7 là "mặc định" (của trình khởi chạy).

Chạy các tập lệnh bằng cách gọi chúng trực tiếp từ dòng lệnh sẽ định tuyến chúng qua trình khởi chạy và phân tích shebang (nếu nó tồn tại). Bạn cũng có thể gọi rõ ràng bộ khởi chạy và sử dụng các công tắc:

#!/bin/env python3.6
8.

Tất cả các loại shebang dường như hoạt động

  • #!/bin/env python3.6
    
    9
  • #!c:/Python/python3_6.exe -u
    
    0
  • #!c:/Python/python3_6.exe -u
    
    1

cũng như lạm dụng bừa bãi

  • #!c:/Python/python3_6.exe -u
    
    2

Đã trả lời ngày 8 tháng 11 năm 2012 lúc 21:11Nov 8, 2012 at 21:11

Nick Tnick tNick T

24.8K11 Huy hiệu vàng79 Huy hiệu bạc118 Huy hiệu đồng11 gold badges79 silver badges118 bronze badges

8

Đây là thiết lập của tôi:

  1. Cài đặt cả Python 2.7 và 3.4 với trình cài đặt Windows.
  2. Chuyển đến
    #!c:/Python/python3_6.exe -u
    
    3 (đường dẫn cài đặt mặc định) và thay đổi python.exe thành python3.exe
  3. Chỉnh sửa các biến môi trường của bạn để bao gồm
    #!c:/Python/python3_6.exe -u
    
    4
    your environment variables to include
    #!c:/Python/python3_6.exe -u
    
    4

Bây giờ trong dòng lệnh, bạn có thể sử dụng

#!c:/Python/python3_6.exe -u
5 cho 2.7 và
#!c:/Python/python3_6.exe -u
6 cho 3,4.

Đã trả lời ngày 25 tháng 8 năm 2015 lúc 5:15Aug 25, 2015 at 5:15

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

6

Từ phiên bản 3.3 Python đã giới thiệu trình khởi chạy cho Windows Utility https://docs.python.org/3/using/windows.html#python-launcher-for-windows.

Vì vậy, để có thể sử dụng nhiều phiên bản Python:

  1. Cài đặt Python 2.x (x là bất kỳ phiên bản nào bạn cần)
  2. Cài đặt Python 3.x (x là bất kỳ phiên bản nào bạn cần bạn cũng phải có một phiên bản 3.x> = 3.3)
  3. Mở dấu nhắc lệnhCommand Prompt
  4. Loại PY -2.x để khởi chạy Python 2.xpy -2.x to launch Python 2.x
  5. Loại PY -3.x để khởi chạy Python 3.xpy -3.x to launch Python 3.x

Đã trả lời ngày 29 tháng 6 năm 2016 lúc 8:18Jun 29, 2016 at 8:18

3

Bạn có thể cài đặt cả hai.

Bạn nên viết cái này trước kịch bản của bạn:

#!/bin/env python2.7

Hoặc, cuối cùng ...

#!/bin/env python3.6

Cập nhật

Giải pháp của tôi hoạt động hoàn hảo với Unix, sau khi tìm kiếm nhanh trên Google, đây là giải pháp Windows:

#!c:/Python/python3_6.exe -u

Điều tương tự: trước kịch bản của bạn.

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Atilioa

3811 Huy hiệu vàng5 Huy hiệu bạc17 Huy hiệu đồng1 gold badge5 silver badges17 bronze badges

Đã trả lời ngày 4 tháng 12 năm 2008 lúc 16:29Dec 4, 2008 at 16:29

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

6

Dưới đây là một cách gọn gàng và sạch sẽ để cài đặt Python2 & Python3 trên Windows.

https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

Trường hợp của tôi: Tôi đã phải cài đặt Apache Cassandra. Tôi đã cài đặt Python3 trong ổ D: Drive. Với vô số công việc phát triển đang được xử lý, tôi đã không muốn gây rối cài đặt Python3 của mình. Và, tôi chỉ cần Python2 cho Apache Cassandra. I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work under process i didn't wanted to mess my Python3 installation. And, i needed Python2 only for Apache cassandra.

Vì vậy, tôi đã thực hiện các bước sau:

  1. Tải xuống & cài đặt Python2.
  2. Đã thêm các mục python2 vào classpath (
    #!c:/Python/python3_6.exe -u
    
    7)
  3. Python.exe đã sửa đổi thành Python2.exe (như trong hình ảnh bên dưới)python.exe to python2.exe (as shown in image below)

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

  1. Bây giờ tôi có thể chạy cả hai. Cho Python 2 (________ 28) & Python 3 (
    #!c:/Python/python3_6.exe -u
    
    9).
    Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Vì vậy, cài đặt Python3 của tôi vẫn còn nguyên vẹn.

Đã trả lời ngày 8 tháng 10 năm 2019 lúc 11:49Oct 8, 2019 at 11:49

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

ManjeetmanjeetManjeet

87515 Huy hiệu bạc23 Huy hiệu đồng15 silver badges23 bronze badges

2

Tôi đang sử dụng 2.5, 2.6 và 3.0 từ shell với một loạt dòng chữ của biểu mẫu:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*

Đặt tên cho chúng

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
0 và đặt chúng ở đâu đó trên con đường của bạn. Sao chép tệp cho phiên bản nhỏ ưa thích (tức là mới nhất) thành
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
1. (Ví dụ:
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
2.) Sau đó, bạn có thể sử dụng
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
3 từ bất cứ đâu.

Tuy nhiên, điều này không giúp ích hoặc thậm chí ảnh hưởng đến tình huống liên kết tệp Windows. Cho rằng bạn sẽ cần một chương trình trình khởi chạy đọc dòng

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
4, sau đó liên kết với các tệp .py và .pyw.

Đã trả lời ngày 12 tháng 1 năm 2009 lúc 18:26Jan 12, 2009 at 18:26

1

Khi bạn thêm cả hai vào các biến môi trường, sẽ là một cuộc xung đột vì hai thực thi có cùng tên:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
5.

Chỉ cần đổi tên một trong số họ. Trong trường hợp của tôi, tôi đã đổi tên nó thành

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
6.

Vì vậy, khi tôi chạy

#!c:/Python/python3_6.exe -u
5, nó sẽ thực thi
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
5 là 2.7 và khi tôi chạy
#!c:/Python/python3_6.exe -u
6, nó sẽ thực thi
:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*
6 là 3,6

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Đã trả lời ngày 19 tháng 10 năm 2017 lúc 8:24Oct 19, 2017 at 8:24

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Charif Dzarif dzCharif DZ

14.2k3 Huy hiệu vàng19 Huy hiệu bạc39 Huy hiệu đồng3 gold badges19 silver badges39 bronze badges

3

Bạn đi đây ...

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

Tôi đã đánh bật điều này khi đọc chủ đề này (bởi vì đó cũng là những gì tôi cần). Tôi có Pythons 2.6.1 và 3.0.1 trên cả Ubuntu và Windows. Nếu nó không hoạt động cho bạn đăng bản sửa lỗi ở đây.

Đã trả lời ngày 18 tháng 4 năm 2009 lúc 1:45Apr 18, 2009 at 1:45

1

Hãy thử sử dụng Anaconda.

Sử dụng khái niệm về môi trường Anaconda, hãy để nói rằng bạn cần Python 3 để học lập trình, nhưng bạn không muốn xóa sạch môi trường Python 2.7 của mình bằng cách cập nhật Python. Bạn có thể tạo và kích hoạt một môi trường mới có tên "Rắn" (hoặc bất cứ điều gì bạn muốn) và cài đặt phiên bản mới nhất của Python 3 như sau:

conda create --name snakes python=3

Nó đơn giản hơn âm thanh của nó, hãy xem trang giới thiệu ở đây: Bắt đầu với Anaconda

Và sau đó để xử lý vấn đề cụ thể của bạn khi có phiên bản 2.x và 3.x chạy cạnh nhau, xem:

  • Quản lý các phiên bản Python với Anaconda
  • Chuyển đổi giữa môi trường Python 2 và Python 3

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Đã trả lời ngày 16 tháng 5 năm 2017 lúc 19:23May 16, 2017 at 19:23

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Cale Sweeneycale SweeneyCale Sweeney

9741 Huy hiệu vàng14 Huy hiệu bạc34 Huy hiệu đồng1 gold badge14 silver badges34 bronze badges

0

Theo như tôi biết Python sẽ chạy khỏi dòng lệnh bằng cách sử dụng biến đường dẫn trái ngược với cài đặt đăng ký.

Vì vậy, nếu bạn chỉ vào phiên bản chính xác trên đường dẫn của bạn, bạn sẽ sử dụng nó. Hãy nhớ khởi động lại lời nhắc lệnh của bạn để sử dụng cài đặt đường dẫn mới.

Đã trả lời ngày 4 tháng 12 năm 2008 lúc 16:29Dec 4, 2008 at 16:29

James McMahonjames McMahonJames McMahon

47.7K64 Huy hiệu vàng206 Huy hiệu bạc279 Huy hiệu Đồng64 gold badges206 silver badges279 bronze badges

2

Cài đặt Python thường liên kết các tệp

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
1,
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
2 và
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
3 với trình thông dịch Python. Vì vậy, bạn có thể chạy tập lệnh Python bằng cách nhấp đúp vào nó trong Explorer hoặc bằng cách nhập tên của nó vào cửa sổ dòng lệnh (vì vậy không cần nhập
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
4, chỉ cần
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
5 sẽ làm).

Nếu bạn muốn thay đổi thủ công liên kết này, bạn có thể chỉnh sửa các khóa này trong sổ đăng ký Windows:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

Python Launcher

Mọi người đã làm việc trên một trình khởi chạy Python cho Windows: một chương trình nhẹ liên quan đến các tệp

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
1 và
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
2 sẽ tìm kiếm một dòng "shebang" (tương tự như Linux et al) trên dòng đầu tiên và khởi chạy Python 2.x hoặc 3. x theo yêu cầu. Xem bài đăng "A Python Launcher cho Windows" để biết chi tiết.

Đã trả lời ngày 3 tháng 3 năm 2009 lúc 0:57Mar 3, 2009 at 0:57

Craig McQueencraig McQueenCraig McQueen

40.5K28 Huy hiệu vàng123 Huy hiệu bạc178 Huy hiệu đồng28 gold badges123 silver badges178 bronze badges

Đây là cách chạy Python 2 và 3 trên cùng một máy

  1. Cài đặt Python 2.x
  2. Cài đặt Python 3.x
  3. Bắt đầu PowerShell
  4. Loại Python -2 để khởi chạy Python 2.xPython -2 to launch Python 2.x
  5. Loại Python -3 để khởi chạy Python 3.xPython -3 to launch Python 3.x

Trình khởi chạy Python cho Windows đã được nhúng vào Python kể từ phiên bản 3.3, như đã hứa vào năm 2011 khi The Stand Breuning lần đầu tiên ra mắt:

Python Launcher cho Windows

Đã trả lời ngày 3 tháng 1 năm 2016 lúc 3:10Jan 3, 2016 at 3:10

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

1

Dễ dàng, sau khi cài đặt cả hai phiên bản Python thêm các đường dẫn vào các biến môi trường; xem. Sau đó truy cập Python 2 và Python 3 thư mục và đổi tên chúng thành Python2 và Python3 tương ứng như được hiển thị và. Bây giờ trong CMD loại Python2 hoặc Python3 để sử dụng phiên bản cần thiết của bạn, xem.

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?
. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown
Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?
and
Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?
. Now in cmd type python2 or python3 to use your required version see
Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?
.

Đã trả lời ngày 20 tháng 6 năm 2019 lúc 3:11Jun 20, 2019 at 3:11

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Tôi nghĩ rằng có một tùy chọn để thiết lập liên kết tệp Windows cho các tệp .py trong trình cài đặt. Bỏ chọn nó và bạn sẽ ổn.

Nếu không, bạn có thể dễ dàng liên kết lại các tệp .py với phiên bản trước. Cách đơn giản nhất là nhấp chuột phải vào tệp .py, chọn "Mở bằng" / "chọn chương trình". Trên hộp thoại xuất hiện, chọn hoặc duyệt đến phiên bản Python bạn muốn sử dụng theo mặc định và kiểm tra hộp kiểm "Luôn sử dụng chương trình này để mở loại tệp này".

Đã trả lời ngày 4 tháng 12 năm 2008 lúc 16:33Dec 4, 2008 at 16:33

BrianbrianBrian

Huy hiệu vàng 115K2828 gold badges107 silver badges111 bronze badges

1

Bạn nên đảm bảo rằng biến môi trường đường dẫn không chứa cả các tệp python.exe (thêm tệp bạn hiện đang sử dụng để chạy tập lệnh hàng ngày) hoặc làm như KNIHT đề xuất với các tệp Batch. Bên cạnh đó, tôi không hiểu tại sao không.

P.S: Tôi có 2.6 được cài đặt dưới dạng python "chính" chính và 3.0 là python "play" của tôi. 2.6 được bao gồm trong đường dẫn. Mọi thứ đều hoạt động tốt ."primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .

Đã trả lời ngày 17 tháng 1 năm 2009 lúc 16:53Jan 17, 2009 at 16:53

GEOGEOGeo

91.3K115 Huy hiệu vàng338 Huy hiệu bạc515 Huy hiệu Đồng115 gold badges338 silver badges515 bronze badges

Trước khi tôi can đảm cài đặt cả hai cùng một lúc, tôi đã có rất nhiều câu hỏi. Nếu tôi cho Python, nó sẽ đến PY3 khi tôi muốn PY2? PIP/VirtualEnv sẽ xảy ra theo PY2/3?

Nó có vẻ rất đơn giản bây giờ.

Chỉ cần cài đặt một cách mù quáng cả hai. Hãy chắc chắn rằng bạn có được loại đúng (x64/x32). Trong khi/sau khi cài đặt, hãy đảm bảo bạn thêm vào đường dẫn vào các biến môi trường của bạn.

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")

Thay thế X trong lệnh ở trên để đặt đường dẫn.

Sau đó đi đến cả hai thư mục.

Hướng đến

python3.6/Scripts/

và đổi tên PIP thành PIP3.

Nếu PIP3 đã tồn tại, hãy xóa PIP. Điều này sẽ đảm bảo rằng PIP sẽ chạy theo Python2. Bạn có thể xác minh bằng:python2. You can verify by:

pip --version

Trong trường hợp bạn muốn sử dụng pip với python3 thì chỉ cần sử dụngpython3 then just use

#!/bin/env python3.6
0

Bạn cũng có thể làm tương tự với tệp Python và các tệp khác.

Cheers!

Đã trả lời ngày 12 tháng 3 năm 2017 lúc 9:32Mar 12, 2017 at 9:32

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Tôi đã có cùng một vấn đề mà tôi muốn sử dụng Python3 cho hầu hết các công việc nhưng Ida Pro yêu cầu Python2. Vì vậy, đây là những gì tôi đã làm.

Đầu tiên tôi đã tạo 3 biến trong biến môi trường người dùng như sau:

  1. Python_active: ban đầu là trống
  2. Home_python27: có một đường dẫn đến một thư mục nơi cài đặt Python 2. Ví dụ. ";/kịch bản;"
  3. Home_python38: Tương tự như Python 2, biến này chứa một đường dẫn đến Python 3 thư mục.

Bây giờ tôi đã thêm

%PYTHON_ACTIVE%

đến biến đường dẫn. Vì vậy, về cơ bản nói rằng bất cứ điều gì "python_active" này đều chứa là python hoạt động. Chúng tôi lập trình thay đổi một cách tự nhiên của "python_active" để chuyển phiên bản Python.

Đây là tập lệnh ví dụ:

#!/bin/env python3.6
1

Kịch bản này lấy phiên bản Python làm đầu vào và theo đó sao chép home_python27 hoặc home_python38 thành python_active. Do đó thay đổi phiên bản Python toàn cầu.

Đã trả lời ngày 17 tháng 5 năm 2020 lúc 9:25May 17, 2020 at 9:25

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Mandar Sadyemandar SadyeMandar Sadye

6872 Huy hiệu vàng9 Huy hiệu bạc29 Huy hiệu đồng2 gold badges9 silver badges29 bronze badges

Tôi sẽ giả sử như vậy, tôi có Python 2.4, 2.5 và 2.6 được cài đặt cạnh nhau trên cùng một máy tính.

Đã trả lời ngày 4 tháng 12 năm 2008 lúc 17:31Dec 4, 2008 at 17:31

1

Bây giờ tôi mới bắt đầu với Python. Tôi đang đọc cuốn sách của Zed Shaw "Học Python theo cách khó khăn", đòi hỏi Python phiên bản 2.x nhưng cũng đang tham gia một lớp yêu cầu Python 3.x

Vì vậy, đây là những gì tôi đã làm.

  1. Tải xuống Python 2.7
  2. Run Power Shell (nên được cài đặt trên Windows)
  3. Chạy Python trong PowerShell (nếu nó không nhận ra thì hãy chuyển sang bước 4)
  4. Chỉ khi PowerShell không nhận ra loại Python 2.7 theo cách sau: type in the following:

"[Môi trường] :: SetEnvironmentVarable (" đường dẫn "," $ env: path; c: \ python27 "," user ")" (không có báo giá bên ngoài)

  1. Bây giờ hãy gõ python và bạn sẽ thấy nó nói python 2.7 blah blah blah

Bây giờ cho Python 3.x

Tải xuống đơn giản, Python 3.x đi kèm với ứng dụng Python cho Windows. Vì vậy, chỉ cần ghim ứng dụng Python cho Windows vào thanh tác vụ của bạn hoặc tạo lối tắt vào máy tính để bàn và bạn đã hoàn tất!

Mở Python cho Windows cho 3.x

Mở PowerShell cho Python 2.x

Tôi hi vọng cái này giúp được!

Đã trả lời ngày 7 tháng 2 năm 2017 lúc 0:46Feb 7, 2017 at 0:46

Hướng dẫn can python 2 and 3 coexist windows? - cửa sổ python 2 và 3 có thể cùng tồn tại không?

Hmm..Tôi đã làm điều này ngay bây giờ bằng cách tải xuống Python 3.6.5 cho Windows tại https://www.python.org/doads/release/python-365/ và đảm bảo rằng trình khởi chạy sẽ được cài đặt. Sau đó, tôi đã làm theo các hướng dẫn sử dụng Python 2 và Python 3. Khởi động lại dấu nhắc lệnh và sau đó sử dụng

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py  [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)
8 để sử dụng Python 2 và
#!/bin/env python3.6
6 hoặc
conda create --name snakes python=3
0 để sử dụng Python 3. Bạn cũng có thể sử dụng
conda create --name snakes python=3
1 cho Python 2 .

Đã trả lời ngày 7 tháng 6 năm 2018 lúc 1:37Jun 7, 2018 at 1:37

FearlessfuturefearlessfutureFearlessFuture

2.1203 Huy hiệu vàng18 Huy hiệu bạc25 Huy hiệu Đồng3 gold badges18 silver badges25 bronze badges

Tôi có thể có cả Python 2 và Python 3 không?

Phiên bản ổn định mới nhất là Python 3.9 được phát hành vào năm 2020. Bản chất của Python 3 là những thay đổi được thực hiện trong Python 3 làm cho nó không tương thích với Python 2. Vì vậy, nó không tương thích và mã được viết trong Python 3 sẽ không hoạt động trên Python 2 mà không cần sửa đổi.code written in python 3 will not work on python 2 without modifications.

Chúng ta có thể có hai phiên bản Python trong Windows không?

Cài đặt nhiều phiên bản Python cho người dùng Windows, tôi khuyên bạn nên sử dụng tùy chọn Trình cài đặt thực thi Windows X86-64 nếu bạn hoạt động trên hệ thống 64bit.Nếu không, chỉ cần sử dụng trình cài đặt thực thi Windows X86.Sau khi định vị tùy chọn cài đặt cho phiên bản cụ thể, chỉ cần nhấn liên kết tải xuống.I recommend using the Windows x86-64 executable installer option if you work on a 64bit system. Otherwise, just use the Windows x86 executable installer . After locating the install option for the specific version, just press the download link.

Python 2 có tương thích ngược với Python 3 không?

Python 3 không tương thích ngược với Python 2, vì vậy mã của bạn có thể cần phải được điều chỉnh.Vui lòng bắt đầu di chuyển mã Python 2 hiện tại của bạn sang Python 3., so your code may need to be adapted. Please start migrating your existing your existing Python 2 code to Python 3.

Tôi có thể có nhiều phiên bản Python không?

Với Navigator, bạn có thể có các phiên bản Python khác nhau trên máy tính của mình và dễ dàng chuyển đổi giữa chúng bằng cách giữ chúng trong các môi trường khác nhau.Để sử dụng một phiên bản khác của Python so với phiên bản bạn hiện đang sử dụng, hãy thiết lập một môi trường mới: bắt đầu điều hướng. and easily switch between them by keeping them in different environments. To use a different version of Python than the one you are currently using, set up a new environment: Start Navigator.