Search code examples
pythonwindows-servicespy2execonfigparser

Modify path of execution of a python script running as a windows service


I have a Python application that uses ConfigParser.ConfigParser() to access a configuration file. I have created a windows service of the Python application using py2exe. The problem that I have is that the service can only find the configuration file if I place it in windows/system32 folder. I would like to have the configuration file in the same folder where the service was installed. For example, after using py2exe I have the following folder:

c:/temp/dist/winservice.exe
c:/temp/dist/configfile.cfg

Then I do:

winservice.exe install

But the service does not look for the configfile.cfg in path: c:/temp/dist/ but in path: c:/windows/system32/

Is there any way to change that?

Thanks!


Solution

  • You can use Inspect module to get the name of the file in which this code object was created. So to get the path of the file you will use : inspect.currentframe().f_code.co_filename

    So, to get a directory name where your winservice.exe is:

    dirPath = os.path.dirname(inspect.currentframe().f_code.co_filename)
    

    hope it helps