pyglet.resource and setup.py's entry_points

78 views
Skip to first unread message

Jonathan Hartley

unread,
Jul 5, 2012, 4:02:42 AM7/5/12
to pyglet...@googlegroups.com
Hey folks.

I'm using pyglet.resource for the first time. Forgive my idiocy. Project layout:

./
  |-tanks\
  |   |-source code goes here
  |-data\
  |   |-image files go here
  |-setup.py
  |-run_game.py


in my source, I have:

    pyglet.resource.path = ['data']
    pyglet.resource.reindex()
    image = pyglet.resource.texture(name + '.png')


and this works fine.

My problem is that the 'run_game.py' I have shown above doesn't actually exist. I have no executable script. Instead I use setup.py's "entry_points" to specify that the entry point to my application is my "main" function, which resides within the source code directory.

Calling "python setup.py install" or "python setup.py develop" generates an executable script
on the PATH which calls my main function. (this is standard setuptools functionality, not my own crazy invention)

When I do this, pyglet.resource is unable to find my image files, because it now thinks my top-level executable script is in my virtualenv's 'bin' directory.

I could work around this by specifying an absolute pyglet.resource.path, derived from __file__.  But this will break when I package my application up and distribute the data files using setup.py's "data_files". The install location of my data doesn't have a well-defined location relative to __file__ - I have to use "pkg_resources" to find it. Based on a quick grep, I don't *think* pyglet.resource is doing that. ( But the docs say "The resource module also behaves as expected when applications are bundled using py2exe or py2app.", so I'm probably wrong.)

I guess the alternative is I could move my data files into my source code (yuck), so that I can distribute them using setup.py's 'package_data', and use pyglet.resource.path = ["@data"]?

Or I could stop using setup.py's "entry_points" and create an actual top-level "run_game.py" script. But I thought "entry_points" was "the right way to do it" for distributing python application packages?

I'm sure I can muddle through, but thought I'd air my confusion on the offchance someone could point out how I've managed to misunderstand more-or-less everything.

Feel free to lampoon me for comedy effect if it makes responding more fun.

Cheers!

    Jonathan
-- 
Jonathan Hartley    tar...@tartley.com    http://tartley.com
Made of meat.       +44 7737 062 225       twitter/skype: tartley

Andreas Schiefer

unread,
Jul 5, 2012, 4:43:55 AM7/5/12
to pyglet...@googlegroups.com
Hi,

you're probably right, I don't think pyglet.resource supports this.

> I don't *think* pyglet.resource is doing that. ( But the docs say "The
> resource module also behaves as expected when applications are bundled using
> py2exe or py2app.", so I'm probably wrong.)

I think this only means that, in the case of a frozen application, it
also searches in the directory of the executable.

I guess you have to somehow find out the install location of your data
manually and add that directory to pyglet.resource.path.


Andy

Richard Jones

unread,
Jul 5, 2012, 5:01:02 AM7/5/12
to pyglet...@googlegroups.com
It may be that pyglet.resource needs to be made virtualenv- and
setuptools-aware since it was written before those came into broad
use.


Richard
> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To post to this group, send email to pyglet...@googlegroups.com.
> To unsubscribe from this group, send email to
> pyglet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pyglet-users?hl=en.

Jonathan Hartley

unread,
Jul 5, 2012, 7:18:22 AM7/5/12
to pyglet...@googlegroups.com
On Thursday, July 5, 2012 10:01:02 AM UTC+1, Richard Jones wrote:
It may be that pyglet.resource needs to be made virtualenv- and
setuptools-aware since it was written before those came into broad
use.



Thanks for the info and background, Richard and Andy. Reassuring to see that I'm not simply swimming against the current.

I'll therefore think I ought to sidestep pyglet.resource, overcome my revulsion and use package_data rather than data_files, and use pkg_resources.resource_stream to load images. Hence:

First I moved 'data' dir into 'tanks' dir. (which I regarded as 'yuck', but I'm getting used to it. I feel as though setuptools makes this sort of data easier to deal with than 'data_files')

Then in my source, I load images using:

    from pkg_resources import resource_stream
    image_stream = resource_stream('tanks', 'data/%s.png' % (name,))
    image = pyglet.image.load('%s.png' % (name,), image_stream)

(where 'tanks' is the name of my project's top-level package)

This works for running in development. For this to work on an installed package, I also added a MANIFEST.in:
(so that the images get included in my sdist zip file)

    include tanks/data/*.*

And lines to my setup.py config dictionary:
(so that the images are installed from the sdist to the target python's site-packages or virtualenv)

    include_package_data=True,

And I think that's it. Works for me, even though the data files are installed inside an '.egg' zipfile. Thanks for chatting me through it.

Reply all
Reply to author
Forward
0 new messages