How do I write code to avoid error when windows service read config file?
Question: I have file tree:

    
    
    f:/src/
       restore.ini
       config.py
       log.py
       service.py
       test.py
    

the `test.py` code like this:

    
    
    import service
    import log
    import config
    
    class Test(object):
        def __init__(self):
            super(Test, self).__init__()
    
        def setUp(self):
            self.currentRound = int(config.read_config_co(r'restore.ini', 'Record')['currentRound'])
    
        def testAction(self):
            log.info(self.currentRound)
    
        def tearDown(self):
            config.write_config_update_co(self.currentRound-1, 'Record', 'currentRound', r'restore.ini')
    
    
    class PerfServiceThread(service.NTServiceThread):
    
        def run (self):
            while self.notifyEvent.isSet():
                try:
                    test = Test()
                    test.setUp()
                    test.testAction()
                    test.tearDown()
                except:
                    import traceback
                    log.info(traceback.format_exc())
    
    
    class PerfService(pywinservice.NTService):
        _svc_name_ = 'myservice'
        _svc_display_name_ = "My Service"
        _svc_description_ = "This is what My Service does"
        _svc_thread_class = PerfServiceThread
    
    
    if __name__ == '__main__':
        pywinservice.handleCommandLine(PerfService)
    

Now, I use cmdline `python test.py install` and `python test.py start` to
action service, but error.

If I move all files in directory `src` to `C:\Python27\Lib\site-
packages\win32\src`, and change code:

    
    
    self.currentRound = int(config.read_config_co(r'src\restore.ini', 'Record')['currentRound'])
    
    config.write_config_update_co(self.currentRound-1, 'Record', 'currentRound', r'src\restore.ini')
    

Now, it's OK!

I want not move directory `src`, how do I do? Thanks!


Answer: if you use relative paths for file or directory names python will look for
them (or create them) in your current working directory (the $PWD variable in
bash; something similar on windows?).

if you want to have them relative to the current python file, you can use
(python 3.4)

    
    
    from pathlib import Path
    HERE = Path(__file__).parent.resolve()
    RESTORE_INI = HERE / 'restore.ini'
    

or (python 2.7)

    
    
    import os.path
    HERE = os.path.abspath(os.path.dirname(__file__))
    RESTORE_INI = os.path.join(HERE, 'restore.ini')
    

if your `restore.ini` file lives in the same directory as your python script.

then you can use that in

    
    
    def setUp(self):
        self.currentRound = int(config.read_config_co(RESTORE_INI, 
                               'Record')['currentRound'])
    

