executable path finding

Diez B. Roggisch deets at nospam.web.de
Mon Aug 31 18:07:20 CEST 2009


koranthala wrote:

> Hi,
>     I am creating a python application using py2exe. I am facing a
> problem which I am not sure how to solve.
>     The application contains many other files associated with it -
> like icons, config files etc. The executable can be in any directory.
> If the user creates a shortcut to the executable to run in desktop,
> the program fails - saying that the icon image cannot be found. But I
> can run the application properly, if I run it from application
> directory - where all the other icons, config files etc are kept.
>     How is such issues usually solved? I do not want to hardcode the
> paths of icons and store the icons in those paths. I guess, we can
> change the directory to the application directory before running the
> application. (in __init__.py ???) But how do we find the current
> directory in that case? I am completely at sea in solving this.
>     This looks to be a very common issue. How is this usually solved?

You can get the location of a module via

 module.__file__

This can be used to find a file relative to e.g. the toplevel module/package
of your application.

The pkg_resources-module of setuptools encapsulates that even into a stream
and file-name api.

Diez



More information about the Python-list mailing list