How to I get Python 2.x and 3.x to co-exist? [duplicate]
I'm new to programming and thought Python would be开发者_如何转开发 a good language to learn. Most of the tutorials I found were 2.7 based so I started to learn using that version. I've recently found a tkinter tutorial that I'd like to try out but I'm having a problem. If I run a script it will use Python 2.7 which contains Tkinter and not tkinter.
This problem made me think, how can I get my two versions to co-exist so I can program in both 2.x and 3.x?
I'm not sure I understand your question, but I'll take a shot. I'm also assuming you're on Windows.
It's simple -- just install both. They will install to different directories, create different start menu folders, etc. I'd also reccomend PyWin32 for the PythonWin editor installed in both 2.7 and 3.2,
- http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.1.win32-py3.2.exe/download &
- http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.win32-py2.7.exe/download
If you mean how do you write one script that works with either Python 2 or Python 3, look at http://docs.python.org/library/2to3.html
wget <python download url>
tar xfvz Python-X.Y.Z.tar.gz
cd Python-X.Y.Z
configure --prefix=/path/to/python-x.y.z
make install
There is limited support for some of the Python 3 functionality in Python 2. >= 6 (using the __future__
module and py2to3) as well as (even more limited, in my optinon) Python 3 to Python 2. >= 6 with the py3to2, but for a very large percentage of code -- there simply isn't a way to make it work.
In along with the addition of generators (making some function calls from 2.x simply not work in Python 3) many of the major frameworks have not made it to Py3k. Django comes to mind, and, if I'm not mistaken Hg is also in Python 2 still.
You don't specify what operating system you're on. It's been my experience that installing multiple Python versions alongside one another tends to just work. For example on Ubuntu it's simply a matter of installing both the 2.x and 3.x packages (using sudo apt-get install
or Ubuntu Software Centre):
aix@aix:~$ python2.6
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
aix@aix:~$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
I even have a third version (an EPD build) installed on the same box:
aix@aix:~$ python2.7
Enthought Python Distribution -- www.enthought.com
Version: 7.0-2 (64-bit)
Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Nov 29 2010, 13:51:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
It depends on what you are trying to do. You can have them coexist because 3.2 is backwards-compatible. Now you can install both versions on your computer 3.2 and 2.7, but 3.2 unfortunately will have to be used in IDLE....ugh.... Just install both and then depending on which one you want to use. Run that IDLE for either one or the other.
Now if you are meaning that you want more stable version that can be professionally done, go with 2.7 ( I have done otherwise, but if you run into problems 2.7 is more supported.
If you want the more cutting edge stuff, go with 3.2. Either one you go with works with about eveyrthing. If it doesn't, give it a month or two and the rest of the world will catch up.
精彩评论