Programming

동일한 Windows 컴퓨터에 Python 3.x 및 2.x를 설치할 수 있습니까?

procodes 2020. 6. 19. 22:05
반응형

동일한 Windows 컴퓨터에 Python 3.x 및 2.x를 설치할 수 있습니까?


Windows를 실행 중이며 명령 줄에서 프로그램을 실행할 때 셸 / OS가 레지스트리 설정에 따라 Python을 자동으로 실행합니다. 동일한 컴퓨터에 2.x 및 3.x 버전의 Python을 설치하면 문제가 발생합니까?

같은 컴퓨터에서 2.x 스크립트를 계속 실행하면서 Python 3을 가지고 놀고 싶습니다.


공존을위한 공식 솔루션 Python 3.3.0 에 포함 된 Windows 용 Python Launcher , PEP 397로 보입니다 . 릴리스 덤프 설치 에 발사기 ( 다음과 관련된) 각각 스크립트.py.exepyw.exe%SYSTEMROOT%C:\Windowspypyw

새 실행기를 사용하려면 (자신의 연결을 수동으로 설정하지 않고) "확장자 등록"옵션을 활성화 된 상태로 두십시오. 왜 그런지 잘 모르겠지만 내 컴퓨터에서는 Py 2.7을 "기본"(런처의)으로 두었습니다.

명령 행에서 직접 호출하여 스크립트를 실행하면 실행기를 통해 스크립트를 라우팅하고 shebang (있는 경우)을 구문 분석합니다. 런처를 명시 적으로 호출하고 스위치를 사용할 수도 있습니다 py -3 mypy2script.py.

모든 방식의 세방이 작동하는 것 같습니다

  • #!C:\Python33\python.exe
  • #!python3
  • #!/usr/bin/env python3

원치 않는 학대뿐만 아니라

  • #! notepad.exe

내 설정은 다음과 같습니다.

  1. Windows 설치 프로그램으로 Python 2.7 및 3.4를 모두 설치하십시오 .
  2. C:\Python34(기본 설치 경로)로 이동하여 python.exe를 python3.exe로 변경하십시오.
  3. 포함 할 환경 변수 편집C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

이제 명령 행 python에서 2.7 및 python33.4에 사용할 수 있습니다 .


둘 다 설치할 수 있습니다.

스크립트 앞에 이것을 작성해야합니다.

#!/bin/env python2.7

또는 결국 ...

#!/bin/env python3.6

최신 정보

내 솔루션은 Google 에서 빠른 검색 후 Unix와 완벽하게 작동 하며 Windows 솔루션은 다음과 같습니다.

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

같은 것 : 스크립트 앞에서.


버전 3.3부터 Python은 Windows 용 Launcher 유틸리티 https://docs.python.org/3/using/windows.html#python-launcher-for-windows 를 도입했습니다 .

따라서 여러 버전의 Python을 사용할 수 있습니다.

  1. Python 2.x 설치 (x는 필요한 버전 임)
  2. Python 3.x를 설치하십시오 (x는 필요한 버전입니다. 3.x> = 3.3 버전이 있어야합니다)
  3. 명령 프롬프트 열기
  4. py -2.x입력 하여 Python 2.x를 시작하십시오.
  5. py -3.x입력 하여 Python 3.x를 시작하십시오.

쉘의 2.5, 2.6 및 3.0을 한 줄 배치 스크립트 형식으로 사용하고 있습니다.

:: 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 %*

그것들의 이름을 지정하고 pythonX.Y.batPATH 어딘가에 넣으십시오. 선호하는 부 버전 (즉, 최신 버전)의 파일을로 복사하십시오 pythonX.bat. (예 :) 어디서나 copy python2.6.bat python2.bat사용할 수 있습니다 python2 file.py.

그러나 이것은 Windows 파일 연결 상황에 도움이되지 않거나 심지어 영향을 미치지 않습니다. 이를 위해서는 #!라인 을 읽은 다음 .py 및 .pyw 파일과 연결 하는 시작 프로그램이 필요 합니다.


환경 변수에 두 가지를 모두 추가하면 두 실행 파일의 이름이 동일하기 때문에 충돌이 발생합니다 python.exe.

그중 하나의 이름 만 바꾸십시오. 제 경우에는 이름을로 변경했습니다 python3.exe.

내가 도망 그래서 python그것을 실행할 python.exe2.7 인 내가 실행할 때 python3이 실행됩니다 python3.exe이는 3.6

여기에 이미지 설명을 입력하십시오


여기 있습니다 ...

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 <script.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)

방금이 스레드를 읽었습니다. (필요한 것이기 때문에). 우분투와 Windows 모두에 Pythons 2.6.1과 3.0.1이 있습니다. 그래도 문제가 해결되지 않으면 여기에 수정 사항을 게시하십시오.


내가 아는 한 파이썬은 레지스트리 설정과 달리 PATH 변수를 사용하여 명령 줄을 벗어납니다.

따라서 PATH에서 올바른 버전을 가리키면 해당 버전을 사용합니다. 새 PATH 설정을 사용하려면 명령 프롬프트를 다시 시작하십시오.


The Python installation normally associates .py, .pyw and .pyc files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type python scriptname.py, just scriptname.py will do).

If you want to manually change this association, you can edit these keys in the Windows registry:

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

People have been working on a Python launcher for Windows: a lightweight program associated with .py and .pyw files which would look for a "shebang" line (similar to Linux et al) on the first line, and launch Python 2.x or 3.x as required. See "A Python Launcher for Windows" blog post for details.


Try using Anaconda.

Using the concept of Anaconda environments, let’s say you need Python 3 to learn programming, but you don’t want to wipe out your Python 2.7 environment by updating Python. You can create and activate a new environment named "snakes" (or whatever you want), and install the latest version of Python 3 as follows:

conda create --name snakes python=3

Its simplier than it sounds, take a look at the intro page here: Getting Started with Anaconda

And then to handle your specific problem of having version 2.x and 3.x running side by side, see : Managing Python Versions with Anaconda


Here is how to run Python 2 and 3 on the same machine

  1. install Python 2.x
  2. install Python 3.x
  3. Start Powershell
  4. Type Python -2 to launch Python 2.x
  5. Type Python -3 to launch Python 2.x

The Python Launcher for Windows was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:

Python Launcher for Windows


Before I courageously installed both simultaneously, I had so many questions. If I give python will it go to py3 when i want py2? pip/virtualenv will happen under py2/3?

It seems to be very simple now.

Just blindly install both of them. Make sure you get the right type(x64/x32). While/after installing make sure you add to the path to your environment variables.

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

Replace the x in the command above to set the path.

Then go to both the folders.

Navigate to

python3.6/Scripts/

and rename pip to pip3.

If pip3 already exists delete the pip. This will make sure that just pip will run under python2. You can verify by:

pip --version

In case you want to use pip with python3 then just use

pip3 install 

You can similarly do the same to python file and others.

Cheers!


I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.

If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select "open with" / "choose program". On the dialog that appears, select or browse to the version of python you want to use by default, and check the "always use this program to open this kind of file" checkbox.


You should make sure that the PATH environment variable doesn't contain both python.exe files ( add the one you're currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files . Aside from that , I don't see why not .

P.S : I have 2.6 installed as my "primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .


Easy-peasy ,after installing both the python versions add the paths to the environment variables ;see환경 변수 설정. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown python2 여기 and python3 여기. Now in cmd type python2 or python3 to use your required version see 여기.


I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.


I am just starting out with python now. I'm reading Zed Shaw's book "Learn Python the Hard Way" which requires python version 2.x but am also taking a class that requires python 3.x

So here is what I did.

  1. Download python 2.7
  2. run power shell (should already be installed on windows)
  3. run python IN POWERSHELL (if it doesn't recognize then go to step 4)
  4. Only if powershell doesn't recognize python 2.7 type in the following:

"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")" (no outside quotes)

  1. Now type python and you should see it say python 2.7 blah blah blah

NOW for python 3.x

Simple, python 3.x download comes with python for windows app. SO simply pin the Python for Windows app to your task bar, or create shortcut to the desktop and you are done!

Open Python for Windows for 3.x

Open Powershell for python 2.x

I hope this helps!


Hmm..I did this right now by just downloading Python 3.6.5 for Windows at https://www.python.org/downloads/release/python-365/ and made sure that the launcher would be installed. Then, I followed the instructions for using python 2 and python 3. Restart the command prompt and then use py -2.7 to use Python 2 and py or py -3.6 to use Python 3. You can also use pip2 for Python 2's pip and pip for Python 3's pip.

참고 URL : https://stackoverflow.com/questions/341184/can-i-install-python-3-x-and-2-x-on-the-same-windows-computer

반응형