executable path finding

K

koranthala

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?
 
D

Diez B. Roggisch

koranthala said:
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
 
K

koranthala

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

Thank you Diez. It was what I was looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top