No module named when using PyInstaller

后端 未结 7 1907
说谎
说谎 2020-11-27 15:11

I try to compile a Python project under Windows 7 using PyInstaller. The project works fine, there are no issues, however when I try to compile it the result doesn\'t work.

相关标签:
7条回答
  • 2020-11-27 15:56

    If the matter is that you don't need Tkinter and friends because you are using PyQt4, then it might be best to avoid loading Tkinter etc altogether. Look into /etc/matplotlibrc and change the defaults to PyQt4, see the 'modified' lines below:

    #### CONFIGURATION BEGINS HERE
    
    # The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
    # CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
    # Template.
    # You can also deploy your own backend outside of matplotlib by
    # referring to the module name (which must be in the PYTHONPATH) as
    # 'module://my_backend'.
    
    #modified 
    #backend      : TkAgg
    backend      : Qt4Agg
    
    
    # If you are using the Qt4Agg backend, you can choose here
    # to use the PyQt4 bindings or the newer PySide bindings to
    # the underlying Qt4 toolkit.
    
    #modified 
    #backend.qt4 : PyQt4        # PyQt4 | PySide
    backend.qt4 : PyQt4        # PyQt4 | PySide
    
    0 讨论(0)
  • 2020-11-27 16:02

    Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.

    There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file. Just change the following line:

    hiddenimports=[],
    

    to

    hiddenimports=["Tkinter", "FileDialog"],
    
    0 讨论(0)
  • 2020-11-27 16:02

    The problem were some runtime dependencies of matplotlib. So the compiling was fine while running the program threw some errors. Because the terminal closed itself immediately I didn't realize that. After redirecting stdout and stderr to a file I could see that I missed the libraries Tkinter and FileDialog. Adding two imports at the top of the main solved this problem.

    0 讨论(0)
  • 2020-11-27 16:05

    I had the same problem with pyinstaller 3.0 and weblib. Importing it in the main didn't help.

    Upgrading to 3.1 and deleting all build files helped.

    pip install --upgrade pyinstaller
    
    0 讨论(0)
  • 2020-11-27 16:12

    Had a similar problem with no module named FileDialog. Discovered that with version 3.2, I could use

    pyinstaller --hidden-import FileDialog ...

    instead of modifying my main script.

    0 讨论(0)
  • 2020-11-27 16:14

    If you are getting ModuleNotFoundError: No module named ... errors and you:

    • call PyInstaller from a directory other than your main script
    • use relative imports in your script

    then your executable can have trouble finding the relative imports.

    This can be fixed by:

    • calling PyInstaller from the same directory as your main script
    • removing any __init__.py files (empty __init__.pt files are not required in Python 3.3+)
    • or using PyInstaller's paths flag to specify a path to search for imports. E.g. if you are calling PyInstaller from a parent folder to your main script, and your script lives in subfolder, then call PyInstaller as such:

      pyinstaller --paths=subfolder subfolder/script.py.

    0 讨论(0)
提交回复
热议问题