Application creating second instance of self

81 views
Skip to first unread message

Chris Coerber

unread,
Sep 23, 2023, 3:54:57 PM9/23/23
to PyInstaller
Hey everyone!  I have a Python application using Tkinter that I have bundled with PyInstaller.  It has a number of large libraries such as tensorflow, torch, and so on.  It's been very challenging to get everything in the right place.

When I finally got things working, one of my first issues I ran into was that, when I performed a particular workflow, a second instance of my application was opened and the original didn't continue until I closed this new one.  I traced the issue to somewhere in the ultralytics module, but I have no idea where in there exactly the problem is.

I've read some about how sys.executable() is different when you run a packaged application, so I'm sure that could be playing a role, but I have no idea how to fix that.

My next idea was to package the app without a console at all.  In this instance, I ran into another problem with my app crashing in the same workflow, but without the console window, I'm unable to see any errors.  I'm very stuck!  I've probably put around 20+ hours into packaging this app and keep hitting walls!

Thanks for any help :)

bwoodsend

unread,
Sep 24, 2023, 4:59:46 AM9/24/23
to PyInstaller

I’ve read some about how sys.executable() is different when you run a packaged application, so I’m sure that could be playing a role, but I have no idea how to fix that.

A library that tries to run Python subprocesses will cause a PyInstaller application to keep spawning itself because sys.executable points to your application rather than python.exe. There’s no universal magic fix for this. Sometimes you can configure a library not to use subprocesses. Sometimes the library uses Python’s multprocessing library for which you can enable PyInstaller’s special handling. In nastier cases, you can put a if sys.argv[1:3] == [whatever arguments the subprocess always uses]: do the task the subprocess was intended to do at the top of your code. If you can strip the problem down to one specific library and a small bit of code then share that, I should be able to give you a more precise workaround.

Chris Coerber

unread,
Sep 25, 2023, 2:55:52 AM9/25/23
to pyins...@googlegroups.com
Thanks so much for the response! I have narrowed it down to the ultralytics library but I haven't been able to identify where in that code it's calling a subprocess. I'll try to investigate some more. 

On Sun, Sep 24, 2023, 1:59 AM bwoodsend <bwoo...@gmail.com> wrote:

I’ve read some about how sys.executable() is different when you run a packaged application, so I’m sure that could be playing a role, but I have no idea how to fix that.

A library that tries to run Python subprocesses will cause a PyInstaller application to keep spawning itself because sys.executable points to your application rather than python.exe. There’s no universal magic fix for this. Sometimes you can configure a library not to use subprocesses. Sometimes the library uses Python’s multprocessing library for which you can enable PyInstaller’s special handling. In nastier cases, you can put a if sys.argv[1:3] == [whatever arguments the subprocess always uses]: do the task the subprocess was intended to do at the top of your code. If you can strip the problem down to one specific library and a small bit of code then share that, I should be able to give you a more precise workaround.

--
You received this message because you are subscribed to a topic in the Google Groups "PyInstaller" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyinstaller/Z76GgQlCruY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/a3f0bcd5-07e2-40c6-8705-22a935d3b154n%40googlegroups.com.

Chris Coerber

unread,
Sep 25, 2023, 2:56:10 AM9/25/23
to pyins...@googlegroups.com
I have found at least one of the issues.  I dumped the stack trace in subprocess.run() to find out who was calling it.  During my code, at some point, a numpy function called check_support_sve() gets called.  It resides in numpy/testing/_private/utils.py.  Here's the code

def check_support_sve():
    """
    gh-22982
    """
   
    import subprocess
    cmd = 'lscpu'
    try:
        output = subprocess.run(cmd, capture_output=True, text=True)
        return 'sve' in output.stdout
    except OSError:
        return False


So, I am wondering how (and if) I can modify that function to behave differently if it recognizes that it's an executable packaged by PyInstaller.  I saw something similar in a package called cpuinfo, where they specifically mention Pyinstaller issues, but not exactly sure how to adapt it to numpy.

bwoodsend

unread,
Sep 26, 2023, 3:52:21 AM9/26/23
to PyInstaller

That sounds like a red herring. It’s specifically subprocess.run([sys.executable, ...]) that we’re interested in — any other command should run fine under PyInstaller.

Chris Coerber

unread,
Sep 26, 2023, 2:24:44 PM9/26/23
to pyins...@googlegroups.com
Ok, looks like I'll have to do some more digging.  

The other issue I'm running into has to do with TensorFlow.  My goal is to create a single windowed executable with no console.  Unfortunately, when I package my application without a console, I run into this error:

WARNING - AutoGraph is not available in this environment: functions lack code information. This is typical of some environments like the interactive Python shell. See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code for more information.

Any ideas of a workaround?  I've looked through various forums and such and haven't found a solution yet.

On Tue, Sep 26, 2023 at 12:52 AM bwoodsend <bwoo...@gmail.com> wrote:

That sounds like a red herring. It’s specifically subprocess.run([sys.executable, ...]) that we’re interested in — any other command should run fine under PyInstaller.

--
You received this message because you are subscribed to a topic in the Google Groups "PyInstaller" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyinstaller/Z76GgQlCruY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyinstaller...@googlegroups.com.

Chris Barker

unread,
Sep 26, 2023, 2:24:49 PM9/26/23
to pyins...@googlegroups.com
On Tue, Sep 26, 2023 at 12:52 AM bwoodsend <bwoo...@gmail.com> wrote:

That sounds like a red herring. It’s specifically subprocess.run([sys.executable, ...]) that we’re interested in — any other command should run fine under PyInstaller.

indeed -- a search for "sys.executable" may find the culprit. 

-CHB

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Chris Coerber

unread,
Sep 27, 2023, 2:48:45 AM9/27/23
to pyins...@googlegroups.com
Sadly, there are about 250 matches to that call within all the libraries packaged in my application!  I did some more logging.  The last time subprocess.run() gets called before a second instance of my application is initialized is in the following custom logging I did...

*** check_output command called by _syscmd_ver at platform.py:284
*** run command called by check_output at subprocess.py:480

I looked up _syscmd_ver in platform.py.  But, nowhere in there do I see it using sys.executable.  Here's the relevant portion of _syscmd_ver()

    import subprocess
    for cmd in ('ver', 'command /c ver', 'cmd /c ver'):
        try:
            info = subprocess.check_output(cmd,
                                           stdin=subprocess.DEVNULL,
                                           stderr=subprocess.DEVNULL,
                                           text=True,
                                           encoding="locale",
                                           shell=True)
        except (OSError, subprocess.CalledProcessError) as why:
            #print('Command %s failed: %s' % (cmd, why))
            continue
        else:
            break
    else:
        return system, release, version


--
You received this message because you are subscribed to a topic in the Google Groups "PyInstaller" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyinstaller/Z76GgQlCruY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyinstaller...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages