<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>That error typically means your Python code raised an exception
      before the service could be started. You probably need some way to
      track that down - eg, some way to discover stdout/stderr from your
      service - win32traceutil etc might help depending on how the
      service is configured. Does the demo itself work? Does it work
      without the command-line mods?</p>
    <p>Mark<br>
    </p>
    <div class="moz-cite-prefix">On 2023-06-06 9:57 a.m., Gualtiero
      Scotti wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAAD_EgF1cG22vF=45bZ9CSFNDuAhhGZON6Z=bFZ=RFP-9NUhJQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">Thank you Mark for your response, and your
        explanation.You are right, python.exe is not involved but I
        thought you handled the flag in your embedded calls..
        <div>When I try to start service it doesn't start and the error 
          message  is:</div>
        <div><b><br>
            Error starting service: The service did not respond to the
            start or control request in a timely fashion.</b><br>
        </div>
        <div><b><br>
          </b></div>
        <div>
          <div
style="background-color:rgb(30,31,34);color:rgb(188,190,196);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre">_svc_name_ = <span
          style="color:rgb(106,171,115)">'XXXXXXXXXXXXXXX'
</span>_svc_display_name_ = <span style="color:rgb(106,171,115)">'XXXXXXXXXXXXXXXXXXX'
</span>_svc_description_ = <span style="color:rgb(106,171,115)">'SXXXXXXXXXXXXXXX'
</span>_exe_name_ = sys.executable
_exe_args_ = <span style="color:rgb(106,171,115)">'-X utf8 "' </span>+ os.path.abspath(__file__) + <span
          style="color:rgb(106,171,115)">'"'</span></div>
        </div>
        <div><b><br>
          </b></div>
        <div>When I take a look to the service on Regedit I've the
          following situation:</div>
        <div>ImagePath = <b>"C:\Program Files
            (x86)\snapp-device-manager-win32\Python\python.exe" -X utf8
            "C:\Program Files
            (x86)\xxxxxxxxxxxxxx\winservice_launcher32.py"</b></div>
        <div>I can't find any logs about failure.... windows doesn't
          help me......</div>
        <div><img src="cid:part1.qAzrjGrN.YL2pRpYH@skippinet.com.au"
            alt="image.png" class="" width="562" height="242"><br>
        </div>
        <div><br>
        </div>
        <div>Thanks in advance for your availability.</div>
        <div>Best regards</div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Il giorno mar 6 giu 2023 alle
          ore 15:05 Mark Hammond <<a
            href="mailto:mhammond@skippinet.com.au"
            moz-do-not-send="true" class="moz-txt-link-freetext">mhammond@skippinet.com.au</a>>
          ha scritto:<br>
        </div>
        <blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div>
            <p>pythonservice.exe doesn't "wrap" python.exe - it's a
              stand-alone executable which embeds Python. Thus there's
              no way to pass a cmd-line param to python.exe as
              python.exe isn't involved. You could patch
              pythonservice.exe to handle that arg, then do whatever it
              is python.exe does with that arg.</p>
            <p>If you told me more about what went wrong with using
              python.exe as the executable for the service we might be
              able to work something out there, but "doesn't work" has
              never been a useful starting point.</p>
            <p>Cheers,</p>
            <p>Mark<br>
            </p>
            <div>On 2023-06-06 2:56 a.m., Gualtiero Scotti wrote:<br>
            </div>
            <blockquote type="cite">
              <div dir="ltr">HI Mark and Hi all,
                <div>as I wrote previously Mark's solution did not work
                  for me. I need pythonsevice.exe is able to pass -X
                  utf8 parameter to python interpreter or in alternative
                  is able to evaluate evaluate the environment
                  variable PYTHONUTF8=1. Any suggestions?</div>
                <div>Please help me.</div>
                <div>Thanks in advance</div>
              </div>
              <div dir="ltr"><br>
              </div>
              <br>
              <div class="gmail_quote">
                <div dir="ltr" class="gmail_attr">Il giorno lun 5 giu
                  2023 alle ore 10:51 Gualtiero Scotti <<a
                    href="mailto:tierox@gmail.com" target="_blank"
                    moz-do-not-send="true" class="moz-txt-link-freetext">tierox@gmail.com</a>>
                  ha scritto:<br>
                </div>
                <blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                  <div dir="ltr">HI Mark,
                    <div>I've tried your solution but it doesn't work.
                      When calling python directly, service fails to
                      start. </div>
                    <div>When i use </div>
                    <div>
                      <div
style="background-color:rgb(30,31,34);color:rgb(188,190,196);font-family:"JetBrains Mono",monospace;font-size:9.8pt;white-space:pre-wrap"><span
                      style="color:rgb(122,126,133)">#_exe_name_ = sys.executable
</span><span style="color:rgb(122,126,133)">#_exe_args_ = ' -X utf8 ' + '"' + os.path.abspath(sys.argv[0]) + '"'</span></div>
                    </div>
                    <div>service are directly linked to my python app
                      without using pythonservice.exe. ( I saw this on
                      Regedit)</div>
                    <div><br>
                    </div>
                    <div><u>As you wrote on your example:</u></div>
                    <div><u><br>
                      </u></div>
                    <div><br>
                    </div>
                    <div># This is an example of a service hosted by
                      python.exe rather than<br>
                      # pythonservice.exe.<br>
                      <br>
                      # <b>Note that it is very rare that using
                        python.exe is a better option</b><br>
                      # than the default pythonservice.exe - the latter
                      has better error handling<br>
                      # so that if Python itself can't be initialized or
                      there are very early<br>
                      # i<b>mport errors, you will get error details
                        written to the event log</b>.  When<br>
                      # using python.exe instead, you are forced to wait
                      for the interpreter startup<br>
                      # and imports to succeed before you are able to
                      effectively setup your own<br>
                      # error handling.<br>
                      <br>
                      <b># So in short, please make sure you *really*
                        want to do this, otherwise just<br>
                        # stick with the default.</b><br>
                    </div>
                    <div><b><br>
                      </b></div>
                    <div> I need to use pythonservice.exe but I don't
                      understand how it wraps my python application. Is
                      there a way to tell to pythonservice.exe to call
                      python with some interpreter arguments?</div>
                    <div>Thanks in advance.</div>
                    <div><b><br>
                      </b></div>
                  </div>
                  <br>
                  <div class="gmail_quote">
                    <div dir="ltr" class="gmail_attr">Il giorno gio 1
                      giu 2023 alle ore 17:05 Mark Hammond <<a
                        href="mailto:skippy.hammond@gmail.com"
                        target="_blank" moz-do-not-send="true"
                        class="moz-txt-link-freetext">skippy.hammond@gmail.com</a>>
                      ha scritto:<br>
                    </div>
                    <blockquote class="gmail_quote"
style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                      <div>
                        <p><a
href="https://github.com/mhammond/pywin32/blob/main/win32/Demos/service/nativePipeTestService.py"
                            target="_blank" moz-do-not-send="true"
                            class="moz-txt-link-freetext">https://github.com/mhammond/pywin32/blob/main/win32/Demos/service/nativePipeTestService.py</a>
                          is a demo of using a service using python.exe
                          and supports specifying the command-line, so
                          that might be an option?</p>
                        <p>Mark<br>
                        </p>
                        <div>On 2023-06-01 5:28 a.m., Gualtiero Scotti
                          wrote:<br>
                        </div>
                        <blockquote type="cite">
                          <div dir="ltr">Hi, I need to start my python
                            application through windows service. For
                            this purpose I use pywin32.
                            <div>I'm not able to make a Python instance
                              to evaluate the -X uf8 flag.</div>
                            <div><br>
                            </div>
                            <div>This flag must be passed during python
                              invocation only and not when it is already
                              started.</div>
                            <div>After some investigations I've found
                              the service launch <b>pythonservice.exe</b>
                              wrapper but I don't know how to pass the
                              flag to the Python interpreter.</div>
                            <div><br>
                            </div>
                            <div>Is there another option to set this
                              flag? It is possible to set an env
                              variable <b>PYTHONUTF8=1 </b> but
                              pythonservice.exe doesn't seem to evaluate
                              it...</div>
                            <div>
                              <div><b><br>
                                </b></div>
                              <div><b>My environment</b></div>
                              <div><b>- Windows 11 Home 22H2</b></div>
                              <div><b>- Python 3.11.1</b> (tags/v3.11.1:a7a450f,
                                Dec  6 2022, 19:43:28) [MSC v.1934 32
                                bit (Intel)] on win32</div>
                              <div>- <b>Pywin32 305</b></div>
                              - <b>Pywin32-ctypes 0.2.0</b></div>
                            <div><br>
                            </div>
                            <div><b>Python documentation</b></div>
                            <div>
                              <h2
style="font-family:"Lucida Grande",Arial,sans-serif;font-weight:normal;color:rgb(26,26,26);border:0px;margin:0px;padding:0.3em 0px;font-size:25.6px"><span>4.7. </span>UTF-8
                                mode<a
href="https://docs.python.org/3/using/windows.html#utf-8-mode"
                                  title="Permalink to this headline"
style="color:rgb(0,114,170);text-decoration-line:none;font-size:0.8em;padding:0px 4px"
                                  target="_blank" moz-do-not-send="true">¶</a></h2>
                              <div
style="font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">
                                <p style="line-height:1.4"><span
                                    style="font-style:italic">New in
                                    version 3.7.</span></p>
                              </div>
                              <p
style="line-height:1.4;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">Windows
                                still uses legacy encodings for the
                                system encoding (the ANSI Code Page).
                                Python uses it for the default encoding
                                of text files (e.g. <a
href="https://docs.python.org/3/library/locale.html#locale.getencoding"
                                  title="locale.getencoding"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><code
style="background-color:transparent;padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                      style="white-space:nowrap">locale.getencoding()</span></code></a>).</p>
                              <p
style="line-height:1.4;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">This
                                may cause issues because UTF-8 is widely
                                used on the internet and most Unix
                                systems, including WSL (Windows
                                Subsystem for Linux).</p>
                              <p
style="line-height:1.4;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">You
                                can use the <a
href="https://docs.python.org/3/library/os.html#utf8-mode"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><span>Python
                                    UTF-8 Mode</span></a> to change the
                                default text encoding to UTF-8. You can
                                enable the <a
href="https://docs.python.org/3/library/os.html#utf8-mode"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><span>Python
                                    UTF-8 Mode</span></a> via the <code
style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                    style="white-space:nowrap">-X</span> <span
                                    style="white-space:nowrap">utf8</span></code> command
                                line option, or the <code
style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                    style="white-space:nowrap">PYTHONUTF8=1</span></code> environment
                                variable. See <span
id="m_-1163491933251433179m_-3821053292700552241m_-3479018251099287660m_-8621810217580148104gmail-index-15"></span><a
href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUTF8"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><code
style="background-color:transparent;padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                      style="white-space:nowrap">PYTHONUTF8</span></code></a> for
                                enabling UTF-8 mode, and <a
href="https://docs.python.org/3/using/windows.html#setting-envvars"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><span>Excursus:
                                    Setting environment variables</span></a> for
                                how to modify environment variables.</p>
                              <p
style="line-height:1.4;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">When
                                the <a
href="https://docs.python.org/3/library/os.html#utf8-mode"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                  moz-do-not-send="true"><span>Python
                                    UTF-8 Mode</span></a> is enabled,
                                you can still use the system encoding
                                (the ANSI Code Page) via the “mbcs”
                                codec.</p>
                              <p
style="line-height:1.4;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">Note
                                that adding <code
style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                    style="white-space:nowrap">PYTHONUTF8=1</span></code> to
                                the default environment variables will
                                affect all Python 3.7+ applications on
                                your system. If you have any Python 3.7+
                                applications which rely on the legacy
                                system encoding, it is recommended to
                                set the environment variable temporarily
                                or use the <code
style="background-color:rgb(236,240,243);padding:0px 1px;font-size:15.44px;font-family:ui-monospace,"Cascadia Mono","Segoe UI Mono","Liberation Mono",Menlo,Monaco,Consolas,monospace;border-radius:3px"><span
                                    style="white-space:nowrap">-X</span> <span
                                    style="white-space:nowrap">utf8</span></code> command
                                line option.</p>
                              <div
style="clear:left;margin-top:10px;margin-bottom:10px;padding:7px;background-color:rgb(238,238,238);border:1px solid rgb(204,204,204);border-radius:3px;font-family:"Lucida Grande",Arial,sans-serif;font-size:16px">
                                <p
style="margin:0px 10px 5px 0px;font-weight:bold;display:inline;line-height:1.4">Note</p>
                                 
                                <p
style="line-height:1.4;margin-bottom:5px;display:inline">Even when UTF-8
                                  mode is disabled, Python uses UTF-8 by
                                  default on Windows for:</p>
                                <ul style="margin-bottom:0px">
                                  <li
style="text-align:left;line-height:1.4">
                                    <p
style="margin-bottom:5px;line-height:1.4;margin-top:0px">Console I/O
                                      including standard I/O (see <span
id="m_-1163491933251433179m_-3821053292700552241m_-3479018251099287660m_-8621810217580148104gmail-index-16"></span><a
href="https://peps.python.org/pep-0528/"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                        moz-do-not-send="true"><strong>PEP
                                          528</strong></a> for details).</p>
                                  </li>
                                  <li
style="text-align:left;line-height:1.4">
                                    <p
style="margin-bottom:0px;line-height:1.4;margin-top:0px">The <a
href="https://docs.python.org/3/glossary.html#term-filesystem-encoding-and-error-handler"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                        moz-do-not-send="true"><span>filesystem
                                          encoding</span></a> (see <span
id="m_-1163491933251433179m_-3821053292700552241m_-3479018251099287660m_-8621810217580148104gmail-index-17"></span><a
href="https://peps.python.org/pep-0529/"
style="color:rgb(0,114,170);text-decoration-line:none" target="_blank"
                                        moz-do-not-send="true"><strong>PEP
                                          529</strong></a> for details).</p>
                                  </li>
                                </ul>
                              </div>
                            </div>
                            <div>from -> <a
href="https://docs.python.org/3/using/windows.html" target="_blank"
                                moz-do-not-send="true"
                                class="moz-txt-link-freetext">https://docs.python.org/3/using/windows.html</a><br>
                            </div>
                            <div><br>
                            </div>
                            <div>Please help me!</div>
                            <div>Thanks in advance</div>
                            <div><b><br>
                              </b></div>
                          </div>
                          <br>
                          <fieldset></fieldset>
                          <pre>_______________________________________________
python-win32 mailing list
<a href="mailto:python-win32@python.org" target="_blank"
                          moz-do-not-send="true"
                          class="moz-txt-link-freetext">python-win32@python.org</a>
<a href="https://mail.python.org/mailman/listinfo/python-win32"
                          target="_blank" moz-do-not-send="true"
                          class="moz-txt-link-freetext">https://mail.python.org/mailman/listinfo/python-win32</a>
</pre>
                        </blockquote>
                      </div>
                    </blockquote>
                  </div>
                </blockquote>
              </div>
            </blockquote>
          </div>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>