a couple of scary moments in wxpython under win32 under py2exe with a ridiculous deadline thrown in...

Ahh. I know. Long day.

···

On 8/14/07, Brian Martin thebrianmartin@gmail.com wrote:

Thanks for the typical mailing list punch-in-the-face.

On 8/14/07, Chris Mellon < > arkanes@gmail.com> wrote:

On 8/14/07, Brian Martin < > > thebrianmartin@gmail.com > > > wrote:

Just some weird things that happened during my project that I wanted to
share…

  1. For simplicity sake (and because my installer knowledge was minimal),
    I was just putting icons and logos for my application in the root directory

of the source folder. When I would refer
to them when creating an image for example i could just exclude any path
information (ie. ‘foo.img’ vs ‘c:\Program Files\proj\foo.img’) and

wxpython (or python) would know how to get them. Then, when adding a feature
to allow someone to add a custom graphic to the application I make a call to
shutil.copy to move the image from a user location into the source tree. As

soon as I do this, wxpython could not find any of my images or icons. I had
to explicitly point to them or it would barf. Now this is probably the best
way to do it and I just made a global constant which used sys.path[0].
Regardless, it was interesting. Mid-application, references to these images
were broken when i called the shutil.copy command.

Your problem here is lack of knowledge. All common OSes have a concept

known as the “current working directory”. Relative paths are so called
because they are relative to this directory.

You probably used wx.FileDialog to allow the user to select the file
to move. wx.FileDialog changes the current working directory, because
windows is bizarre. Clear the wx.FD_CHANGE_DIR style flag to prevent
this behavior.

There’s actually lots of different ways this can fail and you’ve run

into only one of them. Because of this, relying on the current working
directory to find your data files is heavily discouraged.

  1. This is related to #1 and was extremely stressful at the time. I packaged

this project up for my boss to take to a presentation using py2exe. The
killer here is that while running under py2exe the sys.path contains a
single entry which is the “library.zip” file. So my application was no

searching for the icons and images here: “C:\Program
Files\proj\library.zip\hello
.gif” where sys.path[0] contained: C:\Program
Files\proj\library.zip

sys.path isn’t intended to be used to find your data files. It’s a

mechanism used by Python to find imports. wxPython, in the interests
of platform compatability, provides the wx.StandardPaths class which
can help you find well-defined file locations.

  1. Keyboard events between win32 and Linux are a not compatible. You cannot

depend on it working remotely the same if you, for example, spend part of an
evening in Ubuntu working on a project and then try it on XP the next
morning at work.

You should always test on platforms that you are going to deploy on.

There are some platform differences but by far a larger category of
problems is incorrect code that happens to work on one platform
failing on another. Giving details about your problem will help more
than broad statements of incompatibility.


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

Hey, just revisiting a few issues with my App this week to do things the “right” way.

How do I safely reference icons and images used in my application… I can’t seem to find a way to get the directory where my python script is actually executed from… I’m in the win32 environment. With wx.StandardPaths I can get all sorts of windows centric directories but i really just want the installation directory.

thanks

···

On 8/15/07, Steve Holden steve@holdenweb.com wrote:

[off-list]

Don’t worry. Happens to us all, if we live long enough :wink:

regards
Steve

Brian Martin wrote:

Ahh. I know. Long day.

On 8/14/07, Brian Martin < > thebrianmartin@gmail.com > > mailto:thebrianmartin@gmail.com> wrote:

Thanks for the typical mailing list punch-in-the-face.
On 8/14/07, *Chris Mellon* < arkanes@gmail.com > >     <mailto:arkanes@gmail.com>> wrote:

    On 8/14/07, Brian Martin < thebrianmartin@gmail.com > >         <mailto:thebrianmartin@gmail.com>> wrote:
    >  Just some weird things that happened during my project that I
    wanted to
    >  share....
    >
    >  1) For simplicity sake (and because my installer knowledge was
    minimal),
    >  I was just putting icons and logos for my application in the
    root directory
    >  of the source folder. When I would refer
    >  to them when creating an image for example i could just
    exclude any path
    >  information (ie. '

foo.img’ vs 'c:\Program

    Files\proj\foo.img')   and
    >  wxpython (or python) would know how to get them. Then, when
    adding a feature
    >  to allow someone to add a custom graphic to the application I
    make a call to
    >  shutil.copy to move the image from a user location into the
    source tree. As
    >  soon as I do this, wxpython could not find any of my images or
    icons. I had
    >  to explicitly point to them or it would barf. Now this is
    probably the best
    >  way to do it and I just made a global constant which used
    sys.path[0].
    >  Regardless, it was interesting. Mid-application, references to
    these images
    >  were broken when i called the shutil.copy command.
    >
    Your problem here is lack of knowledge. All common OSes have a
    concept
    known as the "current working directory". Relative paths are so
    called
    because they are relative to this directory.

    You probably used wx.FileDialog to allow the user to select the file
    to move. wx.FileDialog changes the current working directory,
    because
    windows is bizarre. Clear the wx.FD_CHANGE_DIR style flag to prevent
    this behavior.

    There's actually lots of different ways this can fail and you've
    run
    into only one of them. Because of this, relying on the current
    working
    directory to find your data files is heavily discouraged.

    >  2) This is related to #1 and was extremely stressful at the
    time. I packaged
    >  this project up for my boss to take to a presentation using
    py2exe. The
    >  killer here is that *while running under py2exe* the sys.path
    contains a
    >  single entry which is the "library.zip" file. So my
    application was no
    >  searching for the icons and images here: "C:\Program
    >  Files\proj\library.zip\hello
    >  .gif"  where sys.path[0] contained: C:\Program
    >  Files\proj\library.zip
    >

    sys.path

isn’t intended to be used to find your data files. It’s a

    mechanism used by Python to find imports. wxPython, in the interests
    of platform compatability, provides the wx.StandardPaths

class which

    can help you find well-defined file locations.

    >  3) Keyboard events between win32 and Linux are a not
    compatible. You cannot
    >  depend on it working remotely the same if you, for example,
    spend part of an
    >  evening in Ubuntu working on a project and then try it on XP
    the next
    >  morning at work.

    You should always test on platforms that you are going to deploy
    on.
    There are some platform differences but by far a larger category of
    problems is incorrect code that happens to work on one platform
    failing on another. Giving details about your problem will help more
    than broad statements of incompatibility.





    ---------------------------------------------------------------------

    To unsubscribe, e-mail:
    wxPython-users-unsubscribe@lists.wxwidgets.org
    <mailto:

wxPython-users-unsubscribe@lists.wxwidgets.org>

    For additional commands, e-mail:
    wxPython-users-help@lists.wxwidgets.org
    <mailto:wxPython-users-help@lists.wxwidgets.org>


Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------

Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Because

a) you need to init wx.App to set the app name before it
   really starts getting useful

b) you need wx imported before it really needs to if you
   want to access config files and such early in the app
   startup process (Which really means: why doesn't
   something like this exist in Python proper ?)

For GNUmed I have written a class which works around a few
of those problems (look for the class gmPaths):

  http://cvs.savannah.gnu.org/viewvc/gnumed/gnumed/client/pycommon/gmTools.py?root=gnumed&view=markup

HTH,
Karsten

···

On Mon, Aug 20, 2007 at 12:46:00PM -0700, Christopher Barker wrote:

That being said, why not:

wx.StandardPaths.GetDataDir

--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

How about: os.path.abspath(os.path.dirname(sys.argv[0]))

works for me under Windows, OS X, and Ubuntu (with py2exe or py2app or just running the script, even from command line).

···

On 8/20/07, Chris Mellon arkanes@gmail.com wrote:

Using the directory of your executable is against the best practices
and HIG of every platform. You really should use use the values in

wx.StandardPaths (usually GetDataDir and GetConfigDir).

If you really, truly want avoid best practices then you’ll have to
make some decisions. wx.StandardPaths.GetExecutablePath will usually
be right if you’re running under py2exe, but it won’t be right when

running as a script

os.getcwd() will usually be right if the app was started via a menu
shortcut, but won’t be correct if the app was started via a hand-made
shortcut or from the command line or in any number of other ways. You

shouldn’t rely on this.

os.path.dirname(os.path.abspath(file)) will return the directory
where your .pyc file is located (probably not what you want under
py2exe).


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org