Python3, GUIs, game APIs, PyGLet, 2to3...?

J

John Ladasky

I am teaching Python 3 to a few beginning computer programming students. Being high-school age boys, they are, unsurprisingly, interested in games. I want to introduce them to real-time programming and GUI in the most painless way possible.

I know that Python comes bundled with Tkinter. Aside from the fact that I dislike its look and feel, Tkinter is not a beginners' GUI tool. You have to write a fair amount of boiler-plate code, and you have to understand classes pretty well. My students are not at the OOP level yet. Yet Python's turtle package, which is built on Tkinter, manages to avoid a lot of that complexity, at least at first. I am looking for a tool which gives my students the simplicity of turtle, but which works with 2D raster graphics. And, it must work on Py3.

Deep in the middle of another thread, I had PyGLet recommended to me. So Itried to get PyGLet working on Python 3, which it supposedly will do. It installs on Py3, but importing pyglet fails. The test program also fails. The tracebacks were showing me that the package code is full of Python 2.xprint statements. I started fixing them manually and, after a while, gaveup.

https://groups.google.com/d/msg/comp.lang.python/J64gfFg3ZKw/hH-lXurR70EJ

There may be other Py3-incompatible code in the PyGLet package that I haven't encountered yet. Thus I have looked at the Python docs for the "2to3" utility. 2to3 probably does what I want, except for one thing: recursive operations on subfolders. Do I really have to manually traverse the directory tree of the package, modify one folder's worth of .py files, and then repeat ad nauseam? Yes, I could write a script to do that -- but shouldn't that functionality be built into 2to3?

Any advice that you folks might offer -- either about getting 2to3 to execute recursively, or about installing any GUI with a shallow learning curve installed on Py3, would be appreciated. Thanks.
 
J

John Ladasky

Followup to my own post: I've made progress with PyGLet. I should mention that I'm using Ubuntu Linux 13.04 64-bit, in case it matters.

I tried executing "2to3 -w *.py" on just the files in the directory pyglet-1.2alpha1/pyglet. I then changed back to the pyglet-1.2alpha1 directory, and executed "sudo python setup.py install". Finally, I started my Python3 interpreter. This time, "import pyglet" generated no errors.

The PyGLet "Hello, World" code found on this web page runs:

http://www.pyglet.org/doc/programming_guide/hello_world.html

While unexplored parts of the PyGLet package may yet contain Py2-specific code, I'm definitely on my way.
 
K

Kushal Kumaran

John Ladasky said:
Followup to my own post: I've made progress with PyGLet. I should mention that I'm using Ubuntu Linux 13.04 64-bit, in case it matters.

I tried executing "2to3 -w *.py" on just the files in the directory pyglet-1.2alpha1/pyglet. I then changed back to the pyglet-1.2alpha1 directory, and executed "sudo python setup.py install". Finally, I started my Python3 interpreter. This time, "import pyglet" generated no errors.

Does your python command mean python2 or python3? The setup.py at
https://code.google.com/p/pyglet/source/browse/setup.py seems to run
2to3 automatically, but that will only happen if you actually use
python3 to run setup.py.
 
J

John Ladasky

Does your python command mean python2 or python3? The setup.py at
https://code.google.com/p/pyglet/source/browse/setup.py seems to run
2to3 automatically, but that will only happen if you actually use
python3 to run setup.py.

Thank you for that sharp observation, Kushal! (And I hope that more packages besides pyglet are configured this way -- to automatically run 2to3, when you install on Python 3.)

How strange that I remember typing "sudo python setup.py install". Becauseon my Ubuntu 13.04 system, that would invoke Python 2.7, which is an essential part of the OS. Nevertheless, I was able to import pyglet after invoking "python3" from the shell. I didn't think that installed modules could be shared between multiple Python versions.

I'll try again from scratch, and see whether that clears up my problems.
 
J

John Ladasky

I'll try again from scratch, and see whether that clears up my problems.

Nope, that didn't work.

=======================================

john@john:~/Desktop/pyglet-1.2alpha1$ sudo python3 setup.py install

[sudo] password for john:

running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info
Writing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info

john@john:~/Desktop/pyglet-1.2alpha1$ 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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./pyglet/__init__.py", line 276
print '[%d] %s%s %s' % (thread, indent, name, location)
^
SyntaxError: invalid syntax

=======================================

The source code link that Kushal posted is dated December 19, 2012. Since that was several months ago, I double-checked the source code of setup.py in the 1.2alpha1 package that I downloaded. It would appear to perform the same check of sys.version_info that was shown on the Google Code page.

To see how that check actually runs, I saved a copy of setup.py as setup2.py, adding diagnostic calls to print() as shown in the code block below:

=======================================

if sys.version_info >= (3,):
# Automatically run 2to3 when using Python 3
print("Python version is 3.0 or later.") # I added this
if _have_setuptools:
print("Have setuptools.") # I added this
setup_info["use_2to3"] = True
else:
print("Do not have setuptools.") # I added this
from distutils.command.build_py import build_py_2to3
setup_info["cmdclass"] = {"build_py" : build_py_2to3}

=======================================

Here's the output:

=======================================

john@john:~/Desktop/pyglet-1.2alpha1$ sudo python3 setup2.py install

Python version is 3.0 or later.
Do not have setuptools.
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info
Writing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info

=======================================

So, I don't know much yet about this _have_setuptools flag. I don't know whether it has to be True, instead of False, in order for 2to3 to work properly. I get the impression from the code that 2to3 should run regardless ofthe _have_setuptools flag, it is just that the task is accomplished in twodifferent ways?
 
K

Kushal Kumaran

John Ladasky said:
I'll try again from scratch, and see whether that clears up my problems.

Nope, that didn't work.

=======================================

john@john:~/Desktop/pyglet-1.2alpha1$ sudo python3 setup.py install

[sudo] password for john:

running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info
Writing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info

john@john:~/Desktop/pyglet-1.2alpha1$ 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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./pyglet/__init__.py", line 276

----------^

Your import is attempting to import from your current directory, which
has the pre-2to3 version of the files packaged in the original
distribution. Switch away and try again.
print '[%d] %s%s %s' % (thread, indent, name, location)
^
SyntaxError: invalid syntax

=======================================

The source code link that Kushal posted is dated December 19, 2012. Since that was several months ago, I double-checked the source code of setup.py in the 1.2alpha1 package that I downloaded. It would appear to perform the same check of sys.version_info that was shown on the Google Code page.

To see how that check actually runs, I saved a copy of setup.py as setup2.py, adding diagnostic calls to print() as shown in the code block below:

=======================================

if sys.version_info >= (3,):
# Automatically run 2to3 when using Python 3
print("Python version is 3.0 or later.") # I added this
if _have_setuptools:
print("Have setuptools.") # I added this
setup_info["use_2to3"] = True
else:
print("Do not have setuptools.") # I added this
from distutils.command.build_py import build_py_2to3
setup_info["cmdclass"] = {"build_py" : build_py_2to3}

=======================================

Here's the output:

=======================================

john@john:~/Desktop/pyglet-1.2alpha1$ sudo python3 setup2.py install

Python version is 3.0 or later.
Do not have setuptools.
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info
Writing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info

=======================================

So, I don't know much yet about this _have_setuptools flag. I don't know whether it has to be True, instead of False, in order for 2to3 to work properly. I get the impression from the code that 2to3 should run regardless of the _have_setuptools flag, it is just that the task is accomplished in two different ways?

That seems correct. My familiarity with the python packaging tools is
limited, though.
 
J

Jerry Hill

=======================================

john@john:~/Desktop/pyglet-1.2alpha1$ sudo python3 setup.py install

[sudo] password for john:

running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info
Writing /usr/local/lib/python3.3/dist-packages/pyglet-1.2alpha1.egg-info

Pyglet was installed to /usr/local/lib/python3.3/dist-packages ...
john@john:~/Desktop/pyglet-1.2alpha1$ 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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./pyglet/__init__.py", line 276
print '[%d] %s%s %s' % (thread, indent, name, location)
^
SyntaxError: invalid syntax

.... But here, the error message is talking about ./pyglet/__init__.py.
I think you're accidentally importing the pyglet package from the
local directory instead of from the proper location in dist-packages.
Try changing back to your home directory and trying this again. I
think you're picking up the code from
~/Desktop/pyglet-1.2alpha1/pyglet instead of from
/usr/local/lib/python3.3/dist-packages.
 
J

John Ladasky

Nope, that didn't work.

Thanks to both Jerry and Kushal. You were right, I was doing a local import of Py2.x code, instead of importing the 2to3-converted code from site-packages.
 
J

John Ladasky

I'm making progress, but I'm not out of the woods yet.

I'm trying to run some of the programs from the tutorial web pages, and from the pyglet1.2alpha1/examples directory. I've realized that I will probably need to run 2to3 on the many of the latter.

The Hello, World example runs.

http://www.pyglet.org/doc/programming_guide/hello_world.html

The Image Viewer example...

http://www.pyglet.org/doc/programming_guide/image_viewer.html

....runs if I provide a local image file, and load it using pyglet.image.load(). As written, the example fails on the line:

image = pyglet.resource.image('kitten.jpg')

It's possible that the alpha1.2 version of PyGLet is missing some resources..

It looks like the code for the bouncing ball example ought to work, but it doesn't. That code can be found on the web at:

http://www.pyglet.org/doc/programming_guide/noisy.py

and also in the PyGLet package, in the folder pyglet1.2alpha1/examples/noisy.

Here's my traceback which, I am sorry to report, I find less than fully informative:

===============================

Traceback (most recent call last):
File "/usr/local/lib/python3.3/dist-packages/pyglet/__init__.py", line 332, in __getattr__
return getattr(self._module, name)
AttributeError: 'NoneType' object has no attribute 'load'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.3/dist-packages/pyglet/lib.py", line 111, inload_library
lib = ctypes.cdll.LoadLibrary(name)
File "/usr/lib/python3.3/ctypes/__init__.py", line 431, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python3.3/ctypes/__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libavbin.so: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "noisy.py", line 56, in <module>
sound = pyglet.resource.media(BALL_SOUND, streaming=False)
File "/usr/local/lib/python3.3/dist-packages/pyglet/resource.py", line 610, in media
return media.load(path, streaming=streaming)
File "/usr/local/lib/python3.3/dist-packages/pyglet/__init__.py", line 338, in __getattr__
__import__(import_name)
File "/usr/local/lib/python3.3/dist-packages/pyglet/media/__init__.py", line 1469, in <module>
from . import avbin
File "/usr/local/lib/python3.3/dist-packages/pyglet/media/avbin.py", line64, in <module>
darwin='/usr/local/lib/libavbin.dylib')
File "/usr/local/lib/python3.3/dist-packages/pyglet/lib.py", line 118, inload_library
if ((self.linux_not_found_error not in o.message) and
AttributeError: 'OSError' object has no attribute 'message'

===============================

I think that only the top few lines of this traceback are relevant. Somehow a None is being passed into some function in pyglet/__init__.py, when that function expects an object with an attribute named "load". Looking at the source, the function being called is _ModuleProxy.__getattr__().

I often find that Python's tracebacks stop one level short of what I reallywant to know. The top level calling function is frequently not named. What part of noisy.py called the code that crashed? Is there any way to get traceback to tell you more?

Anyway, I wondered whether this might be a Python compatibility wart in noisy.py. Thus I tried running 2to3 on noisy.py, even though my eyes told me that the code was OK. 2to3 agreed with me, reporting: "RefactoringTool: Nochanges to noisy.py".

I'm not sure where to go next with this. One thing is for certain, all of this is way over the heads of my students...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,985
Messages
2,570,199
Members
46,766
Latest member
rignpype

Latest Threads

Top