简体   繁体   中英

Pyinstaller - Python App with Matplotlib runs from command line, but not from Explorer

I am trying to convert my python program into an.exe on Windows using pyinstaller. Here is a minimal example test_script.py :

import matplotlib.pyplot as plt

print("Script starting")

# do important thing 1 here
print("Important thing 1 done")

# do important thing 2 here
print("Important thing 2 done")

fig, ax = plt.subplots()
print("Fig and ax created")
plt.plot([1,2,3,4,5])
print("plot done")
plt.show()
print("everything done")

When running pyinstaller --onefile test_script.py , pyinstaller runs through and produces the binary which works fine when starting it from Power Shell (all printouts appear and the final plot is displayed). But, when I run it from the explorer, the last printout that appears is Important thing 2 done before the program quits. This led me to suspect that the problem is related to Matlpotlib. I tried passing different arguments to pyinstaller, but none of them lead to success. I also tried wrapping the fig, ax =... line into a try: block to see if any exception was happening, but there were none. Anyone have a suggestion how to make the program work also from the explorer?

On MacOS, the binary works without problem both from the command line and the finder.

Edit:

On suggestion, I tried adding some debugging code like so:

import matplotlib.pyplot as plt
import traceback

print("Script starting")

# do important thing 1 here
print("Important thing 1 done")

# do important thing 2 here
print("Important thing 2 done")

input()
try:
    fig, ax = plt.subplots()
except Exception as e:
    print("Exception!")
    traceback.print_exc()
    input()
print("Fig and ax created")
input()
plt.plot([1,2,3,4,5])
print("plot done")
plt.show()
print("everything done")

When building the above with pyinstaller --console --debug=all test_script.py , I get this message shortly before the program quits: "LOADER: Back to parent (RC: -1066598274)".


I am on Windows 10 using a Miniconda python3 environment with python 3.8.8 and matplotlib 3.2 (tried also 3.3, but same result). Here is the output from the pyinstaller run:

549 INFO: Python: 3.8.8 (conda)
549 INFO: Platform: Windows-10-10.0.18362-SP0
551 INFO: wrote C:\Users\mee\Documents\test_project\test_script.spec
559 INFO: UPX is not available.
560 INFO: Extending PYTHONPATH with paths
['C:\\Users\\mee\\Documents\\test_project',
 'C:\\Users\\mee\\Documents\\test_project']
572 INFO: checking Analysis
588 INFO: Building because C:\Users\mee\Documents\test_project\test_script.py changed
588 INFO: Initializing module dependency graph...
591 INFO: Caching module graph hooks...
598 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
602 INFO: Analyzing base_library.zip ...
2788 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-pa
ckages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
2788 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib'
5147 INFO: Caching module dependency graph...
5274 INFO: running Analysis Analysis-00.toc
5276 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\mee\Miniconda3\envs\env\python.exe
5532 INFO: Analyzing C:\Users\mee\Documents\test_project\test_script.py
6333 INFO: Processing pre-safe import module hook setuptools.extern.six.moves from 'C:\\Users\\mee\\Miniconda3\\env
s\\env\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-setuptools.extern.six.moves.py'.
6737 INFO: Processing pre-find module path hook site from 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-package
s\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
6737 INFO: site: retargeting to fake-dir 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\fake-modules'
10269 INFO: Processing pre-safe import module hook six.moves from 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site
-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
11970 INFO: Processing module hooks...
11970 INFO: Loading module hook 'hook-difflib.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
11972 INFO: Excluding import of doctest from module difflib
11972 INFO: Loading module hook 'hook-distutils.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
11974 INFO: Loading module hook 'hook-distutils.util.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
11975 INFO: Excluding import of lib2to3.refactor from module distutils.util
11975 INFO: Loading module hook 'hook-encodings.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
12092 INFO: Loading module hook 'hook-heapq.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
12094 INFO: Excluding import of doctest from module heapq
12094 INFO: Loading module hook 'hook-lib2to3.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
12130 INFO: Loading module hook 'hook-matplotlib.backends.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
12798 INFO:   Matplotlib backend "GTK3Agg": ignored
    backend Gtk3Agg requires cairo
13151 INFO:   Matplotlib backend "GTK3Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
13503 INFO:   Matplotlib backend "MacOSX": ignored
    cannot import name '_macosx' from 'matplotlib.backends' 
(C:\Users\mee\Miniconda3\envs\env\lib\site-packages\matplotlib\backends\__init__.py)
13854 INFO:   Matplotlib backend "nbAgg": ignored
    No module named 'IPython'
14385 INFO:   Matplotlib backend "Qt4Agg": added
14736 INFO:   Matplotlib backend "Qt4Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
15259 INFO:   Matplotlib backend "Qt5Agg": added
15610 INFO:   Matplotlib backend "Qt5Cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
16081 INFO:   Matplotlib backend "TkAgg": added
16554 INFO:   Matplotlib backend "TkCairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
17059 INFO:   Matplotlib backend "WebAgg": added
17510 INFO:   Matplotlib backend "WX": ignored
    No module named 'wx'
17861 INFO:   Matplotlib backend "WXAgg": ignored
    No module named 'wx'
18211 INFO:   Matplotlib backend "WXCairo": ignored
    No module named 'wx'
18561 INFO:   Matplotlib backend "agg": added
18914 INFO:   Matplotlib backend "cairo": ignored
    cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
19377 INFO:   Matplotlib backend "pdf": added
19835 INFO:   Matplotlib backend "pgf": added
20192 INFO:   Matplotlib backend "ps": added
20548 INFO:   Matplotlib backend "svg": added
21006 INFO:   Matplotlib backend "template": added
21343 INFO: Loading module hook 'hook-matplotlib.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21663 INFO: Loading module hook 'hook-multiprocessing.util.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21665 INFO: Excluding import of test from module multiprocessing.util
21665 INFO: Excluding import of test.support from module multiprocessing.util
21665 INFO: Loading module hook 'hook-numpy.core.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21767 INFO: MKL libraries found when importing numpy. Adding MKL to binaries
21770 INFO: Loading module hook 'hook-numpy.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21771 INFO: Loading module hook 'hook-numpy._pytesttester.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21774 INFO: Excluding import of pytest from module numpy._pytesttester
21774 INFO: Loading module hook 'hook-pickle.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
21776 INFO: Excluding import of argparse from module pickle
21776 INFO: Loading module hook 'hook-pkg_resources.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
22059 INFO: Processing pre-safe import module hook win32com from 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-
packages\\_pyinstaller_hooks_contrib\\hooks\\pre_safe_import_module\\hook-win32com.py'.
22452 WARNING: Hidden import "pkg_resources.py2_warn" not found!
22453 WARNING: Hidden import "pkg_resources.markers" not found!
22454 INFO: Excluding import of __main__ from module pkg_resources
22455 INFO: Loading module hook 'hook-PyQt5.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
22775 INFO: Loading module hook 'hook-PyQt5.QtCore.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
22844 INFO: Loading module hook 'hook-PyQt5.QtGui.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23068 INFO: Loading module hook 'hook-PyQt5.QtWidgets.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23289 INFO: Loading module hook 'hook-scipy.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23293 INFO: Loading module hook 'hook-setuptools.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23852 INFO: Excluding import of setuptools.py27compat from module setuptools.command.easy_install
23852 INFO: Excluding import of setuptools.py27compat from module setuptools.package_index
23853 INFO: Excluding import of setuptools.py27compat from module setuptools.depends
23854 INFO: Excluding import of setuptools.py33compat from module setuptools.package_index
23855 INFO: Excluding import of setuptools.py33compat from module setuptools.depends
23855 INFO: Loading module hook 'hook-sysconfig.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23856 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23857 INFO: Loading module hook 'hook-xml.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
23906 INFO: Loading module hook 'hook-_tkinter.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
24130 INFO: checking Tree
24173 INFO: checking Tree
24296 INFO: checking Tree
24300 INFO: Loading module hook 'hook-pythoncom.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
24575 INFO: Loading module hook 'hook-pywintypes.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
24851 INFO: Loading module hook 'hook-win32com.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25154 INFO: Loading module hook 'hook-setuptools.msvc.py' from 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks'...
25156 INFO: Excluding import of numpy from module setuptools.msvc
25202 INFO: Looking for ctypes DLLs
25271 INFO: Analyzing run-time hooks ...
25277 INFO: Including run-time hook 'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\
\rthooks\\pyi_rth_multiprocessing.py'
25279 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgres.py'
25281 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32api.py'
25282 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_win32comgenpy.py'
25283 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
25285 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pyqt5.py'
25285 INFO: Including run-time hook 
'C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_mplconfig.py'
25296 INFO: Looking for dynamic libraries
25309 WARNING: lib not found: impi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_intelmpi_ilp64.dll
25760 WARNING: lib not found: impi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_intelmpi_lp64.dll
25790 WARNING: lib not found: msmpi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_msmpi_ilp64.dll
26062 WARNING: lib not found: mpich2mpi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_mpich2_lp64.dll
26422 WARNING: lib not found: msmpi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_msmpi_lp64.dll
26545 WARNING: lib not found: pgf90.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_pgi_thread.dll
26551 WARNING: lib not found: pgc14.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_pgi_thread.dll
26554 WARNING: lib not found: pgf90rtl.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_pgi_thread.dll
26598 WARNING: lib not found: mpich2mpi.dll dependency of 
C:\Users\mee\Miniconda3\envs\env\Library\bin\mkl_blacs_mpich2_ilp64.dll
27685 INFO: Looking for eggs
27685 INFO: Using Python library C:\Users\mee\Miniconda3\envs\env\python38.dll
27685 INFO: Found binding redirects: 
[]
27693 INFO: Warnings written to C:\Users\mee\Documents\test_project\build\test_script\warn-test_script.txt
27806 INFO: Graph cross-reference written to 
C:\Users\mee\Documents\test_project\build\test_script\xref-test_script.html
27857 INFO: checking PYZ
27862 INFO: Building because toc changed
27862 INFO: Building PYZ (ZlibArchive) C:\Users\mee\Documents\test_project\build\test_script\PYZ-00.pyz
29333 INFO: Building PYZ (ZlibArchive) C:\Users\mee\Documents\test_project\build\test_script\PYZ-00.pyz completed 
successfully.
29355 INFO: checking PKG
29366 INFO: Building because toc changed
29366 INFO: Building PKG (CArchive) PKG-00.pkg
112497 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
112530 INFO: Bootloader 
C:\Users\mee\Miniconda3\envs\env\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
112530 INFO: checking EXE
112540 INFO: Building because icon changed
112540 INFO: Building EXE from EXE-00.toc
112566 INFO: Copying icons from 
['C:\\Users\\mee\\Miniconda3\\envs\\env\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
112567 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
112567 INFO: Writing RT_ICON 1 resource with 3752 bytes
112567 INFO: Writing RT_ICON 2 resource with 2216 bytes
112567 INFO: Writing RT_ICON 3 resource with 1384 bytes
112567 INFO: Writing RT_ICON 4 resource with 37019 bytes
112567 INFO: Writing RT_ICON 5 resource with 9640 bytes
112568 INFO: Writing RT_ICON 6 resource with 4264 bytes
112568 INFO: Writing RT_ICON 7 resource with 1128 bytes
112571 INFO: Updating manifest in C:\Users\mee\Documents\test_project\build\test_script\run.exe.7qgfmhe5
112571 INFO: Updating resource type 24 name 1 language 0
112574 INFO: Appending archive to EXE C:\Users\mee\Documents\test_project\dist\test_script.exe
112749 INFO: Building EXE from EXE-00.toc completed successfully.

but starting the.exe file from the file explorer, the program just quits

Seems it just finished the execution.

Try debug it with some code for not closing the app.

This might help:

PyInstaller's.exe file closes immediately after opening

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM