Python Forum
How to pass -Xutf8 parametri to python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass -Xutf8 parametri to python
#1
Hi all, I'm struggling with pywin. I use it to wrap a python application and create a windows servire. I need to pass -Xutf8mode to python interpreter. Which is the reccomanded way to do it? I've tried with env variabile on windows servire, but it doesn't work. Any suggestion? Thanks in advance
Reply
#2
To pass the -Xutf8mode flag to the Python interpreter when using pywin32 to create a Windows service, you can modify the service script to include the flag during the initialization of the Python interpreter.

import sys
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import win32timezone
import win32evtlogutil
import pythoncom

class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'

def __init__(self, args):
# Add the -Xutf8mode flag to the Python interpreter
sys.argv.append('-Xutf8mode')

# Call the base class constructor
super().__init__(args)

def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
servicemanager.LogInfoMsg(f'{self._svc_name_} is stopping.')
self.ReportServiceStatus(win32service.SERVICE_STOPPED)

def main(self):
# Your service logic goes here
pass

if __name__ == '__main__':
# Create the service using the MyService class
win32serviceutil.HandleCommandLine(MyService)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this example, the _svc_name_ and _svc_display_name_ attributes have been set to the desired values for your service. The MyService class inherits from win32serviceutil.ServiceFramework, and in its constructor (__init__), the -Xutf8mode flag is appended to sys.argv before calling the base class constructor.

By modifying the service script in this way, the -Xutf8mode flag will be passed to the Python interpreter when the service is started.

Note: Ensure that you have the necessary permissions to install and manage Windows services when working with pywin32.
buran write Jun-06-2023, 06:18 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#3
Hi rajeshgk
thank you for your response.
-Xutf8 parameter must be passed during python interpreter instantiation. I talked with Mark ( a pywin32 developer ) and he told me that when pythonservice.exe is used there is no way to pass parameter to interpreter becasue it doesn't use python.exe but it embeds python directly and flags are not handled.
The only way could be bypass pythonservice.exe and wrapping python application directly but this solution doesn't work for me.
I tested your solution with this:
logger.info(f"UTF8-Mode:" + str(sys.flags.utf8_mode))

Thank you.
Best regards


(Jun-06-2023, 06:10 PM)rajeshgk Wrote: To pass the -Xutf8mode flag to the Python interpreter when using pywin32 to create a Windows service, you can modify the service script to include the flag during the initialization of the Python interpreter.

import sys
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import win32timezone
import win32evtlogutil
import pythoncom

class MyService(win32serviceutil.ServiceFramework):
_svc_name_ = 'MyService'
_svc_display_name_ = 'My Service'

def __init__(self, args):
# Add the -Xutf8mode flag to the Python interpreter
sys.argv.append('-Xutf8mode')

# Call the base class constructor
super().__init__(args)

def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
servicemanager.LogInfoMsg(f'{self._svc_name_} is stopping.')
self.ReportServiceStatus(win32service.SERVICE_STOPPED)

def main(self):
# Your service logic goes here
pass

if __name__ == '__main__':
# Create the service using the MyService class
win32serviceutil.HandleCommandLine(MyService)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this example, the _svc_name_ and _svc_display_name_ attributes have been set to the desired values for your service. The MyService class inherits from win32serviceutil.ServiceFramework, and in its constructor (__init__), the -Xutf8mode flag is appended to sys.argv before calling the base class constructor.

By modifying the service script in this way, the -Xutf8mode flag will be passed to the Python interpreter when the service is started.

Note: Ensure that you have the necessary permissions to install and manage Windows services when working with pywin32.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 874 Jul-27-2023, 12:40 AM
Last Post: tester_V
  How can I pass&return ndarray between python and c++? JESuh 0 1,787 Mar-09-2021, 08:29 AM
Last Post: JESuh
  How do I pass a dictionary for the in3_registry value for the Incubed Python API? nilesh 0 1,456 Jan-11-2021, 11:19 AM
Last Post: nilesh
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,571 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Pass by reference vs Pass by value leodavinci1990 1 2,221 Nov-20-2019, 02:05 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020