CWD is still \Windows\System32 for service application
According to the changelog at http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/raw-file/tip/CHANGES.txt build 204 added this fix:
Python services will no longer have %System32% as the CWD when they start (the directory of the hosting exectutable will be used instead). This should prevent ImportError due to DLLs in that System32 directory being found for Python imports.
But I am using build 219 on Python 3.4 / x64 and my service application reports that at servce start the current working direcory is in fact C:\Windows\System32
The following information was included with the event:
CWD=C:\WINDOWS\system32
This log message was written by my app at the start of function def SvcDoRun(self):
Am I misunderstanding or doing something wrong? Or did the fix mentioned in build 204 subsequently go away?
Reported by: davidrueter
Original Ticket: pywin32/bugs/720
The code remains in place in PythonService_main in PythonService.cpp - however, it's certainly possible that it is not working as expected for you. I'll try and find some time to take a look.
Original comment by: mhammond
Thanks, Mark. I have a Windows service, packaged with Py2EXE. The service needs to be able to find a settings.cfg
FYI I am seeing different behavior between running the service on Windows 10 Pro x64 and Win Server 2012 R2 Datacenter x64, not to mention when run as a Py2Exe vs. a .py script, as follows:
Win10
CWD: Win10=C:\WINDOWS\system32
Os.path.dirname(sys.executable): C:\MyProgramDir
Sys.argv[0]: C:\MyProgramDir\MyProgram.exe
Win2012
CWD: C:\Windows\system32
Os.path.dirname(sys.executable): C:\Windows\system32
Sys.argv[0]: C:\MyProgramDir\MyProgram.exe
Python Script on Win 10
CWD: C:\MyProgramDir
Os.path.dirname(sys.executable): C:\MyPythonDir\python.exe
Sys.argv[0]: = C:\MyProgramDir\MyProgram.py
Some of this makes sense-when running as a python script, the executable is python.exe, and the first parameter passed to python.exe would be MyProgram.py
The surprise is that Os.path.dirname(sys.executable) returns different results on Win10 than it does on Server 2012.
It seems that looking to Sys.argv[0] is (in my limited testing) the only consistent place to look for the path of the program file.
If pywin32 reported CWD as the program file directory, this would be convenient. However, it isn't a huge deal to manually figure out the path from Sys.argv[0]
From: Mark Hammond [mailto:[email protected]] Sent: Monday, May 09, 2016 4:22 PM To: [pywin32:bugs] [email protected] Subject: [pywin32:bugs] #720 CWD is still \Windows\System32 for service application
The code remains in place in PythonService_main in PythonService.cpp - however, it's certainly possible that it is not working as expected for you. I'll try and find some time to take a look.
[bugs:#720] https://sourceforge.net/p/pywin32/bugs/720/ CWD is still \Windows\System32 for service application
Status: open Group: v1.0 (example) Labels: service cwd Created: Mon May 09, 2016 04:39 PM UTC by David Rueter Last Updated: Mon May 09, 2016 04:39 PM UTC Owner: nobody
According to the changelog at http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/raw-file/tip/CHANGES .txt build 204 added this fix:
Python services will no longer have %System32% as the CWD when they start (the directory of the hosting exectutable will be used instead). This should prevent ImportError due to DLLs in that System32 directory being found for Python imports.
But I am using build 219 on Python 3.4 / x64 and my service application reports that at servce start the current working direcory is in fact C:\Windows\System32
The following information was included with the event:
CWD=C:\WINDOWS\system32
This log message was written by my app at the start of function def SvcDoRun(self):
Am I misunderstanding or doing something wrong? Or did the fix mentioned in build 204 subsequently go away?
Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/pywin32/bugs/720/
To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
Original comment by: davidrueter
Thanks for the update - note that py2exe will have slightly different behaviour and in general does not hit that "set the cwd" code - the easiest fix for that is probably to modify boot_service.py in py2exe to set the cwd before calling the servicemanager functions.
Original comment by: mhammond
FYI, I ended up writing a procedure like this to figure out what directory to use. I have tested it only on Windows-but it does the job on running as a python script, running as a Py2Exe .EXE, running as a Py2Exe service, on Win10 x64 and Win Server 2012 x64.
import os
import sys
def get_program_directory():
program_cmd = sys.argv[0]
program_directory = ''
program_filename = ''
if program_cmd:
program_directory, program_filename = os.path.split(program_cmd)
if not program_directory:
# no path is provided if running the python script as: python
myscript.py
# fall back to CWD
program_directory = os.getcwd()
if program_directory.endswith('system32'):
# a service application may return C:\Windows\System32 as the
CWD
# Look to the executable path.
program_directory = os.path.dirname(sys.executable)
if program_directory.endswith('system32'):
# However this too will be returned as C:\Windows\System32
when
# running as a service on Windows Server 2012 R2. In that
case...
# we are stuck.
program_directory = ''
program_directory = os.path.normpath(program_directory)
if not program_directory.endswith(os.sep):
program_directory += os.sep
return program_directory, program_filename
From: David Rueter [mailto:[email protected]] Sent: Monday, May 09, 2016 5:15 PM To: '[pywin32:bugs] ' [email protected] Subject: RE: [pywin32:bugs] #720 CWD is still \Windows\System32 for service application
Thanks, Mark. I have a Windows service, packaged with Py2EXE. The service needs to be able to find a settings.cfg
FYI I am seeing different behavior between running the service on Windows 10 Pro x64 and Win Server 2012 R2 Datacenter x64, not to mention when run as a Py2Exe vs. a .py script, as follows:
Win10
CWD: Win10=C:\WINDOWS\system32
Os.path.dirname(sys.executable): C:\MyProgramDir
Sys.argv[0]: C:\MyProgramDir\MyProgram.exe
Win2012
CWD: C:\Windows\system32
Os.path.dirname(sys.executable): C:\Windows\system32
Sys.argv[0]: C:\MyProgramDir\MyProgram.exe
Python Script on Win 10
CWD: C:\MyProgramDir
Os.path.dirname(sys.executable): C:\MyPythonDir\python.exe
Sys.argv[0]: = C:\MyProgramDir\MyProgram.py
Some of this makes sense-when running as a python script, the executable is python.exe, and the first parameter passed to python.exe would be MyProgram.py
The surprise is that Os.path.dirname(sys.executable) returns different results on Win10 than it does on Server 2012.
It seems that looking to Sys.argv[0] is (in my limited testing) the only consistent place to look for the path of the program file.
If pywin32 reported CWD as the program file directory, this would be convenient. However, it isn't a huge deal to manually figure out the path from Sys.argv[0]
From: Mark Hammond [mailto:[email protected]] Sent: Monday, May 09, 2016 4:22 PM To: [pywin32:bugs] <[email protected] mailto:[email protected] > Subject: [pywin32:bugs] #720 CWD is still \Windows\System32 for service application
The code remains in place in PythonService_main in PythonService.cpp - however, it's certainly possible that it is not working as expected for you. I'll try and find some time to take a look.
[bugs:#720] https://sourceforge.net/p/pywin32/bugs/720/ CWD is still \Windows\System32 for service application
Status: open Group: v1.0 (example) Labels: service cwd Created: Mon May 09, 2016 04:39 PM UTC by David Rueter Last Updated: Mon May 09, 2016 04:39 PM UTC Owner: nobody
According to the changelog at http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/raw-file/tip/CHANGES .txt build 204 added this fix:
Python services will no longer have %System32% as the CWD when they start (the directory of the hosting exectutable will be used instead). This should prevent ImportError due to DLLs in that System32 directory being found for Python imports.
But I am using build 219 on Python 3.4 / x64 and my service application reports that at servce start the current working direcory is in fact C:\Windows\System32
The following information was included with the event:
CWD=C:\WINDOWS\system32
This log message was written by my app at the start of function def SvcDoRun(self):
Am I misunderstanding or doing something wrong? Or did the fix mentioned in build 204 subsequently go away?
Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/pywin32/bugs/720/
To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
Original comment by: davidrueter