Python 2 or 3?

A forum for general discussion of the Python programming language.

Python 2 or 3?

Postby dboxall123 » Tue Aug 27, 2013 9:04 pm

Hello all. I have just installed Linux. This comes with Python 2.7 pre-installed, but I have learnt Python 3.3. There is a version of Pygame for 3.3 for windows, but not for linux. I found a way to install python 3.2 online, but it's using a few lines in the terminal, and I think it just automatically installs the latest version of python, which would mean that I wouldn't be able to use pygame (I think). So I was wondering, would it be easier for me to just learn python 2.7? I mean, judging by various codes and tutorials I've looked at online, it doesn't really look any different, apart from the obvious things like print and divisions. Or am I wrong about that? I'm just not really sure if it would be worth going through all the hassle trying to install python 3 if 2 isn't gonna be too tricky to work out y'know? What would you do?

edit - I basically only make games with python. I dont use it for anything else at all
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Python 2 or 3?

Postby micseydel » Tue Aug 27, 2013 9:23 pm

http://wiki.python.org/moin/Python2orPython3

What kind of games? 2D? 3D? Networked or not?

My best advice for learning game programming is to start with Python 2 or 3 (it doesn't matter), once you're very comfortable with one or the other (or both), learn OOP, then after that start moving more toward your goal.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python 2 or 3?

Postby dboxall123 » Tue Aug 27, 2013 9:45 pm

No, I already know Python 3 and I make 2d games using pygame. However, I was just wondering if it would be difficult for me to learn python 2 instead, to save me the trouble of trying to install python 3 and the relevant pygame on my new linux os
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Python 2 or 3?

Postby micseydel » Tue Aug 27, 2013 10:09 pm

Linux usually has nice package managers so that shouldn't be an issue. But using 2 after learning 3 should be next to trivial.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python 2 or 3?

Postby metulburr » Tue Aug 27, 2013 10:51 pm

this is a tutorial for setting up pygame under python3.x, specifically in ubuntu
viewtopic.php?f=25&t=2716
but it is essentially , use pygame 1.9.2 pre with python3.x instead of pygame 1.9.1

You can also have both python versions installed, with python2.x using pygame 1.9.1 (from the repos), and python3.x using pygame 1.9.2 pre. That is pretty much my setup. Then i can run others pygame games regardless of version.

the ubuntu repos has already quite a bit of python3.x 3rd party libraries in their repos out now:
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python 2 or 3?

Postby dboxall123 » Wed Aug 28, 2013 12:37 am

Ripen I tyght, well I now haave python 3 installed, and I have downloaded and extracted the pygame link. But when I type:
Code: Select all
sudo apt-get install python3-dev libsdl-image1

it says:
Code: Select all
E: unable to locate 'libsdl-image1'

Have you any ideas?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Python 2 or 3?

Postby dboxall123 » Wed Aug 28, 2013 12:59 am

I've just discovered something really cool. I installed IDLE, and I wanted to see if it would use python 2 or python 3. So i started up a new window and wrote
Code: Select all
print 'python'

and it ran. So then I deleted it and wrote
Code: Select all
print('python')

and it also ran. Then I tried putting both syntaxes into one script, and it works! I'ts using python 2 and pyhton 3 at the same time! I wonder if this is applicable with pygame too, ie if I could just download one version of pygame and run python 2 and pyhton 3 codes on it? Does anyone konw a line of code that would be different in python 2 frpm pyhton 3? I mean a pygame specific code. That would cool if it worked :D
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Python 2 or 3?

Postby metulburr » Wed Aug 28, 2013 1:09 am

E: unable to locate 'libsdl-image1'

you are missing the second half the package name
Code: Select all
libsdl-image1.2-dev


Then I tried putting both syntaxes into one script, and it works! I'ts using python 2 and pyhton 3 at the same time!

hate to burst your bubble, but...
thats actually an indication that it is using python2.x. Python 2.7.3 and higher (i might be off on specific version number) already is using some of python3.x features (at least syntax style) for porting purposes.
It would break if you input something like:
Code: Select all
from urllib.request import Request

which is python3.x's equivalent of python2.x's urllib2.Request library. the print difference is not the only difference.


If its running python2.x
Code: Select all
print 'test'

will only work in python 2, but
Code: Select all
print('test')

will work in both python2.x (whatever the version number is and higher) and 3.x.

Code: Select all
metulburr@ubuntu:~$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print('test')
test
>>> print 'test'
test
>>> exit()
metulburr@ubuntu:~$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('test')
test
>>> print 'test'
  File "<stdin>", line 1
    print 'test'
               ^
SyntaxError: invalid syntax
>>> exit()


It doesnt mean its can use both versions.


However in those older python2.x versions, you can still use python3.x's print function by importing
Code: Select all
from __future__ import print_function
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python 2 or 3?

Postby Mekire » Wed Aug 28, 2013 1:45 am

In general it is very easy to write Pygame programs in a way that will be compatible with both versions. There is nothing specific to pygame itself that I can think of that is different. You just need to be aware of all the standard caveats when switching between the two. Print won't matter so much as your final release probably won't have print lines. The difference in performance between xrange and range can be a big factor sometimes, and if it is you need to ensure that your program will be using xrange when running under python 2 (but not under python 3 as it doesn't exist (or was renamed rather)).

A lot of people (myself included) always include parenthesis around prints just so it is one less thing to worry about. Note however if you try to use some of the more advanced features of the python 3 print function then yes, you will need the future import.

If you start creating bigger programs and structure them as packages you will need to be sure to use explicit relative imports within packages. If you do it the explicit way it will run on both versions (implicit won't work in python 3).

-Mek
User avatar
Mekire
 
Posts: 816
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Python 2 or 3?

Postby dboxall123 » Wed Aug 28, 2013 1:52 am

Well that was stoooopid of me lol. Anyway, it's all done now, thanks for that. But how do you tell it to run the game in python 2 or 3? Or does it read the script and decide automatically?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Python 2 or 3?

Postby metulburr » Wed Aug 28, 2013 2:09 am

But how do you tell it to run the game in python 2 or 3? Or does it read the script and decide automatically?

it is determined by which version is invoked to start the script. IDE's can be switched to either or version by whicvh version they invoke, or you can use the terminal/command line to invoke a specific version
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python 2 or 3?

Postby dboxall123 » Wed Aug 28, 2013 9:43 am

Wicked, cheers mate
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm


Return to General Discussions

Who is online

Users browsing this forum: dboxall123 and 2 guests