include files with a package

Mike C. Fletcher mcfletch at home.com
Sun May 27 20:53:11 EDT 2001


For python files loaded directly from disk, see the __file__ variable in
your module.  Unfortunately, this variable has a habit of being available or
not depending on the phase of the moon (mostly when you pack Python packages
into an exe with Py2EXE or McMillan Installer).  When using those, you need
hacks to figure out the .exe's directory (which is, with all known versions
of those systems, sys.path[0] if you don't muck around with the path).

Examples:
8<____________ icon.py, icon in local directory
import os
from wxPython.wx import *

### Get the directory for the plug-in
localDirectory = os.path.abspath(
	os.path.dirname(__file__)
)
### Get the icon for this object
DIAGRAMICON = wxIcon(
	os.path.join( localDirectory, 'tour2.ico'),
	wxBITMAP_TYPE_ICO,
)


8<____________ worldbuilder __init___ (excerpt)
import os, sys
try:
	rootdirectory = os.path.split( __file__ )[0]
	standalonestatus = "Standard Python Version"
except NameError:# likely running under exe-d version
	if sys.path:
		rootdirectory = sys.path[0]
		standalonestatus = "Stand-Alone Executable"
	else:
		print "SkeletonBuilder is unable to find it's root directory, it will not
run without this information. Please contact the author."
		raw_input( "Press <Enter> to return to Windows" )


HTH,
Mike

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of John Hunter
Sent: May 27, 2001 16:56
To: python-list at python.org
Subject: include files with a package
...
I have some files that I want to include with a package of modules I
am working on.  These are xlst stylesheets.  From within the module,
say in the __init__ statement, I would like to do
...





More information about the Python-list mailing list