pyglet.resource
Load application resources from a known path.
Loading resources by specifying relative paths to filenames is often problematic in Python, as the working directory is not necessarily the same directory as the application’s script files.
This module allows applications to specify a search path for resources.
Relative paths are taken to be relative to the application’s __main__
module. ZIP files can appear on the path; they will be searched inside. The
resource module also behaves as expected when applications are bundled using
Freezers such as PyInstaller, Nuitka, py2app, etc..
In addition to providing file references (with the file() function),
the resource module also contains convenience functions for loading images,
textures, fonts, media and documents.
3rd party modules or packages not bound to a specific application should
construct their own Loader instance and override the path to use the
resources in the module’s directory.
Path format
The resource path path (see also Loader.__init__() and
Loader.path())
is a list of locations to search for resources. Locations are searched in the
order given in the path. If a location is not valid (for example, if the
directory does not exist), it is skipped.
Locations in the path beginning with an “at” symbol (‘’@’’) specify Python packages. Other locations specify a ZIP archive or directory on the filesystem. Locations that are not absolute are assumed to be relative to the script home. Some examples:
# Search just the `res` directory, assumed to be located alongside the
# main script file.
path = ['res']
# Search the directory containing the module `levels.level1`, followed
# by the `res/images` directory.
path = ['@levels.level1', 'res/images']
Paths are always case-sensitive and forward slashes are always used as path separators, even in cases when the filesystem or platform does not do this. This avoids a common programmer error when porting applications between platforms.
The default path is ['.']. If you modify the path, you must call
reindex().
- exception ResourceNotFoundException
The named resource was not found on the search path.
- __init__(name)
- exception UndetectableShaderType
The type of the Shader source could not be identified.
- __init__(name)
- class FileLocation
Location on the filesystem.
- class Loader
Load program resource files from disk.
The loader contains a search path which can include filesystem directories, ZIP archives, URLs, and Python packages.
- reindex()
Refresh the file index.
You must call this method if
resource.pathis changed, or the filesystem layout changes.
- class Location
Abstract resource location.
Given a location, a file can be loaded from that location with the
open()method. This provides a convenient way to specify a path to load files from, even when that path does not reside on the filesystem.
- class URLLocation
Location on the network.
This class uses the
urllibmodule to open files on the network, given a base URL.
- class ZIPLocation
Location within a ZIP file.
- reindex()
Refresh the file index.
You must call this method if
resource.pathis changed, or the filesystem layout changes.
- path = ['.']
Default resource search path.
Locations in the search path are searched in order and are always case-sensitive. After changing the path you must call reindex.
See the module documentation for details on the path format.
- Type:
list of str
Functions
- reindex()
Refresh the file index.
You must call this method if
resource.pathis changed, or the filesystem layout changes.