Programing

Windows에서 여러 Python 버전을 실행하는 방법

lottogame 2020. 6. 7. 00:41
반응형

Windows에서 여러 Python 버전을 실행하는 방법


내 컴퓨터에 두 가지 버전의 Python이 설치되었습니다 (버전 2.6 및 2.5). 한 프로젝트에는 2.6을, 다른 프로젝트에는 2.5를 실행하고 싶습니다.

사용할 것을 어떻게 지정합니까?

Windows XP SP2에서 작업하고 있습니다.


올바른 실행 파일을 시작하는 것만 큼 다른 Python 사본을 실행하는 것은 쉽습니다. 명령 행에서 간단히을 입력하여 Python 인스턴스를 시작했다고 언급했습니다 python.

Windows에서이 작업은 % PATH % 환경 변수를 실행하여 배치 파일 (.bat), 명령 파일 (.cmd) 또는 실행중인 다른 실행 파일 중 실행 파일을 검사하여 지정된 이름과 일치하는지 확인합니다. 실행할 올바른 파일을 찾으면 수행합니다.

이제 두 개의 Python 버전 2.5 및 2.6을 설치 한 경우 경로에는 두 디렉토리가 모두 들어 PATH=c:\python\2.5;c:\python\2.6있지만 Windows는 일치하는 경로를 찾으면 경로 검사를 중지합니다.

당신이 정말로 필요가 명시 적으로 같은 응용 프로그램 중 하나 또는 둘 모두를 호출하는 것입니다 c:\python\2.5\python.exe또는 c:\python\2.6\python.exe.

다른 대안은 각각의 python.exe에 대한 단축 아이콘을 python25와 다른 python26으로 만드는 것입니다. 그런 다음 python25명령 줄에서 간단히 실행할 수 있습니다 .


문제에 대한 두 가지 솔루션 추가 :

  • pylauncher를 사용 하고 (Python 3.3 이상이 설치된 경우 Python과 함께 제공 되므로 설치할 필요가 없음 ) 스크립트에 shebang 행을 추가하십시오.

#! c:\[path to Python 2.5]\python.exe-Python 2.5
#! c:\[path to Python 2.6]\python.exe로 실행하려는 스크립트의 경우-Python 2.6으로 실행하려는 스크립트의 경우

또는 python명령을 실행 하는 대신 py원하는 Python 버전을 지정하는 pylauncher 명령 ( )을 실행하십시오 .

py -2.6– 버전 2.6
py -2– 최신 설치 버전 2.x
py -3.4– 버전 3.4
py -3– 최신 설치 버전 3.x

  • virtualenv를 설치 하고 두 개의 virtualenv를 작성하십시오.

virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]

virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]

예를 들어

virtualenv -p c:\python2.5\python.exe c:\venvs\2.5

virtualenv -p c:\python2.6\python.exe c:\venvs\2.6

그런 다음 첫 번째를 활성화하고 이와 같이 Python 2.5로 작업 할 수 있으며
c:\venvs\2.5\activate
Python 2.6으로 전환하고 싶을 때

deactivate  
c:\venvs\2.6\activate

Python 3.3부터 Windows 용 공식 Python 런처가 있습니다 ( http://www.python.org/dev/peps/pep-0397/ ). 이제를 사용하여 #!pythonXWindows에서도 원하는 인터프리터 버전을 확인할 수 있습니다 . 다른 의견에서 자세한 내용 보거나 PEP 397을 읽으십시오.

요약 :py script.py 파이썬 버전에 명시된 출시 #!하면 파이썬 2 #!가 없습니다. py -3 script.py파이썬 3 시작됩니다.


@alexander에 따라 아래와 같이 기호 링크 세트를 만들 수 있습니다. 쉽게 호출 할 수 있도록 경로에 포함 된 어딘가에 놓으십시오.

> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe

c : \ bin 또는 어디를 놓아 두었 든 이제는 갈 수 있습니다

> python25

Python을 설치할 때 다른 주요 버전의 다른 설치를 덮어 쓰지 않습니다. 따라서 2.6.6을 설치하면 2.6.5를 덮어 쓰지만 Python 2.5.x를 설치하면 Python 2.6.x를 덮어 쓰지 않습니다.

설치 만하면됩니다. 그런 다음 원하는 Python 버전을 호출합니다. 예를 들면 다음과 같습니다.

C:\Python2.5\Python.exe

Windows의 Python 2.5 및

C:\Python2.6\Python.exe

Windows의 Python 2.6 또는

/usr/local/bin/python-2.5

또는

/usr/local/bin/python-2.6

윈도우 (Linux 및 OS X 포함) 유닉스.

Unix (Linux 및 OS X 포함)에 python설치하면 마지막으로 설치 되는 일반 명령이 설치됩니다. 대부분의 스크립트가 명시 적으로 /usr/local/bin/python2.5 또는 그로부터 보호하기 위해 무언가를 호출하기 때문에 이것은 문제가되지 않습니다. 그러나 그렇게하고 싶지 않으면 다음과 같이 설치할 수 없습니다.

./configure
make
sudo make altinstall

Note the "altinstall" that means it will install it, but it will not replace the python command.

On Windows you don't get a global python command as far as I know so that's not an issue.


  1. install python

    • C:\Python27
    • C:\Python36
  2. environment variable

    • PYTHON2_HOME: C:\Python27
    • PYTHON3_HOME: C:\Python36
    • Path: %PYTHON2_HOME%;%PYTHON2_HOME%\Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%\Scripts;
  3. file rename

    • C:\Python27\python.exe → C:\Python27\python2.exe
    • C:\Python36\python.exe → C:\Python36\python3.exe
  4. pip

    • python2 -m pip install package
    • python3 -m pip install package

Here's a quick hack:

  1. Go to the directory of the version of python you want to run
  2. Right click on python.exe
  3. Select 'Create Shortcut'
  4. Give that shortcut a name to call by( I use p27, p33 etc.)
  5. Move that shortcut to your home directory(C:\Users\Your name)
  6. Open a command prompt and enter name_of_your_shortcut.lnk(I use p27.lnk)

I strongly recommend the pyenv-win project.

enter image description here

Thanks to kirankotari's work, now we have a Windows version of pyenv.


cp c:\python27\bin\python.exe as python2.7.exe

cp c:\python34\bin\python.exe as python3.4.exe

they are all in the system path, choose the version you want to run

C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

For example for 3.6 version type py -3.6. If you have also 32bit and 64bit versions, you can just type py -3.6-64 or py -3.6-32.


The easiest way to run multiple versions of python on windows is described below as follows:-

1)Download the latest versions of python from python.org/downloads by selecting the relevant version for your system.

2)Run the installer and select Add python 3.x to the path to set path automatically in python 3 (you just have to click the checkbox). For python 2 open up your python 2 installer, select whatever preferences you want but just remember to set Add python.exe to path to Will be installed on local hard drive, Now just click next and wait for the installer to finish.

3)When both the installations are complete. Right click on my computer--Go to properties--Select advanced system settings--Go to environment variables--Click on new under System variables and add a new system variable with variable name as PY_PYTHON and set this variable value to 3. Now click on OK and you should be done.

4)Now to test this open the command prompt. Once you are in there type python or py, It should open up python3.

5)Now exit out of python3 by typing exit(). Now type py -2 it should open python 2.

If none of this works then restart the computer and if the problem still persists then uninstall everything and repeat the steps.

Thanks.


Using a batch file to switch, easy and efficient on windows 7. I use this:

In the environment variable dialog (C:\Windows\System32\SystemPropertiesAdvanced.exe),

In the section user variables

  1. added %pathpython% to the path environment variable

  2. removed any references to python pathes

In the section system variables

  1. removed any references to python pathes

I created batch files for every python installation (exmple for 3.4 x64

Name = SetPathPython34x64 !!! ToExecuteAsAdmin.bat ;-) just to remember.

Content of the file =

     Set PathPython=C:\Python36AMD64\Scripts\;C:\Python36AMD64\;C:\Tcl\bin

     setx PathPython %PathPython%

To switch between versions, I execute the batch file in admin mode.

!!!!! The changes are effective for the SUBSEQUENT command prompt windows OPENED. !!!

So I have exact control on it.


I had same problem while working with different python versions so I used anaconda navigator to create different python development environments and used different python versions in each environments.

Here is the help documentation for this.

https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/


  1. You can copy the python.exe from python 2.5 folder to C:\Windows and rename it to py25.exe (or any name you want).
  2. then copy the python.exe from python 2.6 folder and paste it in the C:\Windows folder (rename it to py26.exe).
    Now you can write py25 in the command line to start python 2.5 and py26 to start python 2.6.
    The name of the command should match the renamed file.

Using the Rapid Environment Editor you can push to the top the directory of the desired Python installation. For example, to start python from the c:\Python27 directory, ensure that c:\Python27 directory is before or on top of the c:\Python36 directory in the Path environment variable. From my experience, the first python executable found in the Path environment is being executed. For example, I have MSYS2 installed with Python27 and since I've added C:\MSYS2 to the path before C:\Python36, the python.exe from the C:\MSYS2.... folder is being executed.


Just call the correct executable

참고URL : https://stackoverflow.com/questions/4583367/how-to-run-multiple-python-versions-on-windows

반응형