Running a different copy of Python is as easy as starting the correct executable. You mention that you’ve started a python instance, from the command line, by simply typing python.
What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.
Now, if you’ve installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.
What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.
The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.
Use pylauncher (if you have Python 3.3 or newer there’s no need to install it as it comes with Python already) and either add shebang lines to your scripts;
#! c:\[path to Python 2.5]\python.exe – for scripts you want to be run with Python 2.5 #! c:\[path to Python 2.6]\python.exe – for scripts you want to be run with Python 2.6
or instead of running python command run pylauncher command (py) specyfing which version of Python you want;
py -2.6 – version 2.6 py -2 – latest installed version 2.x py -3.4 – version 3.4 py -3 – latest installed version 3.x
From Python 3.3 on, there is the official Python launcher for Windows (http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonX to determine the wanted version of the interpreter also on Windows. See more details in my another comment or read the PEP 397.
Summary: The py script.py launches the Python version stated in #! or Python 2 if #! is missing. The py -3 script.py launches the Python 3.
When you install Python, it will not overwrite other installs of other major versions. So installing Python 2.5.x will not overwrite Python 2.6.x, although installing 2.6.6 will overwrite 2.6.5.
So you can just install it. Then you call the Python version you want. For example:
C:\Python2.5\Python.exe
for Python 2.5 on windows and
C:\Python2.6\Python.exe
for Python 2.6 on windows, or
/usr/local/bin/python-2.5
or
/usr/local/bin/python-2.6
on Windows Unix (including Linux and OS X).
When you install on Unix (including Linux and OS X) you will get a generic python command installed, which will be the last one you installed. This is mostly not a problem as most scripts will explicitly call /usr/local/bin/python2.5 or something just to protect against that. But if you don’t want to do that, and you probably don’t you can install it like this:
./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.
Go to the directory of the version of python you want to run
Right click on python.exe
Select ‘Create Shortcut‘
Give that shortcut a name to call by( I use p27, p33 etc.)
Move that shortcut to your home directory(C:\Users\Your name)
Open a command prompt and enter name_of_your_shortcut.lnk(I use p27.lnk)
回答 9
cp c:\ python27 \ bin \ python.exe作为python2.7.exe
cp c:\ python34 \ bin \ python.exe作为python3.4.exe
它们都在系统路径中,请选择要运行的版本
C:\Users\username>python2.7Python2.7.8(default,Jun302014,16:03:49)[MSC v.150032 bit (Intel)] on win
32Type"help","copyright","credits"or"license"for more information.>>>
C:\Users\username>python3.4Python3.4.1(v3.4.1:c0e311e010fc,May182014,10:38:22)[MSC v.160032 bit Intel)] on win32
Type"help","copyright","credits"or"license"for more information.>>>
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.
>>>
回答 10
使用批处理文件进行切换,在Windows 7上轻松高效。我使用以下命令:
在环境变量对话框(C:\ Windows \ System32 \ SystemPropertiesAdvanced.exe)中,
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.
You can create different python development environments graphically from Anaconda Navigator.
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.
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.